PyTorch - autograd - One of the differentiated Tensors appears to not have been used in the graph

本文探讨了在PyTorch中遇到的梯度计算问题,当输入output与变量b没有直接依赖时,如何通过调整变量和使用torch.autograd.grad正确解析计算图。解决方法强调了梳理计算图关系的重要性,避免NoneType错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考资料

Example for One of the differentiated Tensors appears to not have been used in the graph - #3 by Sudarshan_VB - autograd - PyTorch Forums

allow_unused=True - 知乎

pytorch 踩坑记录_kdh的专栏-优快云博客

python - Pytorch gradient error: nonetype unsupported operand type(s) for +: 'NoneType' and 'NoneType' - Stack Overflow

关于DiffPool和MAML的结合 | Zhimeng's Personal Website | Welcome!

问题描述

计算梯度时,报错。产生原因是,input 和 output可能不是直接关系,即,两个无关的变量自然没有办法求导。

比如有两个计算图,x->y->z 和 y->m, 那么可以求z到x的梯度,但是你求不到 z 到 m的梯度。

例子

a = torch.rand(10, requires_grad=True)
b = torch.rand(10, requires_grad=True)

output = (2 * a).sum()

torch.autograd.grad(output, (a, b))

if b is not in the graph then the derivative is just 0 everywhere. You don’t need to add it to the graph to get the derivatives.

output is not a function of b.

解决方法

网上有不少建议是,设置 allow_unused=True,这个往往无法解决问题。得到的结果会出现一个None,raise新的error如下

TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'

靠谱的解决方法是,重新梳理一下,torch.autograd.grad的两个variables的计算图关系。

 调整合适的variable or tensor,把output 和 x 的函数关系弄对即可。

grad = torch.autograd.grad(output, x)

 

with timing('<rmp2> forward pass', self.timed): # Generalized coordinates, velocities, and curvatures at the leaf nodes # Step 1: Calculate xs xs = self.forward_mapping(q, **features) # 计算 xs # Step 2: Create dummy ones tensor with requires_grad=True for each element in xs dummy_ones = [torch.ones_like(x, requires_grad=True) for x in xs] # 创建全1张量,并启用梯度计算 # Step 3: Calculate sum_x and sum_xd (equivalent to TensorFlow gradient(ggg, sum_x, q)) sum_x = ip(xs, dummy_ones) # 计算 sum_x sum_x.requires_grad_(True) sum_xd = ip(qd, gradient(sum_x, q)[0]) # 计算 sum_xd,这里调用 gradient 来计算 sum_x 关于 q 的梯度 sum_xd.requires_grad_(True) # Ensure sum_xd can compute gradients # Step 4: Calculate grd (gradient of sum_xd with respect to q) grd = gradient(sum_xd, q)[0] grd.requires_grad_(True) # Step 5: Calculate sum_crv (curvature term) sum_crv = ip(qd, grd) # 计算 sum_crv sum_crv.requires_grad_(True) # Ensure sum_crv can compute gradients # Step 6: Compute curvature terms using gradients crvs = gradient(sum_crv, dummy_ones) xds = gradient(sum_xd, dummy_ones)再思考一下 crvs = gradient(sum_crv, dummy_ones) File "E:\pythonproject\RMP\rmp2_pytorch\rmp2\rmpgraph\rmpgraph.py", line 360, in gradient return torch.autograd.grad(target, sources, create_graph=True, retain_graph=True) File "C:\Users\11615\.conda\envs\rmp2rl\lib\site-packages\torch\autograd\__init__.py", line 302, in grad allow_unused, accumulate_grad=False) # Calls into the C++ engine to run the backward pass RuntimeError: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior.思考一下,为什么出错
最新发布
03-19
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值