☰
激活函数activation function
2026/10/24 7:16:25 网站建设 项目流程

#激活函数

%matplotlib inline

import torch

from d2l import torch as d2l

#ReLU函数

x=torch.arange(-8.0,8.0,0.1,requires_grad=True)

y=torch.relu(x)

d2l.plot(x.detach(),y.detach(),'x','relu(x)',figsize=(5,2.5))

#ReLU函数的导数

y.backward(torch.ones_like(x),retain_graph=True)

d2l.plot(x.detach(),x.grad,'x','grad of relu',figsize=(5,2.5))

#sigmoid函数

y=torch.sigmoid(x)

d2l.plot(x.detach(),y.detach(),'x','sigmoid(x)',figsize=(5,2.5))

#sigmoid函数的导数

#清除以前的梯度

x.grad.data.zero_()

y.backward(torch.ones_like(x),retain_graph=True)

d2l.plot(x.detach(),x.grad,'x','graph of sigmoid',figsize=(5,2.5))

#tanh函数

y=torch.tanh(x)

d2l.plot(x.detach(),y.detach(),'x','tanh(x)',figsize=(5,2.5))

#tanh函数的导数

#清除以前的梯度

x.grad.data.zero_()

y.backward(torch.ones_like(x),retain_graph=True)

d2l.plot(x.detach(),x.grad,'x','graph of tanh',figsize=(5,2.5))

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询