时间:2022-08-14 09:29:56 | 栏目:Python代码 | 点击:次
x_data = torch.tensor([[1.0],[2.0],[3.0]]) y_data = torch.tensor([[2.0],[4.0],[6.0]])
class LinearModel(torch.nn.Module): def __init__(self): super(LinearModel,self).__init__() self.linear = torch.nn.Linear(1,1) def forward(self, x): y_pred = self.linear(x) return y_pred model = LinearModel()
criterion = torch.nn.MSELoss(reduction='sum') optimizer = torch.optim.SGD(model.parameters(),lr=0.01)
epoch_list = [] loss_list = [] w_list = [] b_list = [] for epoch in range(1000): y_pred = model(x_data) # 计算预测值 loss = criterion(y_pred, y_data) # 计算损失 print(epoch,loss) epoch_list.append(epoch) loss_list.append(loss.data.item()) w_list.append(model.linear.weight.item()) b_list.append(model.linear.bias.item()) optimizer.zero_grad() # 梯度归零 loss.backward() # 反向传播 optimizer.step() # 更新
展示最终的权重和偏置:
# 输出权重和偏置 print('w = ',model.linear.weight.item()) print('b = ',model.linear.bias.item())
结果为:
w = 1.9998501539230347
b = 0.0003405189490877092
模型测试:
# 测试模型 x_test = torch.tensor([[4.0]]) y_test = model(x_test) print('y_pred = ',y_test.data) y_pred = tensor([[7.9997]])
分别绘制损失值随迭代次数变化的二维曲线图和其随权重与偏置变化的三维散点图:
# 二维曲线图 plt.plot(epoch_list,loss_list,'b') plt.xlabel('epoch') plt.ylabel('loss') plt.show() # 三维散点图 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(w_list,b_list,loss_list,c='r') #设置坐标轴 ax.set_xlabel('weight') ax.set_ylabel('bias') ax.set_zlabel('loss') plt.show()
结果如下图所示: