# Path: morph_net/network_regularizers/activation_regularizer.py
# class GammaActivationRegularizer(generic_regularizers.NetworkRegularizer):
# class GroupLassoActivationRegularizer(generic_regularizers.NetworkRegularizer):
#   def __init__(self,
#                output_boundary: List[tf.Operation],
#                gamma_threshold,
#                regularizer_decorator: Optional[Type[
#                    generic_regularizers.OpRegularizer]] = None,
#                decorator_parameters=None,
#                input_boundary: Optional[List[tf.Operation]] = None,
#                force_group=None,
#                regularizer_blacklist=None):
#   def get_regularization_term(self, ops=None):
#   def get_cost(self, ops=None):
#   def op_regularizer_manager(self):
#   def name(self):
#   def cost_name(self):
#   def __init__(self,
#                output_boundary: List[tf.Operation],
#                threshold,
#                l1_fraction=0,
#                regularizer_decorator: Optional[Type[
#                    generic_regularizers.OpRegularizer]] = None,
#                decorator_parameters=None,
#                input_boundary: Optional[List[tf.Operation]] = None,
#                force_group=None,
#                regularizer_blacklist=None):
#   def get_regularization_term(self, ops=None):
#   def get_cost(self, ops=None):
#   def op_regularizer_manager(self):
#   def name(self):
#   def cost_name(self):


from absl.testing import parameterized
from morph_net.network_regularizers import activation_regularizer
from tensorflow.contrib import slim as contrib_slim
import numpy as np
import tensorflow.compat.v1 as tf
  def test_loss(self, conv_name, expected_loss):
    self.assertAllClose(expected_loss, self.loss([self.get_conv(conv_name)]))

  def test_loss_gradient_conv2(self):
    loss = self.gamma_activation_reg.get_regularization_term(
        [self.get_conv('conv2')])
    expected_grad = np.array([-1.0] * 11 + [1.0] * 12)
    gammas = [
        self.name_to_var['conv%d/BatchNorm/gamma' % i] for i in range(1, 5)
    ]

    # Although the loss associated with conv2 depends on the gammas of conv2,
    # conv3 and conv4, only gamma2 should receive graients. The other gammas are
    # compared to the threshold to see if they are alive or not, but this should
    # not send gradients to them.
    grads = tf.gradients(loss, gammas)
    self.assertEqual(None, grads[0])
    self.assertEqual(None, grads[2])
    self.assertEqual(None, grads[3])

    # Regarding gamma2, it receives a -1 or a +1 gradient, depending on whether
    # the gamma is negative or positive, since the loss is |gamma|. This is
    # multiplied by expected_coeff.
    with self.cached_session():
      self.assertAllClose(expected_grad, grads[1].eval())

  def test_conv_has_no_gamma(self):
    conv5 = slim.layers.conv2d(
        self.conv3, 11, [1, 1], stride=1, padding='SAME', scope='conv5')
    self.gamma_activation_reg = (

# Path: morph_net/network_regularizers/activation_regularizer.py
# class GammaActivationRegularizer(generic_regularizers.NetworkRegularizer):
# class GroupLassoActivationRegularizer(generic_regularizers.NetworkRegularizer):
#   def __init__(self,
#                output_boundary: List[tf.Operation],
#                gamma_threshold,
#                regularizer_decorator: Optional[Type[
#                    generic_regularizers.OpRegularizer]] = None,
#                decorator_parameters=None,
#                input_boundary: Optional[List[tf.Operation]] = None,
#                force_group=None,
#                regularizer_blacklist=None):
#   def get_regularization_term(self, ops=None):
#   def get_cost(self, ops=None):
#   def op_regularizer_manager(self):
#   def name(self):
#   def cost_name(self):
#   def __init__(self,
#                output_boundary: List[tf.Operation],
#                threshold,
#                l1_fraction=0,
#                regularizer_decorator: Optional[Type[
#                    generic_regularizers.OpRegularizer]] = None,
#                decorator_parameters=None,
#                input_boundary: Optional[List[tf.Operation]] = None,
#                force_group=None,
#                regularizer_blacklist=None):
#   def get_regularization_term(self, ops=None):
#   def get_cost(self, ops=None):
#   def op_regularizer_manager(self):
#   def name(self):
#   def cost_name(self):


# Path: morph_net/network_regularizers/activation_regularizer_test.py
from absl.testing import parameterized
from morph_net.network_regularizers import activation_regularizer
from tensorflow.contrib import slim as contrib_slim
import numpy as np
import tensorflow.compat.v1 as tf

  def test_loss(self, conv_name, expected_loss):
    self.assertAllClose(expected_loss, self.loss([self.get_conv(conv_name)]))

  def test_loss_gradient_conv2(self):
    loss = self.gamma_activation_reg.get_regularization_term(
        [self.get_conv('conv2')])
    expected_grad = np.array([-1.0] * 11 + [1.0] * 12)
    gammas = [
        self.name_to_var['conv%d/BatchNorm/gamma' % i] for i in range(1, 5)
    ]

    # Although the loss associated with conv2 depends on the gammas of conv2,
    # conv3 and conv4, only gamma2 should receive graients. The other gammas are
    # compared to the threshold to see if they are alive or not, but this should
    # not send gradients to them.
    grads = tf.gradients(loss, gammas)
    self.assertEqual(None, grads[0])
    self.assertEqual(None, grads[2])
    self.assertEqual(None, grads[3])

    # Regarding gamma2, it receives a -1 or a +1 gradient, depending on whether
    # the gamma is negative or positive, since the loss is |gamma|. This is
    # multiplied by expected_coeff.
    with self.cached_session():
      self.assertAllClose(expected_grad, grads[1].eval())

  def test_conv_has_no_gamma(self):
    conv5 = slim.layers.conv2d(
        self.conv3, 11, [1, 1], stride=1, padding='SAME', scope='conv5')
    self.gamma_activation_reg = (

        activation_regularizer.GammaActivationRegularizer(