python
复制代码
import torch
import numpy as np
from torch.nn import init
from torch.utils import data
from torch import nn
# 数据集
num_inputs = 2
num_examples = 1000
true_w = [2, -3.4]
true_b = 4.2
features = torch.from_numpy(np.random.normal(0, 1, (num_examples, num_inputs))).type(torch.float32) # 1000*2
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
# 将训练数据的特征和标签组合
dataset = data.TensorDataset(features, labels)
# 随机读取⼩批量
data_iter = data.DataLoader(dataset, batch_size, shuffle=True)
# 使用框架预定义好的层
net = nn.Sequential(nn.Linear(2, 1)) # 输入是二维,输出是一维
# 初始化模型参数
# net[0].weight.data.normal_(0, 0.01)
# net[0].bias.data.fill_(0)
init.normal_(net[0].weight, mean=0, std=0.01)
init.constant_(net[0].bias, val=0)
# 计算均方误差使用的是MELoss类,也称为L_2范数
loss = nn.MSELoss()
# 实例化SGD(随机梯度下降)实例
optimizer = torch.optim.SGD(net.parameters(), lr=0.03)
# 训练
num_epochs = 3
l = 0
for epoch in range(1, num_epochs + 1):
for X, y in data_iter:
output = net(X)
l = loss(output, y.view(-1, 1))
optimizer.zero_grad() # 梯度清零,等价于net.zero_grad()
l.backward()
optimizer.step()
print('epoch %d, loss: %f' % (epoch, l.item()))