1.目标
拟合函数 $ f(x)=2x_{1}^{3}+3x_2^2+4x_3+0.5 $
2.理论
3.实现
3.1 环境
python == 3.6
torch == 1.4
3.2 构造数据
# 这个是目标权重和偏置
w = torch.FloatTensor([2.0, 3.0, 4.0]).unsqueeze(1)
b = torch.FloatTensor([0.5])
def create_data(batch_size=32):
random = torch.randn(batch_size)
random = random.unsqueeze(1) # 添加一个维度
# 纵向连接tensor
x = torch.cat([random**i for i in range(1,4)], 1)
# 矩阵乘法
y = x.mm(w) + b[0]
if torch.cuda.is_available():
return x.cuda(), y.cuda()
return x, y
3.3 构造模型并创建对象
class PloyRegression(nn.Module):
def __init__(self):
super(PloyRegression, self).__init__()
self.ploy = nn.Linear(3,1)
def forward(self, x):
out = self.ploy(x)
return out
model = PloyRegression()
if torch.cuda.is_available():
model = model.cuda()
3.4 选择优化器
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=1e-3)
这里使用的是均方误差,使用随机梯度下降,学习率为0.001.
3.5 开始训练
epoch = 0
while True:
# 创建数据
batch_x, batch_y = create_data()
# 前向传播
output = model(batch_x)
# 损失计算
loss = criterion(output, batch_y)
# 获取损失值
loss_value = loss.data.cpu().numpy()
# 梯度置零
optimizer.zero_grad()
# 反向传播
loss.backward()
# 更新参数
optimizer.step()
epoch += 1
if loss_value < 1e-3:
break
# 每100步打印一次损失
if (epoch+1)%100==0:
print("Epoch{}, loss:{:.6f}".format(epoch+1, loss_value))
3.6 验证结果
model.eval() # 开启验证模式
# 构造数据
x_train = np.array([[2.167],[3.1],[3.3],[4.168],[4.4],[5.313],[5.5],[6.182],[6.7],[6.9],[7.042],[7.59],[7.997],[9.779],[10.791]], dtype=np.float32)
x_train = torch.from_numpy(x_train)
x = torch.cat([x_train**i for i in range(1,4)], 1)
y = x.mm(w) + b
# 绘制数据点
plt.plot(x_train.numpy(),y.numpy(),'ro')
# 提取拟合参数
w_get = model.ploy.weight.data.T
b_get = model.ploy.bias.data
print('w:{},b:{}'.format(w_get.cpu().numpy(), b_get.cpu().numpy()))
# 计算预测值
Y_get = x.mm(w_get.cpu()) + b_get.cpu()
plt.plot(x_train.numpy(), Y_get.numpy(), '-')
plt.show()
# print:w:[[1.9365442],[2.9985998],[4.012949 ]],b:[0.5068841]
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。