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

层激活函数

激活函数的使用

激活函数可以通过一个 Activation 层使用,或者通过所有前向层支持的 activation 参数使用

model.add(layers.Dense(64, activation=activations.relu))

这等价于

from keras import layers
from keras import activations

model.add(layers.Dense(64))
model.add(layers.Activation(activations.relu))

所有内置激活函数也可以通过其字符串标识符传递

model.add(layers.Dense(64, activation='relu'))

可用激活函数

[源]

celu 函数

keras.activations.celu(x, alpha=1.0)

连续可微指数线性单元。

CeLU 激活函数定义为

celu(x) = alpha * (exp(x / alpha) - 1) (当 x < 0)celu(x) = x (当 x >= 0)

其中 alpha 是一个缩放参数,用于控制激活函数的形状。

参数

  • x: 输入张量。
  • alpha: CeLU 公式中的 α 值。默认为 1.0

参考


[源]

elu 函数

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

指数线性单元。

指数线性单元 (ELU) 当 alpha > 0 时定义为

  • x (当 x > 0)
  • alpha * exp(x) - 1 (当 x < 0)

ELU 具有负值,这使得激活的均值更接近于零。

更接近零的均值激活可以加快学习速度,因为它们使梯度更接近自然梯度。当自变量变小时,ELU 会饱和到负值。饱和意味着导数很小,这会降低变异性以及传播到下一层的信息量。

参数

  • x: 输入张量。
  • alpha: 一个标量,正部分的斜率。默认为 1.0

参考


[源]

exponential 函数

keras.activations.exponential(x)

指数激活函数。

参数

  • x: 输入张量。

[源]

gelu 函数

keras.activations.gelu(x, approximate=False)

高斯误差线性单元 (GELU) 激活函数。

高斯误差线性单元 (GELU) 定义为

gelu(x) = x * P(X <= x) 其中 P(X) ~ N(0, 1),即 gelu(x) = 0.5 * x * (1 + erf(x / sqrt(2)))

GELU 根据输入值对其进行加权,而不是像 ReLU 那样根据输入的符号进行门控。

参数

  • x: 输入张量。
  • approximate: 一个 bool 值,是否启用近似计算。

参考


[源]

glu 函数

keras.activations.glu(x, axis=-1)

门控线性单元 (GLU) 激活函数。

GLU 激活函数定义为

glu(x) = a * sigmoid(b),

其中 x 沿给定轴被分割成两个相等的部分 ab

参数

  • x: 输入张量。
  • axis: 沿其分割输入张量的轴。默认为 -1

参考


[源]

hard_shrink 函数

keras.activations.hard_shrink(x, threshold=0.5)

Hard Shrink 激活函数。

它定义为

hard_shrink(x) = x (当 |x| > threshold),否则 hard_shrink(x) = 0

参数

  • x: 输入张量。
  • threshold: 阈值。默认为 0.5。

[源]

hard_sigmoid 函数

keras.activations.hard_sigmoid(x)

Hard sigmoid 激活函数。

Hard sigmoid 激活函数定义为

  • 0 (当 x <= -3)
  • 1 (当 x >= 3)
  • (x/6) + 0.5 (当 -3 < x < 3)

它是 sigmoid 激活函数的更快的分段线性近似。

参数

  • x: 输入张量。

参考


[源]

hard_silu 函数

keras.activations.hard_silu(x)

Hard SiLU 激活函数,也称为 Hard Swish。

它定义为

  • 0 (当 x < -3)
  • x (当 x > 3)
  • x * (x + 3) / 6 (当 -3 <= x <= 3)

它是 silu 激活函数的更快的分段线性近似。

参数

  • x: 输入张量。

参考


[源]

hard_tanh 函数

keras.activations.hard_tanh(x)

HardTanh 激活函数。

它定义为:hard_tanh(x) = -1 (当 x < -1)hard_tanh(x) = x (当 -1 <= x <= 1)hard_tanh(x) = 1 (当 x > 1)

参数

  • x: 输入张量。

[源]

leaky_relu 函数

keras.activations.leaky_relu(x, negative_slope=0.2)

Leaky relu 激活函数。

参数

  • x: 输入张量。
  • negative_slope: 一个 float 值,控制低于阈值的斜率。

[源]

linear 函数

keras.activations.linear(x)

线性激活函数(直通)。

“线性”激活函数是恒等函数:它返回未修改的输入。

参数

  • x: 输入张量。

[源]

log_sigmoid 函数

keras.activations.log_sigmoid(x)

sigmoid 激活函数的对数。

它定义为 f(x) = log(1 / (1 + exp(-x)))

参数

  • x: 输入张量。

[源]

log_softmax 函数

keras.activations.log_softmax(x, axis=-1)

Log-Softmax 激活函数。

每个输入向量独立处理。axis 参数设置函数沿哪个输入轴应用。

参数

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

[源]

mish 函数

keras.activations.mish(x)

Mish 激活函数。

它定义为

mish(x) = x * tanh(softplus(x))

其中 softplus 定义为

softplus(x) = log(exp(x) + 1)

参数

  • x: 输入张量。

参考


[源]

relu 函数

keras.activations.relu(x, negative_slope=0.0, max_value=None, threshold=0.0)

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

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

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

示例

>>> x = [-10, -5, 0.0, 5, 10]
>>> keras.activations.relu(x)
[ 0.,  0.,  0.,  5., 10.]
>>> keras.activations.relu(x, negative_slope=0.5)
[-5. , -2.5,  0. ,  5. , 10. ]
>>> keras.activations.relu(x, max_value=5.)
[0., 0., 0., 5., 5.]
>>> keras.activations.relu(x, threshold=5.)
[-0., -0.,  0.,  0., 10.]

参数

  • x: 输入张量。
  • negative_slope: 一个 float 值,控制低于阈值的斜率。
  • max_value: 一个 float 值,设置饱和阈值(函数返回的最大值)。
  • threshold: 一个 float 值,表示激活函数的阈值,低于此阈值的值将被抑制或设为零。

返回值

一个张量,形状和 dtype 与输入 x 相同。


[源]

relu6 函数

keras.activations.relu6(x)

Relu6 激活函数。

它是 ReLU 函数,但被截断到最大值为 6。

参数

  • x: 输入张量。

[源]

selu 函数

keras.activations.selu(x)

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

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

  • scale * x (当 x > 0)
  • scale * alpha * (exp(x) - 1) (当 x < 0)

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

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

alphascale 的选择使得只要权重初始化正确(参见 keras.initializers.LecunNormal 初始化器)且输入单元数量“足够大”(更多信息请参见参考文献),则连续两层之间的输入均值和方差得以保留。

参数

  • x: 输入张量。

注意

参考


[源]

sigmoid 函数

keras.activations.sigmoid(x)

Sigmoid 激活函数。

它定义为:sigmoid(x) = 1 / (1 + exp(-x))

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

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

参数

  • x: 输入张量。

[源]

silu 函数

keras.activations.silu(x)

Swish(或 Silu)激活函数。

它定义为:swish(x) = x * sigmoid(x)

Swish(或 Silu)激活函数是一个平滑、非单调的函数,其上无界而下有界。

参数

  • x: 输入张量。

参考


[源]

softmax 函数

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

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

输出向量的元素范围在 [0, 1] 内且总和为 1。

每个输入向量独立处理。axis 参数设置函数沿哪个输入轴应用。

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

每个向量 x 的 softmax 计算为 exp(x) / sum(exp(x))

输入值是结果概率的对数几率。

参数

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

[源]

soft_shrink 函数

keras.activations.soft_shrink(x, threshold=0.5)

Soft Shrink 激活函数。

它定义为

soft_shrink(x) = x - threshold (当 x > threshold),soft_shrink(x) = x + threshold (当 x < -threshold),否则 soft_shrink(x) = 0

参数

  • x: 输入张量。
  • threshold: 阈值。默认为 0.5。

[源]

softplus 函数

keras.activations.softplus(x)

Softplus 激活函数。

它定义为:softplus(x) = log(exp(x) + 1)

参数

  • x: 输入张量。

[源]

softsign 函数

keras.activations.softsign(x)

Softsign 激活函数。

Softsign 定义为:softsign(x) = x / (abs(x) + 1)

参数

  • x: 输入张量。

[源]

sparse_plus 函数

keras.activations.sparse_plus(x)

SparsePlus 激活函数。

SparsePlus 定义为

sparse_plus(x) = 0 (当 x <= -1)sparse_plus(x) = (1/4) * (x + 1)^2 (当 -1 < x < 1)sparse_plus(x) = x (当 x >= 1)

参数

  • x: 输入张量。

[源]

sparsemax 函数

keras.activations.sparsemax(x, axis=-1)

Sparsemax 激活函数。

对于每个 batch i 和类别 j,sparsemax 激活函数定义为

sparsemax(x)[i, j] = max(x[i, j] - τ(x[i, :]), 0)。

参数

  • x: 输入张量。
  • axis: int,应用 sparsemax 操作的轴。

返回值

一个张量,sparsemax 转换的输出。与 x 具有相同的类型和形状。

参考


[源]

squareplus 函数

keras.activations.squareplus(x, b=4)

Squareplus 激活函数。

Squareplus 激活函数定义为

f(x) = (x + sqrt(x^2 + b)) / 2

其中 b 是一个平滑参数。

参数

  • x: 输入张量。
  • b: 平滑参数。默认为 4。

参考


[源]

tanh 函数

keras.activations.tanh(x)

双曲正切激活函数。

它定义为:tanh(x) = sinh(x) / cosh(x),即 tanh(x) = ((exp(x) - exp(-x)) / (exp(x) + exp(-x)))

参数

  • x: 输入张量。

[源]

tanh_shrink 函数

keras.activations.tanh_shrink(x)

Tanh shrink 激活函数。

它定义为

f(x) = x - tanh(x).

参数

  • x: 输入张量。

[源]

threshold 函数

keras.activations.threshold(x, threshold, default_value)

阈值激活函数。

它定义为

threshold(x) = x (当 x > threshold),否则 threshold(x) = default_value

参数

  • x: 输入张量。
  • threshold: 决定何时保留或替换 x 的值。
  • default_value: 当 x <= threshold 时赋的值。


创建自定义激活函数

您也可以使用可调用对象作为激活函数(在这种情况下,它应该接受一个张量并返回具有相同形状和 dtype 的张量)

model.add(layers.Dense(64, activation=keras.ops.tanh))

关于“高级激活”层

比简单函数更复杂的激活函数(例如,可学习的激活函数,它们维护状态)可作为 高级激活层 使用。