Pytorch(5)-----梯度计算

一、问题

如何使用Pytorch计算样本张量的基本梯度呢?考虑一个样本数据集,且有两个展示变量,在给定初始权重的基础上,如何在每次迭代中计算梯度呢?

二、如何运行

假设有x_data 和 y_data 列表,计算两个列表需要计算损失函数,一个forward通道以及一个循环中的训练。

forward函数计算权重矩阵和输入张量的乘积。

python 复制代码
from torch import FloatTensor
from torch.autograd import Variable  # 引入Variable方法是为了计算变量的梯度
a = Variable(FloatTensor([5]))
weights = [Variable(FloatTensor([i]), requires_grad=True) for i in (12, 53, 91, 73)]

w1, w2, w3, w4 = weights  #权重赋值
b = w1 * a
c = w2 * a
d = w3 * b + w4 * c
Loss = (10 - d)
Loss.backward() #从loss 开始反向传播


for index, weight in enumerate(weights, start=1):
    gradient, *_ = weight.grad.data  #取出梯度
    print(f"Gradient of w{index} w.r.t to Loss: {gradient}")

Gradient of w1 w.r.t to Loss: -455.0
Gradient of w2 w.r.t to Loss: -365.0
Gradient of w3 w.r.t to Loss: -60.0
Gradient of w4 w.r.t to Loss: -265.0

# 使用forward
def forward(x):
    return x * w  #forwar过程

import torch
from torch.autograd import Variable
x_data = [11.0, 22.0, 33.0]
y_data = [21.0, 14.0, 64.0]

w = Variable(torch.Tensor([1.0]), requires_grad=True) # 初始化为任意值;

# 训练前打印
print("predict (before training)", 4, forward(4).data[0])
# 定义损失函数
def loss(x, y):
    y_pred = forward(x)
    return (y_pred - y) * (y_pred - y)
#运行训练循环
for epoch in range(10):
    for x_val, y_val in zip(x_data, y_data):
        l = loss(x_val, y_val)
        l.backward()
        print("\tgrad: ", x_val, y_val, w.grad.data[0])
        w.data = w.data - 0.01 * w.grad.data
        # 训练后,人工设置梯度为0,否则梯度会累加;
        w.grad.data.zero_()


     print("progress:", epoch, l.data[0])

#结果
grad: 11.0 21.0 tensor(-220.)
grad: 22.0 14.0 tensor(2481.6001)
grad: 33.0 64.0 tensor(-51303.6484)

progress: 0 tensor(604238.8125)
progress: 1 ................................................
..........................................................................................
#训练后的预测 权重已更新
print("predict (after training)", 4, forward(4).data[0])

#结果
predict (after training) 4 tensor(-9.2687e+24)

下面的程序展示了如何用Variable 变量从损失函数计算梯度:

python 复制代码
a = Variable(FloatTensor([5]))
weights = [Variable(FloatTensor([i]), requires_grad=True) for i in (12, 53, 91, 73)]
w1, w2, w3, w4 = weights
b = w1 * a
c = w2 * a
d = w3 * b + w4 * c
Loss = (10 - d)
Loss.backward()
相关推荐
魁首29 分钟前
初识 ACP (Agent Client Protocol)
人工智能·ai编程·mcp
周末程序猿33 分钟前
开源项目|不一样的思维导图
人工智能·后端
鄃鳕1 小时前
装饰器【Python】
开发语言·python·数码相机
Wgrape1 小时前
一文了解常见AI搜索方案的代码实现
人工智能·后端
m0_528489251 小时前
Pycharm修改系统缓存路径(包含config, system, plugins, logs, remote sources等)
ide·python·pycharm·c盘
中医正骨葛大夫1 小时前
一文解决如何在Pycharm中创建cuda深度学习环境?
pytorch·深度学习·pycharm·软件安装·cuda·anaconda·配置环境
Vadaski1 小时前
私有 Context 工程如何落地:从方法论到实战
人工智能·程序员
Franklin1 小时前
AI Coding 基础实践03 - Trae AI在Pycharm中的使用02 - 读取不了项目的文件问题解决
ide·python·pycharm
胖墩会武术1 小时前
【OpenCV图像处理】深度学习:cv2.dnn() —— 图像分类、人脸检测、目标检测
图像处理·pytorch·python·opencv
帮帮志1 小时前
PyCharm 开发工具 不同的模式
ide·python·pycharm