google-research

Форк
0
/
variable_replace_test.py 
55 строк · 1.6 Кб
1
# coding=utf-8
2
# Copyright 2024 The Google Research Authors.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15

16
"""Tests for task_set.variable_replace."""
17

18
import sonnet as snt
19

20
from task_set import variable_replace
21
import tensorflow.compat.v1 as tf
22

23

24
class VaraibleReplaceTest(tf.test.TestCase):
25

26
  def test_variable_replace_getter(self):
27
    with self.test_session() as sess:
28
      context = variable_replace.VariableReplaceGetter()
29
      mod = snt.Linear(1, custom_getter=context)
30

31
      inp_data = tf.ones([10, 1])
32
      with context.use_variables():
33
        y1 = mod(inp_data)
34
        sess.run(tf.initialize_all_variables())
35
        np_y1 = sess.run(y1)
36

37
      values = context.get_variable_dict()
38

39
      new_values = {k: v + 1 for k, v in values.items()}
40

41
      with context.use_value_dict(new_values):
42
        np_y2 = mod(inp_data).eval()
43

44
      self.assertNear((np_y2 - np_y1)[0], 2, 1e-8)
45

46
      for v in values.values():
47
        v.assign(v + 1).eval()
48

49
      with context.use_variables():
50
        np_y3 = mod(inp_data).eval()
51
        self.assertNear((np_y3 - np_y2)[0], 0, 1e-8)
52

53

54
if __name__ == '__main__':
55
  tf.test.main()
56

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.