出自B站up主 刘二大人04.反向传播_哔哩哔哩_bilibili
.data和.item的区别
在于.data是数值在张量之间内操作
而.item是将张量的数值提取出来
代码如下:
import torch
import matplotlib.pyplot as plt
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
w = torch.Tensor([1.0])
w.requires_grad = True
def forward(x):
return x * w
def loss(x, y):
y_pre = forward(x)
return (y_pre - y) ** 2
l_list = []
print("predict (before training)", 4, forward(4).item())
for epoch in range(100):
for x, y in zip(x_data, y_data):
l = loss(x, y) # FP
l.backward() # BP
print('\tgrad:', x, y, w.grad.item(),l.item())
w.data = w.data - 0.01 * w.grad.data
w.grad.data.zero_()
print("progress:", epoch, l.item())
l_list.append(l.item())
print("predict (after training)", 4, forward(4).item())
plt.plot(l_list)
plt.xlabel("epoch")
plt.ylabel("loss")
plt.show()
结果:
predict (before training) 4 4.0
grad: 1.0 2.0 -2.0 1.0
grad: 2.0 4.0 -7.840000152587891 3.841600179672241
grad: 3.0 6.0 -16.228801727294922 7.315943717956543
progress: 0 7.315943717956543
grad: 1.0 2.0 -1.478623867034912 0.5465821623802185
grad: 2.0 4.0 -5.796205520629883 2.099749803543091
grad: 3.0 6.0 -11.998146057128906 3.9987640380859375
progress: 1 3.9987640380859375
grad: 1.0 2.0 -1.0931644439697266 0.2987521290779114
grad: 2.0 4.0 -4.285204887390137 1.1476863622665405
grad: 3.0 6.0 -8.870372772216797 2.1856532096862793
progress: 2 2.1856532096862793
grad: 1.0 2.0 -0.8081896305084229 0.16329261660575867
grad: 2.0 4.0 -3.1681032180786133 0.6273048520088196
grad: 3.0 6.0 -6.557973861694336 1.1946394443511963
progress: 3 1.1946394443511963
grad: 1.0 2.0 -0.5975041389465332 0.08925279974937439
grad: 2.0 4.0 -2.3422164916992188 0.34287363290786743
grad: 3.0 6.0 -4.848389625549316 0.6529689431190491
progress: 4 0.6529689431190491
grad: 1.0 2.0 -0.4417421817779541 0.048784039914608
grad: 2.0 4.0 -1.7316293716430664 0.18740876019001007
grad: 3.0 6.0 -3.58447265625 0.35690122842788696
progress: 5 0.35690122842788696
grad: 1.0 2.0 -0.3265852928161621 0.02666448801755905
grad: 2.0 4.0 -1.2802143096923828 0.10243429243564606
grad: 3.0 6.0 -2.650045394897461 0.195076122879982
progress: 6 0.195076122879982
效果图:
up主出的问题:增加权重,来进行前馈和反馈运算,并输出MSE(平均平方误差)随epoch(训练轮次的) 的变化效果图
自己敲的,参考借鉴,如果理解有偏差,还望指正
import torch
import matplotlib.pyplot as plt
x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
w1 = torch.Tensor([1.0])
w2 = torch.Tensor([1.0])
b = torch.Tensor([1.0])
w1.requires_grad = True
w2.requires_grad = True
b.requires_grad = True
def forward(x):
return (w1*(x * x)) + (w2 * x) + b
def loss(x, y):
y_pre = forward(x)
return (y_pre - y) ** 2
l_list = []
print("predict (before training)", 4, forward(4).item())
for epoch in range(50):
for x, y in zip(x_data, y_data):
l = loss(x, y) # FP
l.backward() # BP
print('\tgrad:', x, y, w1.grad.item(),w2.grad.item(),b.grad.item(),l.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, l.item())
l_list.append(l.item())
print("predict (after training)", 4, forward(4).item(),w1.item(),w2.item(),b.item())
plt.plot(l_list)
plt.xlabel("epoch")
plt.ylabel("loss")
plt.show()