# Path: tf_slim/ops/arg_scope.py
# @tf_contextlib.contextmanager
# def arg_scope(list_ops_or_scope, **kwargs):
#   """Stores the default arguments for the given set of list_ops.
# 
#   For usage, please see examples at top of the file.
# 
#   Args:
#     list_ops_or_scope: List or tuple of operations to set argument scope for or
#       a dictionary containing the current scope. When list_ops_or_scope is a
#       dict, kwargs must be empty. When list_ops_or_scope is a list or tuple,
#       then every op in it need to be decorated with @add_arg_scope to work.
#     **kwargs: keyword=value that will define the defaults for each op in
#               list_ops. All the ops need to accept the given set of arguments.
# 
#   Yields:
#     the current_scope, which is a dictionary of {op: {arg: value}}
#   Raises:
#     TypeError: if list_ops is not a list or a tuple.
#     ValueError: if any op in list_ops has not be decorated with @add_arg_scope.
#   """
#   if isinstance(list_ops_or_scope, dict):
#     # Assumes that list_ops_or_scope is a scope that is being reused.
#     if kwargs:
#       raise ValueError('When attempting to re-use a scope by suppling a'
#                        'dictionary, kwargs must be empty.')
#     current_scope = list_ops_or_scope.copy()
#     try:
#       _get_arg_stack().append(current_scope)
#       yield current_scope
#     finally:
#       _get_arg_stack().pop()
#   else:
#     # Assumes that list_ops_or_scope is a list/tuple of ops with kwargs.
#     if not isinstance(list_ops_or_scope, (list, tuple)):
#       raise TypeError('list_ops_or_scope must either be a list/tuple or reused '
#                       'scope (i.e. dict)')
#     try:
#       current_scope = current_arg_scope().copy()
#       for op in list_ops_or_scope:
#         key = arg_scope_func_key(op)
#         if not has_arg_scope(op):
#           raise ValueError('%s is not decorated with @add_arg_scope',
#                            _name_op(op))
#         if key in current_scope:
#           current_kwargs = current_scope[key].copy()
#           current_kwargs.update(kwargs)
#           current_scope[key] = current_kwargs
#         else:
#           current_scope[key] = kwargs.copy()
#       _get_arg_stack().append(current_scope)
#       yield current_scope
#     finally:
#       _get_arg_stack().pop()


import numpy as np
import tensorflow.compat.v1 as tf
import tf_slim.losses as loss_ops
from tf_slim.ops.arg_scope import arg_scope
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 random_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 momentum as momentum_lib


class AddLossTest(test.TestCase):

  def testAddExternalLoss(self):
    logits = constant_op.constant([[1.2, 0.4, -1.0, -1.1]])
    labels = constant_op.constant([[1.0, 0.0, 0.0, 1.0]])
    losses = loss_ops.hinge_loss(logits, labels)
    self.assertFalse(loss_ops.get_losses())
    loss_ops.add_loss(math_ops.reduce_mean(losses))
    self.assertTrue(loss_ops.get_losses())
    total_loss = loss_ops.get_total_loss()
    with self.cached_session():
      self.assertAllClose(losses.eval(), [[0.0, 1.4, 0.0, 2.1]], atol=1e-3)
      self.assertAllClose(total_loss.eval(), 3.5 / 4.0, atol=1e-3)

  def testNoneLossCollection(self):
    logits = constant_op.constant([[1.2, 0.4, -1.0, -1.1]])
    labels = constant_op.constant([[1.0, 0.0, 0.0, 1.0]])
    losses = loss_ops.hinge_loss(logits, labels)
    self.assertFalse(loss_ops.get_losses())
    loss_ops.add_loss(math_ops.reduce_mean(losses), loss_collection=None)
    self.assertFalse(loss_ops.get_losses())
    with self.cached_session():
      self.assertAllClose(losses.eval(), [[0.0, 1.4, 0.0, 2.1]], atol=1e-3)

  def testNoCollectLosses(self):
    logits = constant_op.constant([[1.2, 0.4, -1.0, -1.1]])
    labels = constant_op.constant([[1.0, 0.0, 0.0, 1.0]])
    self.assertFalse(loss_ops.get_losses())

# Path: tf_slim/ops/arg_scope.py
# @tf_contextlib.contextmanager
# def arg_scope(list_ops_or_scope, **kwargs):
#   """Stores the default arguments for the given set of list_ops.
# 
#   For usage, please see examples at top of the file.
# 
#   Args:
#     list_ops_or_scope: List or tuple of operations to set argument scope for or
#       a dictionary containing the current scope. When list_ops_or_scope is a
#       dict, kwargs must be empty. When list_ops_or_scope is a list or tuple,
#       then every op in it need to be decorated with @add_arg_scope to work.
#     **kwargs: keyword=value that will define the defaults for each op in
#               list_ops. All the ops need to accept the given set of arguments.
# 
#   Yields:
#     the current_scope, which is a dictionary of {op: {arg: value}}
#   Raises:
#     TypeError: if list_ops is not a list or a tuple.
#     ValueError: if any op in list_ops has not be decorated with @add_arg_scope.
#   """
#   if isinstance(list_ops_or_scope, dict):
#     # Assumes that list_ops_or_scope is a scope that is being reused.
#     if kwargs:
#       raise ValueError('When attempting to re-use a scope by suppling a'
#                        'dictionary, kwargs must be empty.')
#     current_scope = list_ops_or_scope.copy()
#     try:
#       _get_arg_stack().append(current_scope)
#       yield current_scope
#     finally:
#       _get_arg_stack().pop()
#   else:
#     # Assumes that list_ops_or_scope is a list/tuple of ops with kwargs.
#     if not isinstance(list_ops_or_scope, (list, tuple)):
#       raise TypeError('list_ops_or_scope must either be a list/tuple or reused '
#                       'scope (i.e. dict)')
#     try:
#       current_scope = current_arg_scope().copy()
#       for op in list_ops_or_scope:
#         key = arg_scope_func_key(op)
#         if not has_arg_scope(op):
#           raise ValueError('%s is not decorated with @add_arg_scope',
#                            _name_op(op))
#         if key in current_scope:
#           current_kwargs = current_scope[key].copy()
#           current_kwargs.update(kwargs)
#           current_scope[key] = current_kwargs
#         else:
#           current_scope[key] = kwargs.copy()
#       _get_arg_stack().append(current_scope)
#       yield current_scope
#     finally:
#       _get_arg_stack().pop()


# Path: tf_slim/losses/loss_ops_test.py
import numpy as np
import tensorflow.compat.v1 as tf
import tf_slim.losses as loss_ops
from tf_slim.ops.arg_scope import arg_scope
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 random_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 momentum as momentum_lib



class AddLossTest(test.TestCase):

  def testAddExternalLoss(self):
    logits = constant_op.constant([[1.2, 0.4, -1.0, -1.1]])
    labels = constant_op.constant([[1.0, 0.0, 0.0, 1.0]])
    losses = loss_ops.hinge_loss(logits, labels)
    self.assertFalse(loss_ops.get_losses())
    loss_ops.add_loss(math_ops.reduce_mean(losses))
    self.assertTrue(loss_ops.get_losses())
    total_loss = loss_ops.get_total_loss()
    with self.cached_session():
      self.assertAllClose(losses.eval(), [[0.0, 1.4, 0.0, 2.1]], atol=1e-3)
      self.assertAllClose(total_loss.eval(), 3.5 / 4.0, atol=1e-3)

  def testNoneLossCollection(self):
    logits = constant_op.constant([[1.2, 0.4, -1.0, -1.1]])
    labels = constant_op.constant([[1.0, 0.0, 0.0, 1.0]])
    losses = loss_ops.hinge_loss(logits, labels)
    self.assertFalse(loss_ops.get_losses())
    loss_ops.add_loss(math_ops.reduce_mean(losses), loss_collection=None)
    self.assertFalse(loss_ops.get_losses())
    with self.cached_session():
      self.assertAllClose(losses.eval(), [[0.0, 1.4, 0.0, 2.1]], atol=1e-3)

  def testNoCollectLosses(self):
    logits = constant_op.constant([[1.2, 0.4, -1.0, -1.1]])
    labels = constant_op.constant([[1.0, 0.0, 0.0, 1.0]])
    self.assertFalse(loss_ops.get_losses())

    with arg_scope([loss_ops.add_loss], loss_collection=None):