Keras 2 API 文档 / 层 API / 层激活

层激活

[源代码]

relu 函数

tf_keras.activations.relu(x, alpha=0.0, max_value=None, threshold=0.0)

应用修正线性单元激活函数。

使用默认值时,这将返回标准的 ReLU 激活:max(x, 0),即 0 和输入张量的元素级最大值。

修改默认参数允许您使用非零阈值,更改激活的最大值,以及对低于阈值的值使用输入的非零倍数。

示例

>>> foo = tf.constant([-10, -5, 0.0, 5, 10], dtype = tf.float32)
>>> tf.keras.activations.relu(foo).numpy()
array([ 0.,  0.,  0.,  5., 10.], dtype=float32)
>>> tf.keras.activations.relu(foo, alpha=0.5).numpy()
array([-5. , -2.5,  0. ,  5. , 10. ], dtype=float32)
>>> tf.keras.activations.relu(foo, max_value=5.).numpy()
array([0., 0., 0., 5., 5.], dtype=float32)
>>> tf.keras.activations.relu(foo, threshold=5.).numpy()
array([-0., -0.,  0.,  0., 10.], dtype=float32)

参数

  • x:输入张量变量
  • alpha:一个浮点数,用于控制低于阈值的值的斜率。
  • max_value:一个浮点数,用于设置饱和阈值(函数将返回的最大值)。
  • threshold:一个浮点数,给出激活函数的阈值,低于该阈值的值将被抑制或设置为零。

返回

一个张量,表示输入张量,经过 relu 激活函数转换。张量的形状和 dtype 将与输入 x 相同。


[源代码]

sigmoid 函数

tf_keras.activations.sigmoid(x)

Sigmoid 激活函数,sigmoid(x) = 1 / (1 + exp(-x))

应用 sigmoid 激活函数。对于小值(<-5),sigmoid 返回接近于零的值,对于大值(>5),函数的结果接近于 1。

Sigmoid 等效于一个 2 元素的 Softmax,其中第二个元素被假定为零。sigmoid 函数始终返回介于 0 和 1 之间的值。

示例

>>> a = tf.constant([-20, -1.0, 0.0, 1.0, 20], dtype = tf.float32)
>>> b = tf.keras.activations.sigmoid(a)
>>> b.numpy()
array([2.0611537e-09, 2.6894143e-01, 5.0000000e-01, 7.3105860e-01,
         1.0000000e+00], dtype=float32)

参数

  • x:输入张量。

返回

  • 具有 sigmoid 激活的张量1 / (1 + exp(-x))

[源代码]

softmax 函数

tf_keras.activations.softmax(x, axis=-1)

Softmax 将值向量转换为概率分布。

输出向量的元素范围为 (0, 1) 并且总和为 1。

每个向量都是独立处理的。axis 参数设置函数应用到输入的哪个轴上。

Softmax 通常用作分类网络的最后一层的激活,因为结果可以解释为概率分布。

每个向量 x 的 softmax 计算为 exp(x) / tf.reduce_sum(exp(x))

输入值是所得概率的对数优势比。

参数

  • x:输入张量。
  • axis:整数,应用 softmax 归一化的轴。

返回

张量,softmax 转换的输出(所有值均为非负数且总和为 1)。

示例

示例 1:独立使用

>>> inputs = tf.random.normal(shape=(32, 10))
>>> outputs = tf.keras.activations.softmax(inputs)
>>> tf.reduce_sum(outputs[0, :])  # Each sample in the batch now sums to 1
<tf.Tensor: shape=(), dtype=float32, numpy=1.0000001>

示例 2:在 Dense 层中使用

>>> layer = tf.keras.layers.Dense(32,
...                               activation=tf.keras.activations.softmax)

[源代码]

softplus 函数

tf_keras.activations.softplus(x)

Softplus 激活函数,softplus(x) = log(exp(x) + 1)

示例用法

>>> a = tf.constant([-20, -1.0, 0.0, 1.0, 20], dtype = tf.float32)
>>> b = tf.keras.activations.softplus(a)
>>> b.numpy()
array([2.0611537e-09, 3.1326166e-01, 6.9314718e-01, 1.3132616e+00,
         2.0000000e+01], dtype=float32)

参数

  • x:输入张量。

返回

  • softplus 激活log(exp(x) + 1)

[源代码]

softsign 函数

tf_keras.activations.softsign(x)

Softsign 激活函数,softsign(x) = x / (abs(x) + 1)

示例用法

>>> a = tf.constant([-1.0, 0.0, 1.0], dtype = tf.float32)
>>> b = tf.keras.activations.softsign(a)
>>> b.numpy()
array([-0.5,  0. ,  0.5], dtype=float32)

参数

  • x:输入张量。

返回

  • softsign 激活x / (abs(x) + 1)

[源代码]

tanh 函数

tf_keras.activations.tanh(x)

双曲正切激活函数。

示例

>>> a = tf.constant([-3.0, -1.0, 0.0, 1.0, 3.0], dtype = tf.float32)
>>> b = tf.keras.activations.tanh(a)
>>> b.numpy()
array([-0.9950547, -0.7615942,  0.,  0.7615942,  0.9950547], dtype=float32)

参数

  • x:输入张量。

返回

  • 与输入 x 具有相同形状和 dtype 的张量,带有 tanh 激活tanh(x) = sinh(x)/cosh(x) = ((exp(x) - exp(-x))/(exp(x) + exp(-x)))

[源代码]

selu 函数

tf_keras.activations.selu(x)

缩放指数线性单元 (SELU)。

缩放指数线性单元 (SELU) 激活函数定义为

  • 如果 x > 0:返回 scale * x
  • 如果 x < 0:返回 scale * alpha * (exp(x) - 1)

其中 alphascale 是预定义的常数(alpha=1.67326324scale=1.05070098)。

基本上,SELU 激活函数将 scale (> 1) 与 tf.keras.activations.elu 函数的输出相乘,以确保正输入的斜率大于 1。

alphascale 的值经过选择,使得只要权重初始化正确(请参阅 tf.keras.initializers.LecunNormal 初始化器)并且输入单元的数量“足够大”(有关更多信息,请参阅参考论文),则两个连续层之间的输入的平均值和方差会保持不变。

示例用法

>>> num_classes = 10  # 10-class problem
>>> model = tf.keras.Sequential()
>>> model.add(tf.keras.layers.Dense(64, kernel_initializer='lecun_normal',
...                                 activation='selu'))
>>> model.add(tf.keras.layers.Dense(32, kernel_initializer='lecun_normal',
...                                 activation='selu'))
>>> model.add(tf.keras.layers.Dense(16, kernel_initializer='lecun_normal',
...                                 activation='selu'))
>>> model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))

参数

  • x:用于计算激活函数的张量或变量。

返回

  • 缩放指数单元激活scale * elu(x, alpha)

注意: - 要与 tf.keras.initializers.LecunNormal 初始化器一起使用。 - 要与 dropout 变体 tf.keras.layers.AlphaDropout(而非常规 dropout)一起使用。

参考文献


[源代码]

elu 函数

tf_keras.activations.elu(x, alpha=1.0)

指数线性单元。

具有 alpha > 0 的指数线性单元 (ELU) 为:如果 x > 0 则为 x,如果 x < 0 则为 alpha * (exp(x) - 1)。ELU 超参数 alpha 控制 ELU 对负净输入饱和的值。ELU 减少了梯度消失效应。

ELU 具有负值,这会将激活的平均值推向接近于零。接近于零的平均激活可以更快地学习,因为它们使梯度更接近自然梯度。当参数变小时,ELU 会饱和到负值。饱和意味着小导数,这会减少变化和传播到下一层的信息。

示例用法

>>> import tensorflow as tf
>>> model = tf.keras.Sequential()
>>> model.add(tf.keras.layers.Conv2D(32, (3, 3), activation='elu',
...          input_shape=(28, 28, 1)))
>>> model.add(tf.keras.layers.MaxPooling2D((2, 2)))
>>> model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='elu'))
>>> model.add(tf.keras.layers.MaxPooling2D((2, 2)))
>>> model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='elu'))

参数

  • x:输入张量。
  • alpha:标量,负部分的斜率。alpha 控制 ELU 对负净输入饱和的值。

返回

  • 指数线性单元 (ELU) 激活函数:如果 x > 0 则为 x,如果 x < 0 则为 alpha * (exp(x) - 1)

参考文献


[源代码]

exponential 函数

tf_keras.activations.exponential(x)

指数激活函数。

示例

>>> a = tf.constant([-3.0, -1.0, 0.0, 1.0, 3.0], dtype = tf.float32)
>>> b = tf.keras.activations.exponential(a)
>>> b.numpy()
array([0.04978707,  0.36787945,  1.,  2.7182817 , 20.085537], dtype=float32)

参数

  • x:输入张量。

返回

  • 具有指数激活的张量exp(x)