# Path: tf_slim/layers/optimizers.py
# OPTIMIZER_CLS_NAMES = {
#     "Adagrad": train.AdagradOptimizer,
#     "Adam": train.AdamOptimizer,
#     "Ftrl": train.FtrlOptimizer,
#     "Momentum": lambda learning_rate: train.MomentumOptimizer(learning_rate, momentum=0.9),  # pylint: disable=line-too-long
#     "RMSProp": train.RMSPropOptimizer,
#     "SGD": train.GradientDescentOptimizer,
# }
# OPTIMIZER_SUMMARIES = [
#     "learning_rate",
#     "loss",
#     "gradients",
#     "gradient_norm",
#     "global_gradient_norm",
# ]
# def optimize_loss(loss,
#                   global_step,
#                   learning_rate,
#                   optimizer,
#                   gradient_noise_scale=None,
#                   gradient_multipliers=None,
#                   clip_gradients=None,
#                   learning_rate_decay_fn=None,
#                   update_ops=None,
#                   variables=None,
#                   name=None,
#                   summaries=None,
#                   colocate_gradients_with_ops=False,
#                   increment_global_step=True):
# def _clip_gradients_by_norm(grads_and_vars, clip_gradients):
# def _adaptive_max_norm(norm, std_factor, decay, global_step, epsilon, name):
#     def moving_average(name, value, decay):
# def adaptive_clipping_fn(std_factor=2.,
#                          decay=0.95,
#                          static_max_norm=None,
#                          global_step=None,
#                          report_summary=False,
#                          epsilon=1e-8,
#                          name=None):
#   def gradient_clipping(grads_and_vars):
# def _add_scaled_noise_to_gradients(grads_and_vars, gradient_noise_scale):
# def _multiply_gradients(grads_and_vars, gradient_multipliers):


import numpy as np
import tensorflow.compat.v1 as tf
from tf_slim.layers import optimizers as optimizers_lib
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import random_seed
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import init_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import state_ops
from tensorflow.python.ops import variable_scope
from tensorflow.python.ops import variables
from tensorflow.python.platform import test
from tensorflow.python.training import gradient_descent


def _setup_model():
  x = array_ops.placeholder(dtypes.float32, [])
  var = variable_scope.get_variable(
      "test", [], initializer=init_ops.constant_initializer(10))
  loss = math_ops.abs(var * x)
  global_step = tf.train.create_global_step()
  return x, var, loss, global_step


def _no_op_learning_rate_decay_fn(lr, global_step):
  assert lr is not None
  assert global_step is not None
  return lr


class OptimizersTest(test.TestCase):

  def testSGDOptimizer(self):
    optimizers = [
        "SGD", gradient_descent.GradientDescentOptimizer,
        gradient_descent.GradientDescentOptimizer(learning_rate=0.1),
        lambda lr: gradient_descent.GradientDescentOptimizer(learning_rate=lr),
        "Momentum"
    ]
    for optimizer in optimizers:
      with ops.Graph().as_default() as g:
        with self.session(graph=g) as session:
          x, var, loss, global_step = _setup_model()

# Path: tf_slim/layers/optimizers.py
# OPTIMIZER_CLS_NAMES = {
#     "Adagrad": train.AdagradOptimizer,
#     "Adam": train.AdamOptimizer,
#     "Ftrl": train.FtrlOptimizer,
#     "Momentum": lambda learning_rate: train.MomentumOptimizer(learning_rate, momentum=0.9),  # pylint: disable=line-too-long
#     "RMSProp": train.RMSPropOptimizer,
#     "SGD": train.GradientDescentOptimizer,
# }
# OPTIMIZER_SUMMARIES = [
#     "learning_rate",
#     "loss",
#     "gradients",
#     "gradient_norm",
#     "global_gradient_norm",
# ]
# def optimize_loss(loss,
#                   global_step,
#                   learning_rate,
#                   optimizer,
#                   gradient_noise_scale=None,
#                   gradient_multipliers=None,
#                   clip_gradients=None,
#                   learning_rate_decay_fn=None,
#                   update_ops=None,
#                   variables=None,
#                   name=None,
#                   summaries=None,
#                   colocate_gradients_with_ops=False,
#                   increment_global_step=True):
# def _clip_gradients_by_norm(grads_and_vars, clip_gradients):
# def _adaptive_max_norm(norm, std_factor, decay, global_step, epsilon, name):
#     def moving_average(name, value, decay):
# def adaptive_clipping_fn(std_factor=2.,
#                          decay=0.95,
#                          static_max_norm=None,
#                          global_step=None,
#                          report_summary=False,
#                          epsilon=1e-8,
#                          name=None):
#   def gradient_clipping(grads_and_vars):
# def _add_scaled_noise_to_gradients(grads_and_vars, gradient_noise_scale):
# def _multiply_gradients(grads_and_vars, gradient_multipliers):


# Path: tf_slim/layers/optimizers_test.py
import numpy as np
import tensorflow.compat.v1 as tf
from tf_slim.layers import optimizers as optimizers_lib
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.framework import random_seed
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import init_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import state_ops
from tensorflow.python.ops import variable_scope
from tensorflow.python.ops import variables
from tensorflow.python.platform import test
from tensorflow.python.training import gradient_descent



def _setup_model():
  x = array_ops.placeholder(dtypes.float32, [])
  var = variable_scope.get_variable(
      "test", [], initializer=init_ops.constant_initializer(10))
  loss = math_ops.abs(var * x)
  global_step = tf.train.create_global_step()
  return x, var, loss, global_step


def _no_op_learning_rate_decay_fn(lr, global_step):
  assert lr is not None
  assert global_step is not None
  return lr


class OptimizersTest(test.TestCase):

  def testSGDOptimizer(self):
    optimizers = [
        "SGD", gradient_descent.GradientDescentOptimizer,
        gradient_descent.GradientDescentOptimizer(learning_rate=0.1),
        lambda lr: gradient_descent.GradientDescentOptimizer(learning_rate=lr),
        "Momentum"
    ]
    for optimizer in optimizers:
      with ops.Graph().as_default() as g:
        with self.session(graph=g) as session:
          x, var, loss, global_step = _setup_model()

          train = optimizers_lib.optimize_loss(