1.目标
拟合函数$ f(x) = 5.0x_1+4.0x_2+3.0x_3+3 $
2.理论
和一维线性回归大同小异。
3.实现
3.0 环境
python == 3.6
torch == 1.4
3.1 必要的包
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
3.2 创造数据并转换形式
# f(x)=5x1+4x2+3x3+3
x_train = np.array([[1,3,4],[2,4,2],[7,5,9], [2,5,6], [6,4,2],[8,2,7],[9,3,6],[1,6,8], [5,3,6],[3,7,3]], dtype=np.float32)
y_train = x_train[:,0]*5+x_train[:,1]*4+3*x_train[:,2]+3
y_train = y_train.reshape((10,1))
x_train = torch.from_numpy(x_train)
y_train = torch.from_numpy(y_train)
3.3 构造模型并创建对象
class MultiLinearRegression(nn.Module):
def __init__(self):
super(MultiLinearRegression, self).__init__()
self.linear = nn.Linear(3,1) # 因为3个变量映射1个输出
def forward(self,x):
out = self.linear(x)
return out
model = MultiLinearRegression()
3.4 检查CUDA
if torch.cuda.is_available():
model = model.cuda()
x_train = x_train.cuda()
y_train = y_train.cuda()
3.5 选择优化器
这里使用均方误差,学习率为0.001
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=1e-3)
3.6 开始训练
epoch = 0
while True:
output = model(x_train) # 前向传播
loss = criterion(output, y_train) # 损失计算
loss_value = loss.data.cpu().numpy() # 获取损失值
optimizer.zero_grad() # 梯度置零
loss.backward() # 反向传播
optimizer.step() # 更新梯度
epoch += 1
if epoch % 100 == 0: # 每100步打印一次损失
print('Epoch:{}, loss:{:.6f}'.format(epoch, loss_value))
if loss_value <= 1e-3:
break
3.7 查看结果
w = model.linear.weight.data.cpu().numpy()
b = model.linear.bias.data.cpu().numpy()
print('w:{},b:{}'.format(w,b))
# 结果为
w:[[5.0077577 4.0204782 3.004031 ]],b:[2.851891]
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。