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)
参数
tensor 或 variable。float,用于控制低于阈值的值的斜率。float,用于设置饱和阈值(函数将返回的最大值)。float,表示激活函数的阈值,低于该阈值的值将被衰减或设置为零。返回
一个 Tensor,表示通过 relu 激活函数转换的输入张量。Tensor 将与输入 x 具有相同的形状和 dtype。
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)
参数
返回
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))。
输入值是结果概率的对数几率。
参数
返回
Tensor,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)
参数
返回
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 / (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 具有相同形状和 dtype 的 Tensor,具有 tanh 激活:tanh(x) = sinh(x)/cosh(x) = ((exp(x) - exp(-x))/(exp(x) + exp(-x)))。selu 函数tf_keras.activations.selu(x)
缩放指数线性单元 (SELU)。
缩放指数线性单元 (SELU) 激活函数定义为:
if x > 0: return scale * xif x < 0: return scale * alpha * (exp(x) - 1)其中 alpha 和 scale 是预定义的常数(alpha=1.67326324 和 scale=1.05070098)。
基本上,SELU 激活函数将 scale (> 1) 与 tf.keras.activations.elu 函数的输出相乘,以确保正输入的斜率大于一。
alpha 和 scale 的值被选择,以便在连续两层之间的输入的均值和方差保持不变,只要权重被正确初始化(参见 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'))
参数
返回
scale * elu(x, alpha)。注意: - 要与 tf.keras.initializers.LecunNormal 初始化器一起使用。 - 要与 dropout 变体 tf.keras.layers.AlphaDropout(非常规 dropout)一起使用。
参考文献
elu 函数tf_keras.activations.elu(x, alpha=1.0)
指数线性单元。
指数线性单元 (ELU) 且 alpha > 0 为:当 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'))
参数
alpha 控制 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)
参数
返回
exp(x)。