2121import numpy as np
2222import tensorflow as tf
2323
24- from tensorflow .python .eager import context
25- from tensorflow .python .ops import variables
2624from tensorflow_addons .optimizers import lazy_adam
2725from tensorflow_addons .utils import test_utils
2826
@@ -83,7 +81,7 @@ def testSparse(self):
8381 opt = lazy_adam .LazyAdam ()
8482 update = opt .apply_gradients (
8583 zip ([grads0 , grads1 ], [var0 , var1 ]))
86- self .evaluate (variables .global_variables_initializer ())
84+ self .evaluate (tf . compat . v1 .global_variables_initializer ())
8785
8886 # Fetch params to validate initial values
8987 self .assertAllClose ([1.0 , 1.0 , 2.0 ], self .evaluate (var0 ))
@@ -120,7 +118,7 @@ def testSparseDevicePlacement(self):
120118 g_sum = lambda : tf .math .reduce_sum (tf .gather (var , indices )) # pylint: disable=cell-var-from-loop
121119 optimizer = lazy_adam .LazyAdam (3.0 )
122120 minimize_op = optimizer .minimize (g_sum , var_list = [var ])
123- self .evaluate (variables .global_variables_initializer ())
121+ self .evaluate (tf . compat . v1 .global_variables_initializer ())
124122 self .evaluate (minimize_op )
125123
126124 @test_utils .run_deprecated_v1
@@ -143,7 +141,7 @@ def testSparseRepeatedIndices(self):
143141 aggregated_update_opt = lazy_adam .LazyAdam ()
144142 aggregated_update = aggregated_update_opt .apply_gradients (
145143 [(grad_aggregated , aggregated_update_var )])
146- self .evaluate (variables .global_variables_initializer ())
144+ self .evaluate (tf . compat . v1 .global_variables_initializer ())
147145 self .assertAllClose (aggregated_update_var .eval (),
148146 repeated_index_update_var .eval ())
149147 for _ in range (3 ):
@@ -182,10 +180,10 @@ def doTestBasic(self, use_callable_params=False):
182180 epsilon = epsilon ()
183181
184182 opt = lazy_adam .LazyAdam (learning_rate = learning_rate )
185- if not context .executing_eagerly ():
183+ if not tf .executing_eagerly ():
186184 update = opt .apply_gradients (
187185 zip ([grads0 , grads1 ], [var0 , var1 ]))
188- self .evaluate (variables .global_variables_initializer ())
186+ self .evaluate (tf . compat . v1 .global_variables_initializer ())
189187 # Fetch params to validate initial values
190188 self .assertAllClose ([1.0 , 2.0 ], self .evaluate (var0 ))
191189 self .assertAllClose ([3.0 , 4.0 ], self .evaluate (var1 ))
@@ -198,7 +196,7 @@ def doTestBasic(self, use_callable_params=False):
198196 0.9 ** (t + 1 ), self .evaluate (beta_1_power ))
199197 self .assertAllCloseAccordingToType (
200198 0.999 ** (t + 1 ), self .evaluate (beta_2_power ))
201- if not context .executing_eagerly ():
199+ if not tf .executing_eagerly ():
202200 self .evaluate (update )
203201 else :
204202 opt .apply_gradients (
@@ -222,8 +220,7 @@ def testResourceBasic(self):
222220 self .doTestBasic ()
223221
224222 def testBasicCallableParams (self ):
225- with context .eager_mode ():
226- self .doTestBasic (use_callable_params = True )
223+ self .doTestBasic (use_callable_params = True )
227224
228225 @test_utils .run_deprecated_v1
229226 def testTensorLearningRate (self ):
@@ -243,7 +240,7 @@ def testTensorLearningRate(self):
243240 opt = lazy_adam .LazyAdam (tf .constant (0.001 ))
244241 update = opt .apply_gradients (
245242 zip ([grads0 , grads1 ], [var0 , var1 ]))
246- self .evaluate (variables .global_variables_initializer ())
243+ self .evaluate (tf . compat . v1 .global_variables_initializer ())
247244
248245 # Fetch params to validate initial values
249246 self .assertAllClose ([1.0 , 2.0 ], var0 .eval ())
@@ -289,7 +286,7 @@ def testSharing(self):
289286 zip ([grads0 , grads1 ], [var0 , var1 ]))
290287 update2 = opt .apply_gradients (
291288 zip ([grads0 , grads1 ], [var0 , var1 ]))
292- self .evaluate (variables .global_variables_initializer ())
289+ self .evaluate (tf . compat . v1 .global_variables_initializer ())
293290
294291 beta_1_power , beta_2_power = get_beta_accumulators (opt , dtype )
295292
@@ -320,16 +317,14 @@ def testSharing(self):
320317 self .evaluate (var1 ))
321318
322319 def testSlotsUniqueEager (self ):
323- with context .eager_mode ():
324- v1 = tf .Variable (1. )
325- v2 = tf .Variable (1. )
326- opt = lazy_adam .LazyAdam (1. )
327- opt .minimize (lambda : v1 + v2 , var_list = [v1 , v2 ])
328- # There should be iteration, and two unique slot variables for v1 and v2.
329- self .assertEqual (5 , len (set (opt .variables ())))
330- self .assertEqual (
331- self .evaluate (opt .variables ()[0 ]),
332- self .evaluate (opt .iterations ))
320+ v1 = tf .Variable (1. )
321+ v2 = tf .Variable (1. )
322+ opt = lazy_adam .LazyAdam (1. )
323+ opt .minimize (lambda : v1 + v2 , var_list = [v1 , v2 ])
324+ # There should be iteration, and two unique slot variables for v1 and v2.
325+ self .assertEqual (5 , len (set (opt .variables ())))
326+ self .assertEqual (
327+ self .evaluate (opt .variables ()[0 ]), self .evaluate (opt .iterations ))
333328
334329
335330if __name__ == "__main__" :
0 commit comments