Пример #1
0
    def testConstructNAdamWithLR(self):
        opt = nadam.Nadam(lr=1.0)
        opt_2 = nadam.Nadam(learning_rate=0.1, lr=1.0)
        opt_3 = nadam.Nadam(learning_rate=0.1)
        self.assertIsInstance(opt.lr, tf.Variable)
        self.assertIsInstance(opt_2.lr, tf.Variable)
        self.assertIsInstance(opt_3.lr, tf.Variable)

        self.evaluate(tf.compat.v1.global_variables_initializer())
        self.assertAllClose(self.evaluate(opt.lr), (1.0))
        self.assertAllClose(self.evaluate(opt_2.lr), (1.0))
        self.assertAllClose(self.evaluate(opt_3.lr), (0.1))
Пример #2
0
    def testBasic(self):
        # TODO(tanzheny, omalleyt): Fix test in eager mode.
        for dtype in [tf.half, tf.float32, tf.float64]:
            with tf.Graph().as_default(), self.cached_session():
                # Initialize variables for numpy implementation.
                m0, v0, m1, v1, mcache = 0.0, 0.0, 0.0, 0.0, 1.0
                var0_np = np.array([1.0, 2.0], dtype=dtype.as_numpy_dtype)
                grads0_np = np.array([0.1, 0.1], dtype=dtype.as_numpy_dtype)
                var1_np = np.array([3.0, 4.0], dtype=dtype.as_numpy_dtype)
                grads1_np = np.array([0.01, 0.01], dtype=dtype.as_numpy_dtype)

                var0 = tf.Variable(var0_np)
                var1 = tf.Variable(var1_np)
                grads0 = tf.constant(grads0_np)
                grads1 = tf.constant(grads1_np)
                opt = nadam.Nadam()
                update = opt.apply_gradients(
                    zip([grads0, grads1], [var0, var1])
                )
                self.evaluate(tf.compat.v1.global_variables_initializer())

                # Fetch params to validate initial values
                self.assertAllClose([1.0, 2.0], var0)
                self.assertAllClose([3.0, 4.0], var1)

                # Run 3 steps of Nadam
                for t in range(3):
                    update.run()

                    mcache = update_m_cache(mcache, t)
                    var0_np, m0, v0 = nadam_update_numpy(
                        var0_np, grads0_np, t, m0, v0, mcache
                    )
                    var1_np, m1, v1 = nadam_update_numpy(
                        var1_np, grads1_np, t, m1, v1, mcache
                    )

                    # Validate updated params
                    self.assertAllCloseAccordingToType(var0_np, var0)
                    self.assertAllCloseAccordingToType(var1_np, var1)
Пример #3
0
    def testSparse(self):
        # TODO(tanzheny, omalleyt): Fix test in eager mode.
        sparse_epsilon = 1e-7
        for dtype in [tf.half, tf.float32, tf.float64]:
            with tf.Graph().as_default(), self.cached_session():
                # Initialize variables for numpy implementation.
                m0, v0, m1, v1, mcache = 0.0, 0.0, 0.0, 0.0, 1.0
                var0_np = np.array([1.0, 1.0, 2.0], dtype=dtype.as_numpy_dtype)
                grads0_np = np.array([0.1, 0, 0.1], dtype=dtype.as_numpy_dtype)
                var1_np = np.array([3.0, 3.0, 4.0], dtype=dtype.as_numpy_dtype)
                grads1_np = np.array([0.01, 0, 0.01],
                                     dtype=dtype.as_numpy_dtype)

                var0 = tf.Variable(var0_np)
                var1 = tf.Variable(var1_np)
                grads0_np_indices = np.array([0, 2], dtype=np.int32)
                grads0 = tf.IndexedSlices(
                    tf.constant(grads0_np[grads0_np_indices]),
                    tf.constant(grads0_np_indices), tf.constant([3]))
                grads1_np_indices = np.array([0, 2], dtype=np.int32)
                grads1 = tf.IndexedSlices(
                    tf.constant(grads1_np[grads1_np_indices]),
                    tf.constant(grads1_np_indices), tf.constant([3]))
                opt = nadam.Nadam(epsilon=sparse_epsilon)
                update = opt.apply_gradients(
                    zip([grads0, grads1], [var0, var1]))
                self.evaluate(tf.compat.v1.global_variables_initializer())

                # Fetch params to validate initial values
                self.assertAllClose([1.0, 1.0, 2.0], var0)
                self.assertAllClose([3.0, 3.0, 4.0], var1)

                beta1_power, beta2_power = get_beta_accumulators(opt, dtype)

                # Run 3 steps of Nadam
                for t in range(3):
                    self.assertAllCloseAccordingToType(0.9**(t + 1),
                                                       beta1_power)
                    self.assertAllCloseAccordingToType(0.999**(t + 1),
                                                       beta2_power)
                    update.run()

                    mcache = update_m_cache(mcache, t)
                    var0_np, m0, v0 = nadam_update_numpy(
                        var0_np,
                        grads0_np,
                        t,
                        m0,
                        v0,
                        mcache,
                        epsilon=sparse_epsilon)
                    var1_np, m1, v1 = nadam_update_numpy(
                        var1_np,
                        grads1_np,
                        t,
                        m1,
                        v1,
                        mcache,
                        epsilon=sparse_epsilon)

                    # Validate updated params
                    self.assertAllCloseAccordingToType(var0_np, var0)
                    self.assertAllCloseAccordingToType(var1_np, var1)
Пример #4
0
 def testConstructNAdamWithScheduleDecay(self):
     opt = nadam.Nadam(schedule_decay=0.2)
     self.assertIsInstance(opt.decay, tf.Variable)
     self.evaluate(tf.compat.v1.global_variables_initializer())
     self.assertAllClose(self.evaluate(opt.decay), (0.2))
Пример #5
0
    "AdadeltaKerasV2", lambda: adadelta_keras_v2.Adadelta(0.001)
)
adagrad_optimizer_keras_v2_fn = tf.__internal__.test.combinations.NamedObject(
    "AdagradKerasV2", lambda: adagrad_keras_v2.Adagrad(0.001)
)
adam_optimizer_keras_v2_fn = tf.__internal__.test.combinations.NamedObject(
    "AdamKerasV2", lambda: adam_keras_v2.Adam(0.001, epsilon=1.0)
)
adam_experimental_fn = tf.__internal__.test.combinations.NamedObject(
    "AdamExperimental", lambda: adam_experimental.Adam(0.001)
)
adamax_optimizer_keras_v2_fn = tf.__internal__.test.combinations.NamedObject(
    "AdamaxKerasV2", lambda: adamax_keras_v2.Adamax(0.001, epsilon=1.0)
)
nadam_optimizer_keras_v2_fn = tf.__internal__.test.combinations.NamedObject(
    "NadamKerasV2", lambda: nadam_keras_v2.Nadam(0.001, epsilon=1.0)
)
ftrl_optimizer_keras_v2_fn = tf.__internal__.test.combinations.NamedObject(
    "FtrlKerasV2", lambda: ftrl_keras_v2.Ftrl(0.001)
)
gradient_descent_optimizer_keras_v2_fn = (
    tf.__internal__.test.combinations.NamedObject(
        "GradientDescentKerasV2", lambda: gradient_descent_keras_v2.SGD(0.001)
    )
)
rmsprop_optimizer_keras_v2_fn = tf.__internal__.test.combinations.NamedObject(
    "RmsPropKerasV2", lambda: rmsprop_keras_v2.RMSprop(0.001)
)

# TODO(shiningsun): consider adding the other v2 optimizers
optimizers_v2 = [
Пример #6
0
 def testNadamCompatibility(self):
   opt_v1 = optimizer_v1.Nadam(lr=0.001)
   opt_v2 = nadam.Nadam(learning_rate=0.001)
   self._testOptimizersCompatibility(opt_v1, opt_v2)