TensorFlow激活函数大全

激活函数在神经网络中具有重要的作用,最主要的是给网络添加了更多的非线性。

1. ReLU函数

Relu函数的作用是求最大值,其公式为:

relu(x)=max(0,x)

在实数定义域内连续,但在0处不可微,但在实际应用中如果碰到0处求导的情况,默认直接让其导数等于0,这样就解决了0处不可导的问题。

ReLU用于激活函数的优点是计算导数简单,如果X大于0,导数为1,如果X小于0,导数为0。

tf.keras.activations.relu(x, alpha=0.0, max_value=None, threshold=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:浮点数,小于该的输入返回0

返回值:

返回与输入相同shape的张量


2. Sigmoid函数

Sigmoid函数表达式为:sigmoid(x) = {\tfrac{1}{1+e^{-x}}}

函数是S形曲线,在实数定义域内连续且可导,值域(0,1)。因为函数输出分布在0和1之间,所以输出可以被看作是概率。

当x<-5时,函数值接近于0;当x>5时,函数值接近于1。

Sigmoid函数也相当2个类别的Softmax函数。

tf.keras.activations.sigmoid(x)

示例:

>>> 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:输入张量

返回值:

与输入shape相同的函数值


3. softmax函数

softmax函数表达式为:

S_i = \frac{e^{V_i}} {\sum_i^C e^{V_i}}

其中:

  • Vi 是分类器前级输出单元的输出。
  • i 表示类别索引,总的类别个数为 C。
  • Si 表示的是当前元素的指数与所有元素指数和的比值,输出范围(0, 1) ,所有Si的和为1。

通过Softmax函数,就可以将多分类的输出数值转化为相对概率,因此softmax函数常常用于多分类任务的最后一层。

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

参数:

  • x:输入张量
  • axis:整数,从哪一个轴参与运行

返回值:

与输入相同shape的张量,每个值范围为(0,1),并且和为1。


4. tanh函数

tanh函数,又称双曲正切函数,其表达式为:

tanh(x)= \frac{sinh(x)}{cosh(x)} = \frac {e^x-e^{-x}}{e^x+e^{-x}}

tanh函数是S形曲线,在实数定义域内连续且可导,值域(-1,1)。因此,在训练刚开始时输出更可能在0附近,有利于模型的收敛。

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: 输入张量

返回值:

与输入相同shape和类型的张量


5. softplus function

tf.keras.activations.softplus(x)

Softplus activation function, softplus(x) = log(exp(x) + 1).

Example Usage:

 

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

 

Arguments

  • x: Input tensor.

Returns

The softplus activation: log(exp(x) + 1).


6. softsign function

tf.keras.activations.softsign(x)

Softsign activation function, softsign(x) = x / (abs(x) + 1).

Example Usage:

 

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

 

Arguments

  • x: Input tensor.

Returns

The softsign activation: x / (abs(x) + 1).


7. selu函数

tf.keras.activations.selu(x)

Scaled Exponential Linear Unit (SELU).

The Scaled Exponential Linear Unit (SELU) activation function is defined as:

  • if x > 0: return scale * x
  • if x < 0: return scale * alpha * (exp(x) - 1)

where alpha and scale are pre-defined constants (alpha=1.67326324 and scale=1.05070098).

Basically, the SELU activation function multiplies scale (> 1) with the output of the tf.keras.activations.elu function to ensure a slope larger than one for positive inputs.

The values of alpha and scale are chosen so that the mean and variance of the inputs are preserved between two consecutive layers as long as the weights are initialized correctly (see tf.keras.initializers.LecunNormal initializer) and the number of input units is "large enough" (see reference paper for more information).

Example Usage:

 

>>> 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'))

 

Arguments

  • x: A tensor or variable to compute the activation function for.

Returns

The scaled exponential unit activation: scale * elu(x, alpha).

Notes: - To be used together with the tf.keras.initializers.LecunNormal initializer. - To be used together with the dropout variant tf.keras.layers.AlphaDropout (not regular dropout).

References: - Klambauer et al., 2017


8. elu函数

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

Exponential Linear Unit.

The exponential linear unit (ELU) with alpha > 0 is: x if x > 0 and alpha * (exp(x) - 1) if x < 0 The ELU hyperparameter alpha controls the value to which an ELU saturates for negative net inputs. ELUs diminish the vanishing gradient effect.

ELUs have negative values which pushes the mean of the activations closer to zero. Mean activations that are closer to zero enable faster learning as they bring the gradient closer to the natural gradient. ELUs saturate to a negative value when the argument gets smaller. Saturation means a small derivative which decreases the variation and the information that is propagated to the next layer.

Example Usage:

 

>>> 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'))

 

 

Arguments

  • x: Input tensor.
  • alpha: A scalar, slope of negative section. alpha controls the value to which an ELU saturates for negative net inputs.

Returns

The exponential linear unit (ELU) activation function: x if x > 0 and alpha * (exp(x) - 1) if x < 0.

Reference: Fast and Accurate Deep Network Learning by Exponential Linear Units (ELUs) (Clevert et al, 2016)


9. exponential function

tf.keras.activations.exponential(x)

Exponential activation function.

For example:

 

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

 

Arguments

  • x: Input tensor.

Returns

Tensor with exponential activation: exp(x).

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值