4.1.0 头文件

import torch
from d2l import torch as d2l
from matplotlib import pyplot as plt

 

4.1.1 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))
plt.savefig('Relu.png')

 

 

4.1.2 sigmoid激活函数

y = torch.sigmoid(x)
d2l.plot(x.detach(), y.detach(), 'x', 'sigmoid(x)', figsize=(5, 2.5))
plt.savefig('sigmoid.png')

 

 

4.1.3 tanh激活函数

y = torch.tanh(x)
d2l.plot(x.detach(), y.detach(), 'x', 'tanh(x)', figsize=(5, 2.5))
plt.savefig('tanh.png')

 

本小节的完整代码如下所示

import torch
from d2l import torch as d2l
from matplotlib import pyplot as plt

# ------------------------------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))
plt.savefig('Relu.png')

# ------------------------------sigmoid激活函数------------------------------------

y = torch.sigmoid(x)
d2l.plot(x.detach(), y.detach(), 'x', 'sigmoid(x)', figsize=(5, 2.5))
plt.savefig('sigmoid.png')

# ------------------------------tanh激活函数------------------------------------

y = torch.tanh(x)
d2l.plot(x.detach(), y.detach(), 'x', 'tanh(x)', figsize=(5, 2.5))
plt.savefig('tanh.png')

 

posted on 2022-11-03 23:06  yc-limitless  阅读(54)  评论(0编辑  收藏  举报