Loading

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])
"""
posted @ 2023-01-11 10:41  ZXYFrank  阅读(168)  评论(0编辑  收藏  举报