两层计算图
反向传播
import numpy as np
import matplotlib.pyplot as plt
import torch
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_pred=forward(x)
return (y_pred-y)**2
print('Prediciton(before training)',4,forward(4).item())# 直接拿出来,变成一个标量
for epoch in range(100):
for x,y in zip(x_data,y_data):
l=loss(x,y) # forward
l.backward() # 计算 .requires_grad=True 的梯度
print('\tgrad:',x,y,w.grad.item())
w.data=w.data-0.01*w.grad.data # utilize梯度来更新w,只是修改数值,用data
w.grad.data.zero_() # 梯度需要清零,否则会累加
print('progress:',epoch,'loss=',l.item())
print('Prediciton(before training)',4,forward(4).item())
exercise
import numpy as np
import matplotlib.pyplot as plt
import torch
x_data = [1.0,2.0,3.0]
y_data = [2.0,4.0,6.0]
w1 = torch.Tensor([1.0])#初始权值
w1.requires_grad = True
w2 = torch.Tensor([1.0])
w2.requires_grad = True
b = torch.Tensor([1.0])
b.requires_grad = True
def forward(x):
return w1*x*x+w2*x+b
def loss(x,y):
y_pred=forward(x)
return (y_pred-y)**2
print('Prediciton(before training)',4,forward(4).item())# 直接拿出来,变成一个标量
for epoch in range(10000):
for x,y in zip(x_data,y_data):
l=loss(x,y) # forward
l.backward() # 计算 .requires_grad=True 的梯度
print('\tx:',x,'y:',y,'w1.grad:',w1.grad.item(),'w2.grad:',w2.grad.item(),'b.grad:',b.grad.item())
w1.data=w1.data-0.01*w1.grad.data # utilize梯度来更新w,只是修改数值,用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,'loss=',l.item())
print('Prediciton(before training)',4,forward(4).item())
次数 | Prediction |
---|---|
500 | 8.43 |
1000 | 8.33 |
5000 | 8.03 |
10000 | 8.00 |