PyTorch实现简单的线性回归

一、实现步骤

1、准备数据

x_data = torch.tensor([[1.0],[2.0],[3.0]])
y_data = torch.tensor([[2.0],[4.0],[6.0]])

2、设计模型

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()  

3、构造损失函数和优化器

criterion = torch.nn.MSELoss(reduction='sum')
optimizer = torch.optim.SGD(model.parameters(),lr=0.01)

4、训练过程

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()        # 更新

5、结果展示

展示最终的权重和偏置:

# 输出权重和偏置
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()

结果如下图所示:
在这里插入图片描述在这里插入图片描述

二、参考文献

[1] https://www.bilibili.com/video/BV1Y7411d7Ys?p=5

猜你喜欢

转载自blog.csdn.net/weixin_43821559/article/details/123298468