ReLU 层

[源代码]

ReLU

tf_keras.layers.ReLU(max_value=None, negative_slope=0.0, threshold=0.0, **kwargs)

修正线性单元激活函数。

使用默认值时,它返回逐元素的 max(x, 0)

否则,它遵循以下公式:

    f(x) = max_value if x >= max_value
    f(x) = x if threshold <= x < max_value
    f(x) = negative_slope * (x - threshold) otherwise

用法

>>> layer = tf.keras.layers.ReLU()
>>> output = layer([-3.0, -1.0, 0.0, 2.0])
>>> list(output.numpy())
[0.0, 0.0, 0.0, 2.0]
>>> layer = tf.keras.layers.ReLU(max_value=1.0)
>>> output = layer([-3.0, -1.0, 0.0, 2.0])
>>> list(output.numpy())
[0.0, 0.0, 0.0, 1.0]
>>> layer = tf.keras.layers.ReLU(negative_slope=1.0)
>>> output = layer([-3.0, -1.0, 0.0, 2.0])
>>> list(output.numpy())
[-3.0, -1.0, 0.0, 2.0]
>>> layer = tf.keras.layers.ReLU(threshold=1.5)
>>> output = layer([-3.0, -1.0, 1.0, 2.0])
>>> list(output.numpy())
[0.0, 0.0, 0.0, 2.0]

输入形状

任意。在模型中使用此层作为第一层时,请使用关键字参数 input_shape(整数元组,不包括批处理轴)。

输出形状

与输入形状相同。

参数

  • max_value: 浮点数 >= 0。最大激活值。None 表示无限制。默认为 None
  • negative_slope: 浮点数 >= 0。负斜率系数。默认为 0.
  • threshold: 浮点数 >= 0。阈值激活的阈值。默认为 0.