pytorch梯度函数的使用方法

import torch
torch.cuda.set_device(0)


t1 = torch.randn(3,3,requires_grad=True) # 定义一个3X3的张量
print(t1)
t2 = t1.pow(2).sum() # 根据t1张量计算t2张量
print(t2)

print(torch.autograd.grad(t2,t1)) # t2张量对t1张量求导

Done!!!

你可能感兴趣的:(pytorch,pytorch)