admin管理员组

文章数量:1579446

Keras 中有leaky_relu的实现。leaky_relu被整合进了relu函数。
参考官方文档:

https://tensorflow.google/api_docs/python/tf/keras/backend/relu?hl=en

Arguments
xA tensor or variable.
alphaA scalar, slope of negative section (default=0.).
max_valuefloat. Saturation threshold.
thresholdfloat. Threshold value for thresholded activation.

alpha(超参数)值控制负数部分线性函数的梯度。当alpha = 0 ,是原始的relu函数。当alpha >0,即为leaky_relu。

查看源码,在Keras.backbend 中,也是调用tensorflow.python.ops库nn中的leaky_relu函数实现的:

def relu(x, alpha=0., max_value=None, threshold=0):
  """Rectified linear unit.
  With default values, it returns element-wise `max(x, 0)`.
  Otherwise, it follows:
  `f(x) = max_value` for `x >= max_value`,
  `f(x) = x` for `threshold <= x < max_value`,
  `f(x) = alpha * (x - threshold)` otherwise.
  Arguments:
      x: A tensor or variable.
      alpha: A scalar, slope of negative section (default=`0.`).
      max_value: float. Saturation threshold.
      threshold: float. Threshold value for thresholded activation.
  Returns:
      A tensor.
  """

  if alpha != 0.:
    if max_value is None and threshold == 0:
      return nn.leaky_relu(x, alpha=alpha)    ##在这里调用了leaky_relu

    if threshold != 0:
      negative_part = nn.relu(-x + threshold)
    else:
      negative_part = nn.relu(-x)

  clip_max = max_value is not None

  if threshold != 0:
    # computes x for x > threshold else 0
    x = x * math_ops.cast(math_ops.greater(x, threshold), floatx())
  elif max_value == 6:
    # if no threshold, then can use nn.relu6 native TF op for performance
    x = nn.relu6(x)
    clip_max = False
  else:
    x = nn.relu(x)

  if clip_max:
    max_value = _constant_to_tensor(max_value, x.dtype.base_dtype)
    zero = _constant_to_tensor(0, x.dtype.base_dtype)
    x = clip_ops.clip_by_value(x, zero, max_value)

  if alpha != 0.:
    alpha = _to_tensor(alpha, x.dtype.base_dtype)
    x -= alpha * negative_part
  return x

本文标签: kerasleakyrelu