python Sigmoid和Tanh 可视化

目录

可视化图:

Sigmoid优点和缺点

tanh函数的优缺点:

可视化代码:


可视化图:

Sigmoid优点和缺点

优点:

Sigmoid函数的输出在(0,1)之间,输出范围有限,优化稳定,可以用作输出层。
连续函数,便于求导。
缺点:

1. sigmoid函数在变量取绝对值非常大的正值或负值时会出现饱和现象,意味着函数会变得很平,并且对输入的微小改变会变得不敏感。

在反向传播时,当梯度接近于0,权重基本不会更新,很容易就会出现梯度消失的情况,从而无法完成深层网络的训练。

2. sigmoid函数的输出不是0均值的,会导致后层的神经元的输入是非0均值的信号,这会对梯度产生影响。

3. 计算复杂度高,因为sigmoid函数是指数形式。

Sigmoid适合在输出层。

原文链接:https://blog.csdn.net/weixin_39731586/article/details/111582190

tanh函数的优缺点:

优点:
部分解决了sigmoid关于zero-centered的输出问题。导数范围变大在(0,1)之间,而sigmoid在 (0,0.25)之间,梯度消失问题有所缓解。


缺点:
•幂运算,计算成本高 •梯度消失问题

可视化代码:

最近在研究神经网络,用python绘制了一下常见的Sigmoid函数和Tanh函数,别的不多说,直接上代码:

#!/usr/bin/python #encoding:utf-8  
import math  
import matplotlib.pyplot as plt  
import numpy as np  
import matplotlib as mpl  
mpl.rcParams['axes.unicode_minus']=False  
  
  
def  sigmoid(x):  
    return 1.0 / (1.0 + np.exp(-x))  
  
fig = plt.figure(figsize=(6,4))  
ax = fig.add_subplot(111)  
  
x = np.linspace(-10, 10)  
y = sigmoid(x)  
tanh = 2*sigmoid(2*x) - 1  
  
plt.xlim(-11,11)  
plt.ylim(-1.1,1.1)  
  
ax.spines['top'].set_color('none')  
ax.spines['right'].set_color('none')  
  
ax.xaxis.set_ticks_position('bottom')  
ax.spines['bottom'].set_position(('data',0))  
ax.set_xticks([-10,-5,0,5,10])  
ax.yaxis.set_ticks_position('left')  
ax.spines['left'].set_position(('data',0))  
ax.set_yticks([-1,-0.5,0.5,1])  
  
plt.plot(x,y,label="Sigmoid",color = "blue")  
plt.plot(2*x,tanh,label="Tanh", color = "red")  
plt.legend()  
plt.show()  
(tanh(i)+1)/2和sigmoid结果基本是一样的:

# !/usr/bin/python #encoding:utf-8
import math
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import torch

mpl.rcParams['axes.unicode_minus'] = False

def sigmoid(x):
    return 1.0 / (1.0 + np.exp(-x))

fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(111)

x = np.linspace(-10, 10)
y = sigmoid(x)
# tanh = 2 * sigmoid(2 * x) - 1

tanh= (math.e**(x)-math.e**(-x))/(math.e**(x)+math.e**(-x))

for i in range(len(tanh)):
    tanh[i]=(tanh[i]+1)/2

# alpha=0.25
# for i in range(len(tanh)):
#     tanh[i]=max(tanh[i], alpha * tanh[i])

# tanh= max(tanha, alpha * tanha)

plt.xlim(-11, 11)
plt.ylim(-1.1, 1.1)

ax.spines['top'].set_color('none')
ax.spines['right'].set_color('none')

ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data', 0))
ax.set_xticks([-10, -5, 0, 5, 10])
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data', 0))
ax.set_yticks([-1, -0.5, 0.5, 1])

plt.plot(x, y, label="Sigmoid", color="blue")
plt.plot(2 * x, tanh, label="Tanh", color="red")
plt.legend()
plt.show()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AI算法网奇

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

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

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

打赏作者

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

抵扣说明:

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

余额充值