|
| 1 | +# Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +"""Tests for MovingAverage optimizers.""" |
| 16 | +from __future__ import absolute_import |
| 17 | +from __future__ import division |
| 18 | +from __future__ import print_function |
| 19 | + |
| 20 | +import tensorflow as tf |
| 21 | + |
| 22 | +from tensorflow_addons.optimizers import MovingAverage |
| 23 | +from tensorflow_addons.utils import test_utils |
| 24 | + |
| 25 | + |
| 26 | +class MovingAverageTest(tf.test.TestCase): |
| 27 | + @test_utils.run_in_graph_and_eager_modes |
| 28 | + def test_run(self): |
| 29 | + for sequential_update in [True, False]: |
| 30 | + var0 = tf.Variable([1.0, 2.0]) |
| 31 | + var1 = tf.Variable([3.0, 4.0]) |
| 32 | + |
| 33 | + grads0 = tf.constant([0.1, 0.1]) |
| 34 | + grads1 = tf.constant([0.01, 0.01]) |
| 35 | + |
| 36 | + grads_and_vars = list(zip([grads0, grads1], [var0, var1])) |
| 37 | + |
| 38 | + opt = MovingAverage( |
| 39 | + tf.keras.optimizers.SGD(lr=2.0), |
| 40 | + average_decay=0.5, |
| 41 | + sequential_update=sequential_update) |
| 42 | + |
| 43 | + if not tf.executing_eagerly(): |
| 44 | + update = opt.apply_gradients(grads_and_vars) |
| 45 | + self.evaluate(tf.compat.v1.global_variables_initializer()) |
| 46 | + self.evaluate(update) |
| 47 | + self.evaluate(update) |
| 48 | + else: |
| 49 | + opt.apply_gradients(grads_and_vars) |
| 50 | + opt.apply_gradients(grads_and_vars) |
| 51 | + |
| 52 | + self.assertAllClose(var0.read_value(), [0.6, 1.6]) |
| 53 | + self.assertAllClose(var1.read_value(), [2.96, 3.96]) |
| 54 | + |
| 55 | + ema_var0 = opt._ema.average(var0) # pylint: disable=protected-access |
| 56 | + ema_var1 = opt._ema.average(var1) # pylint: disable=protected-access |
| 57 | + |
| 58 | + if sequential_update: |
| 59 | + self.assertAllClose(ema_var0.read_value(), [0.75, 1.75]) |
| 60 | + self.assertAllClose(ema_var1.read_value(), [2.975, 3.975]) |
| 61 | + |
| 62 | + assign = opt.assign_average_vars([var0, var1]) |
| 63 | + self.evaluate(assign) |
| 64 | + |
| 65 | + if sequential_update: |
| 66 | + self.assertAllClose(var0.read_value(), [0.75, 1.75]) |
| 67 | + self.assertAllClose(var1.read_value(), [2.975, 3.975]) |
| 68 | + |
| 69 | + perturb = tf.group([ |
| 70 | + var0.assign_add([1.0, 1.0]), |
| 71 | + var1.assign_add([2.0, 2.0]), |
| 72 | + ema_var0.assign_add([3.0, 3.0]), |
| 73 | + ema_var1.assign_add([4.0, 4.0]) |
| 74 | + ]) |
| 75 | + self.evaluate(perturb) |
| 76 | + |
| 77 | + if sequential_update: |
| 78 | + self.assertAllClose(var0.read_value(), [1.75, 2.75]) |
| 79 | + self.assertAllClose(var1.read_value(), [4.975, 5.975]) |
| 80 | + self.assertAllClose(ema_var0.read_value(), [3.75, 4.75]) |
| 81 | + self.assertAllClose(ema_var1.read_value(), [6.975, 7.975]) |
| 82 | + |
| 83 | + @test_utils.run_in_graph_and_eager_modes |
| 84 | + def test_opt_failure(self): |
| 85 | + base_opt = None |
| 86 | + for sequential_update in [True, False]: |
| 87 | + with self.assertRaises(TypeError): |
| 88 | + MovingAverage(base_opt, 0.5, sequential_update) |
| 89 | + |
| 90 | + @test_utils.run_in_graph_and_eager_modes |
| 91 | + def test_model_weights_update(self): |
| 92 | + grad = tf.Variable([[0.1]]) |
| 93 | + model = tf.keras.Sequential([ |
| 94 | + tf.keras.layers.Dense( |
| 95 | + 1, |
| 96 | + kernel_initializer=tf.keras.initializers.Constant([[1.0]]), |
| 97 | + use_bias=False) |
| 98 | + ]) |
| 99 | + model.build(input_shape=[1, 1]) |
| 100 | + |
| 101 | + opt = MovingAverage(tf.keras.optimizers.SGD(lr=2.0), 0.5) |
| 102 | + update = opt.apply_gradients(list(zip([grad], model.variables))) |
| 103 | + |
| 104 | + self.evaluate(tf.compat.v1.global_variables_initializer()) |
| 105 | + self.evaluate(update) |
| 106 | + self.assertAllClose(model.variables[0].read_value(), [[0.8]]) |
| 107 | + |
| 108 | + mean_update = opt.assign_average_vars(model.variables) |
| 109 | + self.evaluate(mean_update) |
| 110 | + self.assertAllClose(model.variables[0].read_value(), [[0.9]]) |
| 111 | + |
| 112 | + @test_utils.run_in_graph_and_eager_modes |
| 113 | + def test_config(self): |
| 114 | + sgd_opt = tf.keras.optimizers.SGD( |
| 115 | + lr=2.0, nesterov=True, momentum=0.3, decay=0.1) |
| 116 | + opt = MovingAverage( |
| 117 | + sgd_opt, |
| 118 | + average_decay=0.5, |
| 119 | + num_updates=100, |
| 120 | + sequential_update=False) |
| 121 | + config = opt.get_config() |
| 122 | + |
| 123 | + self.assertEqual(config['average_decay'], 0.5) |
| 124 | + self.assertEqual(config['decay'], 0.1) |
| 125 | + self.assertEqual(config['learning_rate'], 2.0) |
| 126 | + self.assertEqual(config['momentum'], 0.3) |
| 127 | + self.assertEqual(config['name'], 'SGD') |
| 128 | + self.assertEqual(config['nesterov'], True) |
| 129 | + self.assertEqual(config['num_updates'], 100) |
| 130 | + self.assertEqual(config['sequential_update'], False) |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == '__main__': |
| 134 | + tf.test.main() |
0 commit comments