赞
踩








步骤一:准备数据 y = w x + b y=wx+b y=wx+b
x = torch.rand([50])
y = 3 * x + 0.8
w = torch.rand(1, requires_grad=True)
b = torch.rand(1, requires_grad=True)
步骤二:构造损失函数
def loss_fn(y, y_predict):
loss = (y_predict - y).pow(2).mean()
for i in [w, b]:
# 每次反向传播前把梯度置为0
if i.grad is not None:
i.grad.data.zero_()
# [i.grad.data.zero_() for i in [w,b] if i.grad is not None]
loss.backward()
return loss.data
步骤三: 更新参数
def optimize(learning_rate):
# print(w.grad.data,w.data,b.data)
w.data -= learning_rate * w.grad.data
b.data -= learning_rate * b.grad.data
步骤四:训练参数,计算预测值
for i in range(3000):
# 2. 计算预测值
y_predict = x * w + b
# 3.计算损失,把参数的梯度置为0,进行反向传播
loss = loss_fn(y, y_predict)
if i % 500 == 0:
print(i, loss)
# 4. 更新参数w和b
optimize(0.01)
步骤五:绘制图形,观察训练结束的预测之和真实值
# 绘制图形,观察训练结束的预测值和真实值
predict = x * w + b # 使用训练后的w和b计算预测值
plt.scatter(x.data.numpy(), y.data.numpy(), c="r")
plt.plot(x.data.numpy(), predict.data.numpy())
plt.show()
print("w", w)
print("b", b)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。