报错内容:
- CPU上运行报错:RuntimeError: Trying to backward through the graph a second time (or directly access saved variables after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved variables after calling backward.
- GPU上运行报错:RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!
报错原因: 直接采用 RNN 的隐藏状态
参与到模型运算中。
解决方案: 在使用 RNN 的隐藏状态
时,需要使用 .detach()
将隐藏状态从计算图中分离出来。