点击查看代码
# -*- coding: utf-8 -*-
# @Author : 钱力
# @Time : 2024/7/27 8:48
import torch
# 标量求导
x = torch.tensor(-2.0, requires_grad=True) # x需要被求导
b = torch.tensor(-2.0)
c = torch.tensor(1.0)
d = torch.pow(x, 2)
e = x * b
y = d + e + c
y.backward(retain_graph=True) # 保留图
print("1次求导后", x.grad) # 求导完只后,图会销毁
# y.backward()
# print("2次求导后",x.grad) #再次求导就会报错
x.grad.zero_() # 梯度清零
y.backward(retain_graph=True)
print("2次求导后", x.grad) # 梯度累加,默认保存之前求导的结果
x.grad.zero_() # 梯度清零
y.backward(retain_graph=True)
print("3次求导后", x.grad)
# 向量/矩阵求导
# 实际上pytorch不支持矩阵对矩阵的求导,只支持对标量的求导
x = torch.tensor([[3.0, 4.0], [1.0, 2.0]], requires_grad=True)
a = torch.tensor(1.0)
b = torch.tensor(-2.0)
c = torch.tensor(1.0)
y = a * torch.pow(x, 2) + b * x + c
# 方法1
y1 = y.mean()
y1.backward(retain_graph=True)
print(x.grad)
x.grad.zero_()
# 方法2
gradient = torch.tensor([[1.0, 1.0], [1.0, 1.0]]) /4
# 用gradient进行加权
y.backward(gradient=gradient)
print(x.grad)