Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does tf.nn.l2_loss and tf.contrib.layers.l2_regularizer serve the same purpose of adding L2 regularization in tensorflow?

It seems L2 regularization in tensorflow can be implemented in 2 ways:

(i) using tf.nn.l2_loss or (ii) using tf.contrib.layers.l2_regularizer

Will these both approaches serve the same purpose? If they differ, what do they differ at?

like image 961
Vishnu Sriram Avatar asked Jun 10 '17 13:06

Vishnu Sriram


1 Answers

They do the same thing (at least now). The only difference is that tf.contrib.layers.l2_regularizer multiplies the result of tf.nn.l2_loss by scale.

Look at the implementation of tf.contrib.layers.l2_regularizer[https://github.com/tensorflow/tensorflow/blob/r1.1/tensorflow/contrib/layers/python/layers/regularizers.py]:

def l2_regularizer(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')
      return standard_ops.multiply(my_scale, nn.l2_loss(weights), name=name)

  return l2

The line you are interested in is:

  return standard_ops.multiply(my_scale, nn.l2_loss(weights), name=name)

So in practice, tf.contrib.layers.l2_regularizer calls tf.nn.l2_loss internally and simply multiplies the result by the scale parameter.

like image 72
MZHm Avatar answered Sep 24 '22 18:09

MZHm