from torch.utils import data import torch definit_linear_data(w, b, nums_example): """模拟一个有噪声的多元线性回归数据""" X = torch.normal(0, 1, (nums_example, len(w))) y = torch.matmul(X, w) + b y = y + torch.normal(0, 0.01, (nums_example, len(w))) return X, torch.reshape(y, (-1, 1))
for epoch inrange(num_epochs): for X, y in data_iter: l = loss(net(X) ,y) optimzer.zero_grad() l.backward() optimzer.step() l = loss(net(features), labels) loss_record.append(float(l)) #print(f'epoch {epoch + 1}, loss {l:f}')