Pyotrch入门-第4讲

1.刘二大人第4讲

参考图引用于:PyTorch学习(三)--反向传播_陈同学爱吃方便面的博客-CSDN博客_反向传播pytorch

课后答案参考:

Pyotrch入门-第4讲_第1张图片

 代码实现:

 

import torch
def BP_demo2():
    x_data = [1.0, 2.0, 3.0]
    y_data = [2.0, 4.0, 6.0]

    epoch_list = []
    l_list = []

    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 ** 2 + w2 * x + b

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

    print("Predict (beforetraining):", 4, forward(4))

    for epoch in range(100):
        for x, y in zip(x_data, y_data):
            l = loss(x, y)
            l.backward()
            print("\tgrad:", x, y, w1.grad.item(), w2.grad.item(), 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('epoch:', epoch, l.item())

    print("predict(after training)", 4, forward(4).item())
if __name__=="__main__":
    # BP_demo1()
    BP_demo2()

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