PyTorch深度学习实践-反向传播

PyTorch深度学习实践-反向传播
视频链接(刘二大人):https://www.bilibili.com/video/BV1Y7411d7Ys
代码实现:

# Tensor 用于存数值
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 默认需要计算梯度
# (计算梯度)
w.requires_grad = True

def forward(x):
    # w 是Tensor,此时*是Tensor与Tensor之间的数乘
    # 自动将x转换成Tensor
    return x * w

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

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)
        l.backward()
        # w.grad 求梯度
        print('\tgrad:', x, y, w.grad.item())
        w.data = w.data - 0.01 * w.grad.data

        w.grad.data.zero_()
    print("progress:", epoch, l.item())

print("predict (after training)", 4, forward(4).item())

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