【发布时间】:2023-03-18 11:06:01
【问题描述】:
我正在尝试使用 torch.autograd.grad 在 PyTorch 中计算矩阵导数,但是我遇到了一些问题。这是重现错误的最小工作示例。
theta = torch.tensor(np.random.uniform(low=-np.pi, high=np.pi), requires_grad=True)
rot_mat = torch.tensor([[torch.cos(theta), torch.sin(theta), 0],
[-torch.sin(theta), torch.cos(theta), 0]],
dtype=torch.float, requires_grad=True)
torch.autograd.grad(outputs=rot_mat,
inputs=theta, grad_outputs=torch.ones_like(rot_mat),
create_graph=True, retain_graph=True)
此代码导致错误“图中似乎未使用其中一个微分张量。如果这是所需的行为,请设置 allow_unused=True。”
我尝试使用 allow_unused=True 但渐变返回为 None。我不确定是什么导致图表在此处断开连接。
【问题讨论】: