@test_util.run_in_graph_and_eager_modes
def testSparseWithAmsgrad(self):
    for dtype in [dtypes.float32, dtypes.float64]:
        with self.cached_session():
            m0 = np.array([[0.0], [0.0]])
            v0 = np.array([[0.0], [0.0]])
            v0hat = np.array([[0.0], [0.0]])
            indices_np = np.array([1])
            indices = constant_op.constant(indices_np, dtype=dtypes.int32)
            var0_np = np.array([[1.0], [2.0]], dtype=dtype.as_numpy_dtype)
            repeated_index_update_var = variables.Variable(var0_np, dtype=dtype)
            aggregated_update_var = variables.Variable(var0_np, dtype=dtype)
            grads0_np = np.array([[0.2]], dtype=dtype.as_numpy_dtype)
            grad_repeated_index = ops.IndexedSlices(constant_op.constant([0.1, 0.1], shape=[2, 1], dtype=dtype), constant_op.constant([1, 1]), constant_op.constant([2, 1]))
            grad_aggregated = ops.IndexedSlices(grads0_np, indices, constant_op.constant([2, 1]))
            opt_repeated = adam.Adam(amsgrad=True)
            opt_aggregated = adam.Adam(amsgrad=True)
            if (not context.executing_eagerly()):
                repeated_update = opt_repeated.apply_gradients([(grad_repeated_index, repeated_index_update_var)])
                aggregated_update = opt_aggregated.apply_gradients([(grad_aggregated, aggregated_update_var)])
            self.evaluate(variables.global_variables_initializer())
            self.assertAllClose(self.evaluate(aggregated_update_var), self.evaluate(repeated_index_update_var))
            for t in range(3):
                if (not context.executing_eagerly()):
                    self.evaluate(repeated_update)
                    self.evaluate(aggregated_update)
                else:
                    opt_repeated.apply_gradients([(grad_repeated_index, repeated_index_update_var)])
                    opt_aggregated.apply_gradients([(grad_aggregated, aggregated_update_var)])
                (var0_np, m0, v0, v0hat) = adam_sparse_update_numpy_amsgrad(var0_np, indices_np, grads0_np, t, m0, v0, v0hat)
                self.assertAllCloseAccordingToType(var0_np, self.evaluate(aggregated_update_var))
                self.assertAllCloseAccordingToType(self.evaluate(aggregated_update_var), self.evaluate(repeated_index_update_var))
@test_util.run_in_graph_and_eager_modes
def testSparseWithAmsgrad(self):
    for dtype in [dtypes.float32, dtypes.float64]:
        with self.cached_session():
            m0 = np.array([[0.0], [0.0]])
            v0 = np.array([[0.0], [0.0]])
            v0hat = np.array([[0.0], [0.0]])
            indices_np = np.array([1])
            indices = constant_op.constant(indices_np, dtype=dtypes.int32)
            var0_np = np.array([[1.0], [2.0]], dtype=dtype.as_numpy_dtype)
            repeated_index_update_var = variables.Variable(var0_np, dtype=dtype)
            aggregated_update_var = variables.Variable(var0_np, dtype=dtype)
            grads0_np = np.array([[0.2]], dtype=dtype.as_numpy_dtype)
            grad_repeated_index = ops.IndexedSlices(constant_op.constant([0.1, 0.1], shape=[2, 1], dtype=dtype), constant_op.constant([1, 1]), constant_op.constant([2, 1]))
            grad_aggregated = ops.IndexedSlices(grads0_np, indices, constant_op.constant([2, 1]))
            opt_repeated = adam.Adam(amsgrad=True)
            opt_aggregated = adam.Adam(amsgrad=True)
            if (not context.executing_eagerly()):
                repeated_update = opt_repeated.apply_gradients([(grad_repeated_index, repeated_index_update_var)])
                aggregated_update = opt_aggregated.apply_gradients([(grad_aggregated, aggregated_update_var)])
            self.evaluate(variables.global_variables_initializer())
            self.assertAllClose(self.evaluate(aggregated_update_var), self.evaluate(repeated_index_update_var))
            for t in range(3):
                if (not context.executing_eagerly()):
                    self.evaluate(repeated_update)
                    self.evaluate(aggregated_update)
                else:
                    opt_repeated.apply_gradients([(grad_repeated_index, repeated_index_update_var)])
                    opt_aggregated.apply_gradients([(grad_aggregated, aggregated_update_var)])
                (var0_np, m0, v0, v0hat) = adam_sparse_update_numpy_amsgrad(var0_np, indices_np, grads0_np, t, m0, v0, v0hat)
                self.assertAllCloseAccordingToType(var0_np, self.evaluate(aggregated_update_var))
                self.assertAllCloseAccordingToType(self.evaluate(aggregated_update_var), self.evaluate(repeated_index_update_var))