|
| 1 | +# Copyright 2015 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 Nadam.""" |
| 16 | + |
| 17 | +from __future__ import absolute_import |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +import numpy as np |
| 22 | + |
| 23 | +from tensorflow.python.client import session |
| 24 | +from tensorflow.python.framework import constant_op |
| 25 | +from tensorflow.python.framework import dtypes |
| 26 | +from tensorflow.python.framework import ops |
| 27 | +from tensorflow.python.ops import array_ops |
| 28 | +from tensorflow.python.ops import math_ops |
| 29 | +from tensorflow.python.ops import resource_variable_ops |
| 30 | +from tensorflow.python.ops import variables |
| 31 | +from tensorflow.python.platform import test |
| 32 | +from tensorflow.contrib.opt.python.training import nadam_optimizer |
| 33 | + |
| 34 | + |
| 35 | +def nadam_update_numpy(param, |
| 36 | + g_t, |
| 37 | + t, |
| 38 | + m, |
| 39 | + v, |
| 40 | + alpha=0.001, |
| 41 | + beta1=0.9, |
| 42 | + beta2=0.999, |
| 43 | + epsilon=1e-8): |
| 44 | + alpha_t = alpha * np.sqrt(1 - beta2**t) / (1 - beta1**t) |
| 45 | + |
| 46 | + m_t = beta1 * m + (1 - beta1) * g_t |
| 47 | + v_t = beta2 * v + (1 - beta2) * g_t * g_t |
| 48 | + |
| 49 | + m_bar = (1 - beta1) * g_t + beta1 * m_t |
| 50 | + |
| 51 | + param_t = param - alpha_t * m_bar / (np.sqrt(v_t) + epsilon) |
| 52 | + return param_t, m_t, v_t |
| 53 | + |
| 54 | + |
| 55 | +class NadamOptimizerTest(test.TestCase): |
| 56 | + |
| 57 | + def doTestSparse(self, use_resource=False): |
| 58 | + for dtype in [dtypes.half, dtypes.float32, dtypes.float64]: |
| 59 | + with self.test_session(): |
| 60 | + # Initialize variables for numpy implementation. |
| 61 | + m0, v0, m1, v1 = 0.0, 0.0, 0.0, 0.0 |
| 62 | + var0_np = np.array([1.0, 2.0], dtype=dtype.as_numpy_dtype) |
| 63 | + grads0_np = np.array([0.1, 0.1], dtype=dtype.as_numpy_dtype) |
| 64 | + var1_np = np.array([3.0, 4.0], dtype=dtype.as_numpy_dtype) |
| 65 | + grads1_np = np.array([0.01, 0.01], dtype=dtype.as_numpy_dtype) |
| 66 | + |
| 67 | + if use_resource: |
| 68 | + var0 = resource_variable_ops.ResourceVariable(var0_np) |
| 69 | + var1 = resource_variable_ops.ResourceVariable(var1_np) |
| 70 | + else: |
| 71 | + var0 = variables.Variable(var0_np) |
| 72 | + var1 = variables.Variable(var1_np) |
| 73 | + grads0_np_indices = np.array([0, 1], dtype=np.int32) |
| 74 | + grads0 = ops.IndexedSlices( |
| 75 | + constant_op.constant(grads0_np), |
| 76 | + constant_op.constant(grads0_np_indices), constant_op.constant([2])) |
| 77 | + grads1_np_indices = np.array([0, 1], dtype=np.int32) |
| 78 | + grads1 = ops.IndexedSlices( |
| 79 | + constant_op.constant(grads1_np), |
| 80 | + constant_op.constant(grads1_np_indices), constant_op.constant([2])) |
| 81 | + opt = nadam_optimizer.NadamOptimizer() |
| 82 | + update = opt.apply_gradients(zip([grads0, grads1], [var0, var1])) |
| 83 | + variables.global_variables_initializer().run() |
| 84 | + |
| 85 | + # Fetch params to validate initial values |
| 86 | + self.assertAllClose([1.0, 2.0], var0.eval()) |
| 87 | + self.assertAllClose([3.0, 4.0], var1.eval()) |
| 88 | + |
| 89 | + beta1_power, beta2_power = opt._get_beta_accumulators() |
| 90 | + |
| 91 | + # Run 3 steps of Nadam |
| 92 | + for t in range(1, 4): |
| 93 | + self.assertAllCloseAccordingToType(0.9**t, beta1_power.eval()) |
| 94 | + self.assertAllCloseAccordingToType(0.999**t, beta2_power.eval()) |
| 95 | + update.run() |
| 96 | + |
| 97 | + var0_np, m0, v0 = nadam_update_numpy(var0_np, grads0_np, t, m0, v0) |
| 98 | + var1_np, m1, v1 = nadam_update_numpy(var1_np, grads1_np, t, m1, v1) |
| 99 | + |
| 100 | + # Validate updated params |
| 101 | + self.assertAllCloseAccordingToType(var0_np, var0.eval()) |
| 102 | + self.assertAllCloseAccordingToType(var1_np, var1.eval()) |
| 103 | + |
| 104 | + def testSparse(self): |
| 105 | + self.doTestSparse(use_resource=False) |
| 106 | + |
| 107 | + def testResourceSparse(self): |
| 108 | + self.doTestSparse(use_resource=True) |
| 109 | + |
| 110 | + def doTestBasic(self, use_resource=False): |
| 111 | + for dtype in [dtypes.half, dtypes.float32, dtypes.float64]: |
| 112 | + with self.test_session(): |
| 113 | + # Initialize variables for numpy implementation. |
| 114 | + m0, v0, m1, v1 = 0.0, 0.0, 0.0, 0.0 |
| 115 | + var0_np = np.array([1.0, 2.0], dtype=dtype.as_numpy_dtype) |
| 116 | + grads0_np = np.array([0.1, 0.1], dtype=dtype.as_numpy_dtype) |
| 117 | + var1_np = np.array([3.0, 4.0], dtype=dtype.as_numpy_dtype) |
| 118 | + grads1_np = np.array([0.01, 0.01], dtype=dtype.as_numpy_dtype) |
| 119 | + |
| 120 | + if use_resource: |
| 121 | + var0 = resource_variable_ops.ResourceVariable(var0_np) |
| 122 | + var1 = resource_variable_ops.ResourceVariable(var1_np) |
| 123 | + else: |
| 124 | + var0 = variables.Variable(var0_np) |
| 125 | + var1 = variables.Variable(var1_np) |
| 126 | + grads0 = constant_op.constant(grads0_np) |
| 127 | + grads1 = constant_op.constant(grads1_np) |
| 128 | + opt = nadam_optimizer.NadamOptimizer() |
| 129 | + update = opt.apply_gradients(zip([grads0, grads1], [var0, var1])) |
| 130 | + variables.global_variables_initializer().run() |
| 131 | + |
| 132 | + # Fetch params to validate initial values |
| 133 | + self.assertAllClose([1.0, 2.0], var0.eval()) |
| 134 | + self.assertAllClose([3.0, 4.0], var1.eval()) |
| 135 | + |
| 136 | + beta1_power, beta2_power = opt._get_beta_accumulators() |
| 137 | + |
| 138 | + # Run 3 steps of Nadam |
| 139 | + for t in range(1, 4): |
| 140 | + self.assertAllCloseAccordingToType(0.9**t, beta1_power.eval()) |
| 141 | + self.assertAllCloseAccordingToType(0.999**t, beta2_power.eval()) |
| 142 | + update.run() |
| 143 | + |
| 144 | + var0_np, m0, v0 = nadam_update_numpy(var0_np, grads0_np, t, m0, v0) |
| 145 | + var1_np, m1, v1 = nadam_update_numpy(var1_np, grads1_np, t, m1, v1) |
| 146 | + |
| 147 | + # Validate updated params |
| 148 | + self.assertAllCloseAccordingToType(var0_np, var0.eval()) |
| 149 | + self.assertAllCloseAccordingToType(var1_np, var1.eval()) |
| 150 | + |
| 151 | + def testBasic(self): |
| 152 | + self.doTestBasic(use_resource=False) |
| 153 | + |
| 154 | + def testResourceBasic(self): |
| 155 | + self.doTestBasic(use_resource=True) |
| 156 | + |
| 157 | +if __name__ == "__main__": |
| 158 | + test.main() |
0 commit comments