1. 模型说明
用PyTorch实现线性回归比较简单,主要是调用内置函数
nn.Linear(input_size, output_size),然后使用自动微分和随机梯度下降SGD来更新模型参数。
2. 相关代码
具体代码实现如下:
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt # plot tool
# Hyper -parameters
input_size = 1
output_size = 1
num_epochs = 60 # train time
learning_rate = 0.001
# Toy dataset
x_train = np.array([[3.3], [4.4], [5.5], [6.71], [6.93], [4.168],
[9.779], [6.182], [7.59], [2.167], [7.042],
[10.791], [5.313], [7.997], [3.1]], dtype=np.float32)
y_train = np.array([[1.7], [2.76], [2.09], [3.19], [1.694], [1.573],
[3.366], [2.596], [2.53], [1.221], [2.827],
[3.465], [1.65], [2.904], [1.3]], dtype=np.float32)
# Linear regression model
model = nn.Linear(input_size, output_size)
# Loss and optimizer
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(
model.parameters(),
lr = learning_rate
)
# Train the model
for epoch in range(num_epochs):
# Convert numpy arrays to torch tensors
inputs = torch.from_numpy(x_train)
targets = torch.from_numpy(y_train)
# Forward pass
outputs = model(inputs)
loss = criterion(outputs, targets)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch+1)%5 == 0:
print ('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))
# Plot the graph
predicted = model(torch.from_numpy(x_train)).detach().numpy()
plt.plot(x_train, y_train, 'ro', label='Original data')
plt.plot(x_train, predicted, label='Fitted line')
plt.legend()
plt.show()
# Save the model checkpoint
torch.save(model.state_dict()), 'model.ckpt'
3. 输出结果
每5 epochs输出一次Loss,结果如下:
Epoch [5/60], Loss: 9.8310
Epoch [10/60], Loss: 4.1846
Epoch [15/60], Loss: 1.8968
Epoch [20/60], Loss: 0.9698
Epoch [25/60], Loss: 0.5940
Epoch [30/60], Loss: 0.4414
Epoch [35/60], Loss: 0.3794
Epoch [40/60], Loss: 0.3540
Epoch [45/60], Loss: 0.3435
Epoch [50/60], Loss: 0.3389
Epoch [55/60], Loss: 0.3369
Epoch [60/60], Loss: 0.3358
这里还用matplotlib.pyplot进行绘图,得到数据点和拟合曲线的对照情况。