pytorch深度学习实践 第四讲 反向传播

  1.  先导入需要的包torchmatplotlib
import torch
import matplotlib.pyplot as plt
2.导入数据,并定义随机定义w的初始值
然后定义前向传播函数(forward)

   定义损失函数(loss)

x_data = [1, 2, 3.0]
y_data = [2, 4, 6.0]

w = torch.tensor([1.0])
w.requires_grad = True


def forward(x):
    return x * w


def loss(x, y):
    y_pre = forward(x)
    return (y_pre - y) ** 2

3.定义训练过程

首先指定了一个预测输入值x=4的时候,还没有进行优化要输出的值

print('before training', 4, forward(4).item())

注意:因为传播过程中,w和y_hat都是以梯度的方式存放的,forward储存的变量类型是Tensor,所以要通过item()改成标量的形式

选定要epoch的次数,选择100次

sum1 = 0
sum2_list = []
l_list = []

for epoch in range(100):
    sum1 += 1
    for x, y in zip(x_data, y_data):
        l = loss(x, y)
        l.backward()  # 反向传播计算w的梯度
        print('\tgrad:', x, y, w.grad.item())
        w.data = w.data - 0.01 * w.grad.data  # 更新w
        w.grad.data.zero_()  # w置零
    l_list.append(l.item())
    sum2_list.append(sum1)
    print('progress:', epoch, l.item())  # 打印迭代的次数和损失值
print("predict(after training)", 4, forward(4).item())

4.最后进行可视化:

plt.plot(sum2_list,l_list)
plt.xlabel('epoch')
plt.ylabel('loss')
plt.show()

5.结果展示:

progress: 95 9.094947017729282e-13
    grad: 1 2 -7.152557373046875e-07
    grad: 2 4 -2.86102294921875e-06
    grad: 3.0 6.0 -5.7220458984375e-06
progress: 96 9.094947017729282e-13
    grad: 1 2 -7.152557373046875e-07
    grad: 2 4 -2.86102294921875e-06
    grad: 3.0 6.0 -5.7220458984375e-06
progress: 97 9.094947017729282e-13
    grad: 1 2 -7.152557373046875e-07
    grad: 2 4 -2.86102294921875e-06
    grad: 3.0 6.0 -5.7220458984375e-06
progress: 98 9.094947017729282e-13
    grad: 1 2 -7.152557373046875e-07
    grad: 2 4 -2.86102294921875e-06
    grad: 3.0 6.0 -5.7220458984375e-06
progress: 99 9.094947017729282e-13
predict(after training) 4 7.999998569488525

我也是初学者,哪里有问题请各位大佬指正

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值