google-research

Форк
0
/
optimization_test.py 
51 строка · 1.7 Кб
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 optimization."""
17

18
from __future__ import absolute_import
19
from __future__ import division
20
from __future__ import print_function
21

22
import tensorflow.compat.v1 as tf
23
from mobilebert import optimization
24

25

26
class OptimizationTest(tf.test.TestCase):
27

28
  def test_adam(self):
29
    with self.test_session() as sess:
30
      w = tf.get_variable(
31
          "w",
32
          shape=[3],
33
          initializer=tf.constant_initializer([0.1, -0.2, -0.1]))
34
      x = tf.constant([0.4, 0.2, -0.5])
35
      loss = tf.reduce_mean(tf.square(x - w))
36
      tvars = tf.trainable_variables()
37
      grads = tf.gradients(loss, tvars)
38
      global_step = tf.train.get_or_create_global_step()
39
      optimizer = optimization.AdamWeightDecayOptimizer(learning_rate=0.2)
40
      train_op = optimizer.apply_gradients(zip(grads, tvars), global_step)
41
      init_op = tf.group(tf.global_variables_initializer(),
42
                         tf.local_variables_initializer())
43
      sess.run(init_op)
44
      for _ in range(100):
45
        sess.run(train_op)
46
      w_np = sess.run(w)
47
      self.assertAllClose(w_np.flat, [0.4, 0.2, -0.5], rtol=1e-2, atol=1e-2)
48

49

50
if __name__ == "__main__":
51
  tf.test.main()
52

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

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

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

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