TensorFlow2.0常用函数(四)

1、tf.nn.sigmoid()  #Sigmoid函数的输出范围在(0, 1)之间,曾经是神经网络中最常用的激活函数,目前已被ReLU函数所替代

import tensorflow as tf
x=tf.Variable([-1.,-2.,0.,1.,2.],dtype=tf.float32)
sigmoid = tf.nn.sigmoid(x)
print(sigmoid)

输出结果:(Sigmoid函数将输入压缩到0-1范围内,中间输入接近0时,输出变化最快)

tf.Tensor([0.26894143 0.11920292 0.5 0.7310586  0.8807971 ], shape=(5,), dtype=float32)


2、tf.nn.tanh()  #Tanh函数的输出范围在(-1, 1)之间,和Sigmoid函数类似,目前已被ReLU函数替代

import tensorflow as tf
x=tf.Variable([-1.,-2.,0.,1.,2.],dtype=tf.float32)
tanh = tf.nn.tanh(x)
print(tanh)

输出结果:(Tanh函数将输入压缩到-1,1范围内,中间输入值接近0时,输出变化最快)

tf.Tensor([-0.7615942 -0.9640276  0.  0.7615942  0.9640276], shape=(5,), dtype=float32)


3、tf.nn.relu()  #函数目前是最常用的激活函数,它可以有效地解决神经网络中的梯度消失问题,relu的缺点如果其特征输入恰好在ReLU函数的消失区间(x < 0),那么该样本的梯度将无法有效反向传播,可能导致梯度消失问题

import tensorflow as tf
x=tf.Variable([-1.,-2.,0.,1.,2.],dtype=tf.float32)
relu = tf.nn.relu(x)
print(relu)

输出结果:(ReLU函数目前是最常用的激活函数,它可以有效地解决神经网络中的梯度消失问题)

tf.Tensor([0. 0. 0. 1. 2.], shape=(5,), dtype=float32)


4、tf.nn.leaky_relu()  #LeakyReLU是ReLU的变体,它可以给负值赋予一个非零斜率,缓解梯度消失问题

import tensorflow as tf
x=tf.Variable([-1.,-2.,0.,1.,2.],dtype=tf.float32)
leakyRelu = tf.nn.leaky_relu(x)
print(leakyRelu)

输出结果:(LeakyReLU允许负值有一个小的非零斜率(默认为0.2),而不是像ReLU那样完全过滤)

tf.Tensor([-0.2 -0.4  0.  1.   2. ], shape=(5,), dtype=float32)


5、tf.nn.softmax()  #Softmax函数通常用于神经网络的输出层,它可以将多个实数值压缩到0-1范围内,并使它们的和为1,产生一个概率分布

import tensorflow as tf
x=tf.Variable([-1.,-2.,0.,1.,2.],dtype=tf.float32)
softmax = tf.nn.softmax(x)
print(softmax)

输出结果:(Softmax输出的每个向量的元素之和为1,代表每个类别的概率)

tf.Tensor([0.03168492 0.01165623 0.08612854 0.23412165 0.6364086 ], shape=(5,), dtype=float32)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

缘起性空、

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

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

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

打赏作者

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

抵扣说明:

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

余额充值