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

层激活函数

[源码]

relu 函数

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

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

使用默认值时,它返回标准的 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 激活函数转换。该张量将与输入x具有相同的形状和数据类型。


[源码]

sigmoid 函数

tf_keras.activations.sigmoid(x)

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

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

Sigmoid 等同于双元素 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))

输入值是结果概率的对数几率 (log-odds)。

参数

  • 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形状和数据类型相同,应用 tanh 激活的张量tanh(x) = sinh(x)/cosh(x) = ((exp(x) - exp(-x))/(exp(x) + exp(-x)))

[源码]

selu 函数

tf_keras.activations.selu(x)

缩放指数线性单元(Scaled Exponential Linear Unit,SELU)。

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

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

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

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

选择 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)

指数线性单元(Exponential Linear Unit,ELU)。

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)