(19)tensorflow激活函数

激活函数

激活函数函数代码
Sigmoidtf.nn.sigmoid(x)
ReLUtf.nn.relu(x)
LeakyReLUtf.nn.leaky_relu(x, alpha)
Tanhtf.nn.tanh(x)

Sigmoid

Sigmoid图像

在这里插入图片描述

logistic函数,因为其形状为S型,又称Sigmoid函数
  f ( x ) = e x / ( 1 + e x )   = 1 / ( 1 + e − x ) , . \ f(x) = e^{x}/(1+e^{x})\ =1/(1+e^{-x})\\,.  f(x)=ex/(1+ex) =1/(1+ex),.

Sigmoid特点

  • 单调函数
  • 定义域即自变量 x i x_i xi的取值为   ( − ∞ , + ∞ ) \ (-\infty, +\infty)  (,+),值域为 ( 0 , 1 ) (0,1) (0,1)
  • 处处可微、处处可导
  • 可做概率分布函数

Sigmoid调用

  • tf.nn.sigmoid(x)
import tensorflow as tf
x = tf.linspace(-7.,7,10)
a = tf.nn.sigmoid(x)
print(x)
print(a)

out:
tf.Tensor(
[-7.        -5.4444447 -3.8888888 -2.333333  -0.7777777  0.7777777
  2.333334   3.8888893  5.4444447  7.       ], shape=(10,), dtype=float32)
tf.Tensor(
[9.1102719e-04 4.3017268e-03 2.0057559e-02 8.8399708e-02 3.1479907e-01
 6.8520093e-01 9.1160035e-01 9.7994250e-01 9.9569833e-01 9.9908900e-01], shape=(10,), dtype=float32)

ReLU

ReLU 图像

在这里插入图片描述
线性整流函数(Rectified Linear Unit, ReLU),又称修正线性单元,是一种人工神经网络中常用的激活函数(activation function):
f ( x ) = m a x ( 0 , x ) f(x)=max(0,x) f(x)=max(0,x)

ReLU特点

  • 更加有效率的梯度下降以及反向传播:避免了梯度爆炸和梯度消失问题
  • 简化计算过程:没有了其他复杂激活函数中诸如指数函数的影响
  • 同时活跃度的分散性使得神经网络整体计算成本下降

ReLU调用

  • tf.nn.relu(x)
import tensorflow as tf
x = tf.linspace(-7.,7,10)
a = tf.nn.relu(x)
print(x)
print(a)

out:
tf.Tensor(
[-7.        -5.4444447 -3.8888888 -2.333333  -0.7777777  0.7777777
  2.333334   3.8888893  5.4444447  7.       ], shape=(10,), dtype=float32)
tf.Tensor(
[0.        0.        0.        0.        0.        0.7777777 2.333334
 3.8888893 5.4444447 7.       ], shape=(10,), dtype=float32)

LeakyReLU

LeakyReLU图像

在这里插入图片描述
f ( x ) = m a x ( a l p h a ∗ x , x ) f(x)=max(alpha*x,x) f(x)=max(alphax,x)

leakyReLU特点

  • ReLU 函数在𝑥 < 0时导数值恒为 0,也可能会造成梯度弥散现象
  • 当𝑝 ≠ 0时, 𝑥 < 0处能够获得较小的导数值𝑝,从而避免出现梯度弥散现

leakyReLU调用

  • tf.nn.leaky_relu(x, alpha)
  • alpha为斜率
  • tf.nn.leaky_relu 对应的类为 layers.LeakyReLU,可以通过LeakyReLU(alpha)创建LeakyReLU网络层放置合适位置
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras import Sequential
x = tf.linspace(-7.,7,10)
a = tf.nn.leaky_relu(x,0.1)
print(x)
print(a)

out:
tf.Tensor(
[-7.        -5.4444447 -3.8888888 -2.333333  -0.7777777  0.7777777
  2.333334   3.8888893  5.4444447  7.       ], shape=(10,), dtype=float32)
tf.Tensor(
[-0.7        -0.5444445  -0.3888889  -0.2333333  -0.07777777  0.7777777
  2.333334    3.8888893   5.4444447   7.        ], shape=(10,), dtype=float32)

Tanh

Tanh图像

在这里插入图片描述
t a n h ( x ) = ( e x − e − x ) / ( e x + e − x ) = 2 S i g m o i d ( 2 x ) − 1 tanh(x) =(e^x − e^{−x})/(e^x + e^{−x}) = 2Sigmoid(2x)-1 tanh(x)=(exex)/(ex+ex)=2Sigmoid(2x)1

Tanh特点

  • tanh 激活函数可通过 Sigmoid 函数缩放平移后实现
  • 向量元素值的范围被映射到(−1,1)之间

Tanh调用

import tensorflow as tf
x = tf.linspace(-7.,7,10)
a = tf.nn.tanh(x)
print(x)
print(a)

out:
tf.Tensor(
[-7.        -5.4444447 -3.8888888 -2.333333  -0.7777777  0.7777777
  2.333334   3.8888893  5.4444447  7.       ], shape=(10,), dtype=float32)
tf.Tensor(
[-0.99999833 -0.99996257 -0.9991625  -0.9813682  -0.65142936  0.65142936
  0.9813681   0.9991625   0.99996257  0.99999833], shape=(10,), dtype=float32)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小蜗笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值