报错如下:
File "D:\software\anaconda3\envs\pytorch171\lib\site-packages\torch\tensor.py", line 221, in backward
torch.autograd.backward(self, gradient, retain_graph, create_graph)
File "D:\software\anaconda3\envs\pytorch171\lib\site-packages\torch\autograd_init_.py", line 132, in backward
allow_unreachable=True) # allow_unreachable flag
RuntimeError: Expected grad_output->is_contiguous(grad_output->suggest_memory_format()) to be true, but got false. (Could this error message be improved? If so, please report an enhancement request to PyTorch.)
这个应该是你哪里有经过切片操作或者交换维度导致的,类似torch.view()之后没有操作就会报这个错误。报错的意思就是你前面的操作之后导致tensor矩阵无法连续了,你需要在切片完成之后后面加上X.contiguous()来保证按行优先进行一维展开的时候下标对应得上。具体原因你可以看看 PyTorch中的contiguous - 知乎 本文讲解了pytorch中contiguous的含义、定义、实现,以及contiguous存在的原因,非contiguous时的解决办法。并对比了numpy中的contiguous。 contiguous 本身是形容词,表示连续的,关于 contiguous,PyTorch 提供… https://zhuanlan.zhihu.com/p/64551412