【pytorch异常处理】使用释放的图资源

  • Post author:
  • Post category:其他




【error】Trying to backward through the graph a second time, but the saved intermediate results have already been freed


RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already been freed. Specify retain_graph=True when calling backward the first time.

问题原因:

To reduce memory usage, during the .backward() call, all the intermediary results are deleted when they are not needed anymore. Hence if you try to call .backward() again, the intermediary results don’t exist and the backward pass cannot be performed (and you get the error you see).

You can call .backward(retain_graph=True) to make a backward pass that will not delete intermediary results, and so you will be able to call .backward() again. All but the last call to backward should have the retain_graph=True option.

处于减少使用内存资源,每次执行完backward时,一些中间结果会被释放。如果此时使用了这些值,可能会出现该问题。

如果一定要使用,可以在backward函数中添加参数

retain_graph=True


参考连接:https://discuss.pytorch.org/t/runtimeerror-trying-to-backward-through-the-graph-a-second-time-but-the-buffers-have-already-been-freed-specify-retain-graph-true-when-calling-backward-the-first-time/6795



版权声明:本文为mimiduck原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。