|
| 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 six |
| 21 | + |
| 22 | +import tensorflow as tf |
| 23 | + |
| 24 | +import moving_average |
| 25 | +from tensorflow_addons.utils import test_utils |
| 26 | + |
| 27 | + |
| 28 | +class MovingAverageTest(tf.test.TestCase): |
| 29 | + @test_utils.run_deprecated_v1 |
| 30 | + def test_run(self): |
| 31 | + for seq_update in [True, False]: |
| 32 | + orig_var0 = [1.0, 2.0] |
| 33 | + orig_var1 = [3.0, 4.0] |
| 34 | + |
| 35 | + var0 = tf.Variable(orig_var0) |
| 36 | + var1 = tf.Variable(orig_var1) |
| 37 | + |
| 38 | + grads0 = tf.constant([0.1, 0.1]) |
| 39 | + grads1 = tf.constant([0.01, 0.01]) |
| 40 | + |
| 41 | + opt = moving_average.MovingAverage( |
| 42 | + tf.keras.optimizers.SGD(lr=2.0), |
| 43 | + average_decay=0.5, |
| 44 | + seq_update=seq_update) |
| 45 | + |
| 46 | + update = opt.apply_gradients( |
| 47 | + list(six.moves.zip([grads0, grads1], [var0, var1]))) |
| 48 | + |
| 49 | + ema_var0 = opt._ema.average(var0) # pylint: disable=protected-access |
| 50 | + ema_var1 = opt._ema.average(var1) # pylint: disable=protected-access |
| 51 | + |
| 52 | + self.evaluate(tf.compat.v1.global_variables_initializer()) |
| 53 | + self.evaluate(update) |
| 54 | + |
| 55 | + self.assertAllClose(var0.read_value(), [0.8, 1.8]) |
| 56 | + self.assertAllClose(var1.read_value(), [2.98, 3.98]) |
| 57 | + |
| 58 | + if seq_update: |
| 59 | + self.assertAllClose(ema_var0.read_value(), [0.9, 1.9]) |
| 60 | + self.assertAllClose(ema_var1.read_value(), [2.99, 3.99]) |
| 61 | + |
| 62 | + assign = opt.assign_average_vars([var0, var1]) |
| 63 | + self.evaluate(assign) |
| 64 | + |
| 65 | + if seq_update: |
| 66 | + self.assertAllClose(self.evaluate(var0), [0.9, 1.9]) |
| 67 | + self.assertAllClose(self.evaluate(var1), [2.99, 3.99]) |
| 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 seq_update: |
| 78 | + self.assertAllClose(self.evaluate(var0), [1.9, 2.9]) |
| 79 | + self.assertAllClose(self.evaluate(var1), [4.99, 5.99]) |
| 80 | + self.assertAllClose(self.evaluate(ema_var0), [3.9, 4.9]) |
| 81 | + self.assertAllClose(self.evaluate(ema_var1), [6.99, 7.99]) |
| 82 | + |
| 83 | + @test_utils.run_in_graph_and_eager_modes |
| 84 | + def test_opt_failure(self): |
| 85 | + base_opt = None |
| 86 | + for seq_update in [True, False]: |
| 87 | + with self.assertRaises(TypeError): |
| 88 | + moving_average.MovingAverage(base_opt, 0.5, seq_update) |
| 89 | + |
| 90 | + @test_utils.run_deprecated_v1 |
| 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 | + |
| 100 | + model.build(input_shape=[1, 1]) |
| 101 | + |
| 102 | + opt = moving_average.MovingAverage( |
| 103 | + tf.keras.optimizers.SGD(lr=2.0), 0.5) |
| 104 | + |
| 105 | + update = opt.apply_gradients( |
| 106 | + list(six.moves.zip([grad], model.variables))) |
| 107 | + |
| 108 | + self.evaluate(tf.compat.v1.global_variables_initializer()) |
| 109 | + self.evaluate(update) |
| 110 | + self.assertAllClose(model.variables[0].read_value(), [[0.8]]) |
| 111 | + |
| 112 | + mean_update = opt.assign_average_vars(model.variables) |
| 113 | + self.evaluate(mean_update) |
| 114 | + self.assertAllClose(model.variables[0].read_value(), [[0.9]]) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == '__main__': |
| 118 | + tf.test.main() |
0 commit comments