2017-06-10 15 views

答えて

3

これは同じことです(少なくとも今は)。唯一の違いは、tf.contrib.layers.l2_regularizerがによってtf.nn.l2_lossの結果を乗算することです。 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 

興味がある行は次のようになります。実際にはそう

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

、内部および単純で結果を乗算tf.nn.l2_losstf.contrib.layers.l2_regularizer呼び出しscaleパラメータ。

関連する問題