欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

刘二大人《Pyorch深度学习实践》第4讲反向传播

时间:2023-05-24

指路☞ 《PyTorch深度学习实践》完结合集_哔哩哔哩_bilibili

知识补充:

1、从左到右是前向,从右到左是反向传播

 ********************************************************************************************

import torchx_data = [1.0, 2.0, 3.0]y_data = [2.0, 4.0, 6.0]#tensor张量w = torch.Tensor([1.0])#计算梯度,默认不计算w.requires_grad = Truedef forward(x): return x*wdef loss(x, y): y_p = forward(x) return (y_p - y)**2print("before training", 4, forward(4).item())for epoch in range(100): for x, y in zip(x_data, y_data): l = loss(x, y) l.backward() """ backward把这个链路上之前所有的梯度都求出来, 把梯度存到w里,计算图就没有了,下一次会出现新的计算图 """ print('tgrad:', x, y, w.grad.item()) #item把值取出来,变成一个标量 w.data = w.data - 0.01*w.grad.data """ grad也是Tensor,如果直接用grad,是重新建立计算图 """ w.grad.data.zero_() #清零 print('progress:', epoch, l.item())print("after training", 4, forward(4).item())

部分运行结果:

progress: 89 9.094947017729282e-13grad: 1.0 2.0 -7.152557373046875e-07grad: 2.0 4.0 -2.86102294921875e-06grad: 3.0 6.0 -5.7220458984375e-06progress: 90 9.094947017729282e-13grad: 1.0 2.0 -7.152557373046875e-07grad: 2.0 4.0 -2.86102294921875e-06grad: 3.0 6.0 -5.7220458984375e-06progress: 91 9.094947017729282e-13grad: 1.0 2.0 -7.152557373046875e-07grad: 2.0 4.0 -2.86102294921875e-06grad: 3.0 6.0 -5.7220458984375e-06progress: 92 9.094947017729282e-13grad: 1.0 2.0 -7.152557373046875e-07grad: 2.0 4.0 -2.86102294921875e-06grad: 3.0 6.0 -5.7220458984375e-06progress: 93 9.094947017729282e-13grad: 1.0 2.0 -7.152557373046875e-07grad: 2.0 4.0 -2.86102294921875e-06grad: 3.0 6.0 -5.7220458984375e-06progress: 94 9.094947017729282e-13grad: 1.0 2.0 -7.152557373046875e-07grad: 2.0 4.0 -2.86102294921875e-06grad: 3.0 6.0 -5.7220458984375e-06progress: 95 9.094947017729282e-13grad: 1.0 2.0 -7.152557373046875e-07grad: 2.0 4.0 -2.86102294921875e-06grad: 3.0 6.0 -5.7220458984375e-06progress: 96 9.094947017729282e-13grad: 1.0 2.0 -7.152557373046875e-07grad: 2.0 4.0 -2.86102294921875e-06grad: 3.0 6.0 -5.7220458984375e-06progress: 97 9.094947017729282e-13grad: 1.0 2.0 -7.152557373046875e-07grad: 2.0 4.0 -2.86102294921875e-06grad: 3.0 6.0 -5.7220458984375e-06progress: 98 9.094947017729282e-13grad: 1.0 2.0 -7.152557373046875e-07grad: 2.0 4.0 -2.86102294921875e-06grad: 3.0 6.0 -5.7220458984375e-06progress: 99 9.094947017729282e-13after training 4 7.999998569488525Process finished with exit code 0

 ****************************************************************************************************

设函数为y=x^2+x+1

import torchx_data = [1.0, 2.0, 3.0]y_data = [4.0, 9.0, 16.0]w1 = torch.tensor([1.0])w1.requires_grad = Truew2 = torch.tensor([1.0])w2.requires_grad = Trueb = torch.tensor([1.0])b.requires_grad = Truedef forward(x): return x * x * w1 + x * w2 + bdef loss(x, y): y_p = forward(x) return (y_p - y)**2print("before training", 4, forward(4).item())for epoch in range(6000): for x, y in zip(x_data, y_data): l = loss(x, y) l.backward() print("tgrad", x, y, '{:.6f}'.format(w1.grad.item()), '{:.6f}'.format(w2.grad.item()), '{:.6f}'.format(b.grad.item())) w1.data = w1.data - 0.01 * w1.grad.data w2.data = w2.data - 0.01 * w2.grad.data b.data = b.data - 0.01 * b.grad.data w1.grad.data.zero_() w2.grad.data.zero_() b.grad.data.zero_() print("progress:", epoch, '{:.6f}'.format(l.item()))print("after training", 4, forward(4).item())

部分运行结果:

  

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。