from tensorflow.python.ops import control_flow_ops

    def _create_slots(self, var_list):
        # Create slots for the first and second moments.
        for v in var_list:
            self._zeros_slot(v, "g", self._name)

    def _apply_dense(self, grad, var):
        lr_t = math_ops.cast(self._lr_t, var.dtype.base_dtype)

        g_t = grad
        g_t_1 = self.get_slot(var, "g")
        g_t = g_t_1.assign(g_t)

        var_update = state_ops.assign_sub(var,
                                          2. * lr_t * g_t - lr_t * g_t_1)  # Adam would be lr_t * g_t
        return control_flow_ops.group(*[var_update, g_t])

    def _apply_sparse(self, grad, var):
        raise NotImplementedError("Sparse gradient updates are not supported.")


class OptimisticAdamOptimizer(optimizer.Optimizer):
    def __init__(self, learning_rate=0.001, beta1=0.9, beta2=0.999,
                 epsilon=1e-8,
                 use_locking=False, name="Adamirror"):

        super(OptimisticAdamOptimizer, self).__init__(use_locking, name)
        self._lr = learning_rate
        self._beta1 = beta1
