绘制激活函数及其导数(pytorch)(基础版)

基本的激活函数及其导数绘图(导数部分使用pytorch的自动求导)

本文包括的激活函数有:
1.sigmoid
2.tanh
3.relu
4.leakyrelu
5.elu

准备绘图函数
%matplotlib inline
import torch
import numpy as np
import matplotlib.pylab as plt
import sys
sys.path.append("..") 
from matplotlib import pyplot as plt
from IPython import display
from matplotlib import style

def xyplot(x_vals, y_vals, name):
    display.set_matplotlib_formats('svg')
    plt.rcParams['figure.figsize'] = (5, 3.5)
    plt.plot(x_vals.detach().numpy(), y_vals.detach().numpy(), label = name, linewidth=1.5, color='#FF0000')
    plt.grid(True,linestyle=':')   
    plt.legend(loc='upper left') 
    #dark_background, seaborn, ggplot
    plt.style.use("seaborn")
    ax = plt.gca()
    ax.spines['right'].set_color("none")
    ax.spines['top'].set_color("none")
    ax.spines['bottom'].set_position(("data",0))  
    ax.spines['left'].set_position(("data",0)) 
    ax.spines['bottom'].set_linewidth(0.5)
    ax.spines['left'].set_linewidth(0.5)
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')
(1)sigmoid
#sigmoid激活函数
x = torch.arange(-6.0, 6.0, 0.1, requires_grad=True)
y = x.sigmoid()
xyplot(x, y, 'sigmoid')

在这里插入图片描述

#导数
y.sum().backward()
xyplot(x, x.grad, 'grad of logistic')

在这里插入图片描述

(2)tanh
#tanh激活函数
x = torch.arange(-6.0, 6.0, 0.1, requires_grad=True)
y = x.tanh()
xyplot(x, y, 'tanh')

在这里插入图片描述

#导数
y.sum().backward()
xyplot(x, x.grad, 'grad of tanh')

在这里插入图片描述

(3)relu
#relu激活函数
x = torch.arange(-6.0, 6.0, 0.01, requires_grad=True)
y = x.relu()
xyplot(x, y, 'relu')

在这里插入图片描述

#导数
y.sum().backward()
xyplot(x, x.grad, 'grad of relu')

在这里插入图片描述

(4)leakyrelu
#leakyrelu激活函数
import torch.nn.functional as F
x = torch.arange(-6.0, 6.0, 0.1, requires_grad=True)
y = F.leaky_relu(x, negative_slope=0.1, inplace=False)
xyplot(x, y, 'leakyrelu(γ=0.1)')

在这里插入图片描述

#导数
y.sum().backward()
xyplot(x, x.grad, 'grad of leakyrelu')

在这里插入图片描述

(5)elu
#elu激活函数
import torch.nn.functional as F
x = torch.arange(-6.0, 6.0, 0.01, requires_grad=True)
y = F.elu(x, alpha=0.3, inplace=False)
xyplot(x, y, 'elu(γ=0.3)')

在这里插入图片描述

#导数
y.sum().backward()
xyplot(x, x.grad, 'grad of elu(γ=0.3)')

在这里插入图片描述

  • 6
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值