import torch
import numpy as np
import random
import torch.utils.data as Data
import torch.nn as nn
from torch.nn import init
import torch.optim as optim
num_inputs = 3
num_examples = 2000
features = torch.normal(0, 1, (num_examples, num_inputs))
true_w = torch.tensor([1.0, 2.3, 4.5]).view(-1, 1)
true_b = 3.4
labels = torch.mm(features, true_w) + true_b # 这样可以略过改形状的步骤
labels += torch.normal(0, 0.01, size=labels.size())
# print(y.shape)
dataset = Data.TensorDataset(features, labels)
# print(dataset[:10])
data_item = Data.DataLoader(dataset, 10, shuffle=True)
# for x,y in data_item:
# print(x,y)
net = nn.Sequential(nn.Linear(3, 1))
# print(net[0])
# print(net[0].weight,net[0].bias)
init.normal_(net[0].weight, mean=0, std=0.01)
init.constant_(net[0].bias, 0)
# print(net[0].weight,net[0].bias)
y_hat = net(features)
loss = nn.MSELoss()
optimzer = optim.SGD(net.parameters(), lr=0.02)
empochs = 3
for i in range(empochs):
for x, y in data_item:
l = loss(labels, y_hat)
optimzer.zero_grad()
l.backward()
optimzer.step()
print('epoch %d ,loss: %f' % (i, l.item()))
print( net[0].weight,net[0].bias)
print(true_w,true_b)
报错:
Trying to backward through the graph a second time (or directly access saved variables after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved variables after calling backward.
修改方法:
把y_hat=net(features)改成y_hat=net(features)放在每一批次的循环里面。放在外面相当于每一次都是对同一个output进行backward()操作。
这样不会报语法上的错误,但实际上真正的错误并没有改正。
应该是y_hat=net(x)
l=loss(y_hat,y)
才能实现真正的小批量随机梯度下降。