阅读背景:

PyTorch Lecture 04: Back-propagation and Autograd

来源:互联网 
import torch
from torch import nn
from torch.autograd import Variable

x_data = [1.0, 2.0, 3.0]
y_data = [2.0, 4.0, 6.0]
w = Variable(torch.Tensor([1.0]), requires_grad=True)  # Any random value
print(w)


# our model forward pass
def forward(x):
    return x * w


# Loss function
def loss(x, y):
    y_pred = forward(x)
    return (y_pred - y) * (y_pred - y)


# Training loop

for epoch in range(10):
    for x, y in zip(x_data, y_data):
        l = loss(x, y)
        l.backward() # 自动计算
        print("\t grad", x, y, w.grad.data[0])  # 打印 x,y 以及
        w.data = w.data - 0.01 * w.grad.data # 直接使用
        # manually zero the gradients after running the backward pass and update w
        w.grad.data.zero_()  # 一定要置0

        print("progress:", epoch, l.data[0])

# After training

print("predict (after training)", 4, forward(4))import torch
from torch import nn
from to



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: