一、源码
import sys
import numpy as np
sys.path.append('..')
from d2lzh_pytorch.utils import *
num_input = 2
num_example = 1000
true_w = [2, -3.4]
true_b = 4.2
features = torch.from_numpy(np.random.normal(0, 1, (num_example, num_input)))
labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_b
labels += torch.from_numpy(np.random.normal(0, 0.01, size=labels.size()))
batch_size = 10
w = torch.tensor(np.random.normal(0, 0.01, (num_input, 1)), dtype=torch.float64)
b = torch.zeros(1, dtype=torch.float64)
w.requires_grad_(True)
b.requires_grad_(True)
lr = 0.03
num_epochs = 3
net = linreg
loss = squared_loss
for epoch in range(num_epochs):
for X, y in data_iter(batch_size, features, labels):
l = loss(net(X, w, b), y).sum()
l.backward()
sgd([w, b], lr, batch_size)
w.grad.data.zero_()
b.grad.data.zero_()
train_l = loss(net(features, w, b), labels)
print('epoch %d, loss %f' % (epoch + 1, train_l.mean().item()))
def data_iter(batch_size, features, labels):
num_examples = len(features)
indices = list(range(num_examples))
random.shuffle(indices)
for i in range(0, num_examples, batch_size):
j = torch.LongTensor(indices[i: min(i + batch_size, num_examples)])
yield features.index_select(0, j), labels.index_select(0, j)
def linreg(X, w, b):
return torch.mm(X, w) + b
def squared_loss(y_hat, y):
return (y_hat - y.view(y_hat.size())) ** 2 / 2
def sgd(params, lr, batch_size):
for param in params:
param.data -= lr * param.grad / batch_size
二、结果
epoch 1, loss 0.039660
epoch 2, loss 0.000145
epoch 3, loss 0.000048
三、坑
1、报错
Traceback (most recent call last):
File "D:/pycharm/test.py", line 34, in <module>
l.backwards()
AttributeError: 'Tensor' object has no attribute 'backwards'
2、原因
- 把l.backward()写成了l.backwards()报错。