One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True

使用grad = torch.autograd.grad(loss, self._model.parameters())手动求梯度时,可能遇到该问题:

One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior.

原因是对loss反向求导时,self._model.parameters()中存在没有在链式法则内使用的tensor,也就是说模型中定义了某个layer或者需要求导的tensor,但是loss的计算图里不存在改layer或者该变量,此时需要检查模型代码,删掉改layer或者该变量

你可能感兴趣的:(人工智能,深度学习,机器学习)