PyTorch implements simple linear regression

1. Implementation steps

1. Prepare data

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

2. Design model

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. Construct the loss function and optimizer

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

4. Training process

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. Result display

Show the final weights and biases:

# 输出权重和偏置
print('w = ',model.linear.weight.item())
print('b = ',model.linear.bias.item())

The result is:

w =  1.9998501539230347
b =  0.0003405189490877092

Model test:

# 测试模型
x_test = torch.tensor([[4.0]])
y_test = model(x_test)
print('y_pred = ',y_test.data)
y_pred =  tensor([[7.9997]])

Plot the 2D curve of the loss value as a function of the number of iterations and the 3D scatterplot of the loss as the weight and bias change:

# 二维曲线图
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()

The result is shown below:
insert image description hereinsert image description here

2. References

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

Guess you like

Origin blog.csdn.net/weixin_43821559/article/details/123298468