Pytorch深度学习实践-刘二大人-反向传播demo

import matplotlib.pyplot as plt
import torch

# y = w*x
x_data = [1, 1.8, 2.5, 3.0]
y_data = [2, 4, 6.9, 7.5]

loss_list = []
w = torch.Tensor([0.5])
w.requires_grad = True


def forward(x):
    return x * w        #W是tensor,tensor的运算后是建立计算图


def loss(x, y):
    y_pre = forward(x)
    return (y-y_pre)**2


for epoch in range(100):
    for x, y in zip(x_data, y_data):
        l = loss(x, y)      #建立计算图
        l.backward()        #每次调用backward后,计算图消失
        print('\tgrad:', x, y, w.grad.item())
        w.data = w.data - 0.1 * w.grad.data  #W是tensor,.data属性和W.grad.data也是tensor,但其不建立计算图

        w.grad.data.zero_()     #防止w的梯度累加起来,因此需要清空

    loss_list.append(l.item()) #item()将tensor转换成标量
    print('progress:', epoch, w.item(), l.item())
print("predict (after training)", 4, forward(4).item())

plt.plot(loss_list)
plt.ylabel('loss')
plt.show()

Pytorch深度学习实践-刘二大人-反向传播demo_第1张图片 

 

你可能感兴趣的:(python,深度学习,pytorch)