Pytorch - RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed

前端 未结 1 1306
盖世英雄少女心
盖世英雄少女心 2020-12-08 14:37

I keep running into this error:

RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed. Specify ret

相关标签:
1条回答
  • 2020-12-08 14:51

    The problem is from my training loop: it doesn’t detach or repackage the hidden state in between batches? If so, then loss.backward() is trying to back-propagate all the way through to the start of time, which works for the first batch but not for the second because the graph for the first batch has been discarded.

    there are two possible solutions.

    1) detach/repackage the hidden state in between batches. There are (at least) three ways to do this (and I chose this solution):

     hidden.detach_()
     hidden = hidden.detach()
    

    2) replace loss.backward() with loss.backward(retain_graph=True) but know that each successive batch will take more time than the previous one because it will have to back-propagate all the way through to the start of the first batch.

    Example

    0 讨论(0)
提交回复
热议问题