标签:inf mic int als print 分类 ack range backward
import torch
import matplotlib.pylab as plt
import numpy as np
import xiaobei_pytorch as xb
def xyplot(x_vals,y_vals,name):
xb.set_figsize(figsize=(5, 2.5))
xb.plt.plot(x_vals.detach().numpy(), y_vals.detach().numpy())
xb.plt.xlabel(‘x‘)
xb.plt.ylabel(name + ‘(x)‘)
x = torch.arange(-8,8,0.1,requires_grad=True)
y = x.relu()
xyplot(x,y,‘relu(x)‘)
# print(y)
# 只允许标量对张量求导 所以这里将y转换车才能够标量,求和不改变梯度
y.sum().backward()
xyplot(x,x.grad,‘grad of relu‘)
y = x.sigmoid()
xyplot(x,y,‘sigmoid‘)
x.grad.zero_() #先将之前的梯度清零 否则梯度会累加
y.sum().backward()
xyplot(x,x.grad,‘sigmoid grad‘)
y = x.tanh()
xyplot(x,y,‘tanh‘)
x.grad.zero_()
y.sum().backward()
xyplot(x,x.grad,‘tanh grad‘)
标签:inf mic int als print 分类 ack range backward
原文地址:https://www.cnblogs.com/wangxiaobei2019/p/12923075.html