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.
上述错误是在运行下面语句时发生的
grad = torch.autograd.grad(loss, self.net.parameters())
网上搜到的两种解释是:
1.loss与self.net.parameters()中的参数不相关(net是自己定义的网络)
2.有参数已经计算过梯度,但还未在计算图中使用
(Set allow_unused=True会使grad返回none,不可取)
个人不清楚上述两种解释的正确性,且不清楚该如何debug
还请对这个问题有过研究的同学不吝指教
进来学习一下。