torch.autograd.grad grad_outputs使用示例,batch批量求导
# %%
import torch
from torch import autograd
import torchvision
resnet = torchvision.models.resnet.resnet18()
convs = torch.nn.Sequential(*(list(resnet.children())[:-1]))
x1 = torch.randn(64, 3, 100, 200).requires_grad_()
y1 = convs(x1)
x2 = torch.randn(64, 3, 100, 200).requires_grad_()
y2 = convs(x2)
y = torch.cat([y1,y2],dim = 0)
x = torch.cat([x1,x2],dim = 0)
print(y.shape)
print(x.shape)
grad = torch.autograd.grad(inputs= x,outputs= y, grad_outputs=torch.ones_like(y),allow_unused=True)
grad
"""
torch.Size([128, 512, 1, 1])
torch.Size([128, 3, 100, 200])
(None,)
"""
x1 = torch.randn(64, 3, 100, 200).requires_grad_()
y1 = convs(x1)
x2 = torch.randn(64, 3, 100, 200).requires_grad_()
y2 = convs(x2)
# 一个标量能对一个向量求导数吗
# outputs (sequence of Tensor) – outputs of the differentiated function.
# inputs (sequence of Tensor) – Inputs w.r.t. which the gradient will be returned (and not accumulated into .grad).
grad = torch.autograd.grad(inputs= (x1,x2),outputs= (y1,y2), grad_outputs=[torch.ones_like(y1),torch.ones_like(y2)])
print(len(grad[0]))
# 输出也是一个sequence
print(grad[0].shape)
print(grad[1].shape)
"""
64
torch.Size([64, 3, 100, 200])
torch.Size([64, 3, 100, 200])
"""
本博文本意在于记录个人的思考与经验,部分博文采用英语写作,可能影响可读性,请见谅
本文来自博客园,作者:ZXYFrank,转载请注明原文链接:https://www.cnblogs.com/zxyfrank/p/17043098.html