text: # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #     http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  # ==============================================================================
  """Regularizers for use with layers."""
  
  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  
  import numbers
  
  from tensorflow.python.framework import constant_op
  from tensorflow.python.framework import ops
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops import nn
  from tensorflow.python.ops import standard_ops
  from tensorflow.python.platform import tf_logging as logging
  
  def weight_l2_regularizer(initial_weights, scale, scope=None):
    """Returns a function that can be used to apply L2 regularization to weights.
    Small values of L2 can help prevent overfitting the training data.
    Args:
      scale: A scalar multiplier `Tensor`. 0.0 disables the regularizer.
      scope: An optional scope name.
    Returns:
      A function with signature `l2(weights)` that applies L2 regularization.
    Raises:
      ValueError: If scale is negative or if scale is not a float.
    """
    if isinstance(scale, numbers.Integral):
      raise ValueError('scale cannot be an integer: %s' % (scale,))
    if isinstance(scale, numbers.Real):
      if scale < 0.:
        raise ValueError('Setting a scale less than 0 on a regularizer: %g.' %
                         scale)
      if scale == 0.:
        logging.info('Scale of 0 disables regularizer.')
        return lambda _: None
  
    def l2(weights):
      """Applies l2 regularization to weights."""
      with ops.name_scope(scope, 'l2_regularizer', [weights]) as name:
        my_scale = ops.convert_to_tensor(scale,
                                         dtype=weights.dtype.base_dtype,
                                         name='scale')
        weight_diff = initial_weights - weights
        return standard_ops.multiply(my_scale, nn.l2_loss(weight_diff), name=name)
  
    return l2
  