# Debugging "RuntimeError: Trying to backward through the graph a second time, but the buffers have already been freed."

**URL:** <https://discuss.pytorch.org/t/debugging-runtimeerror-trying-to-backward-through-the-graph-a-second-time-but-the-buffers-have-already-been-freed/10900>\
**Category:** autograd\
**Created:** [December 7, 2017, 5:42pm UTC](https://discuss.pytorch.org/t/debugging-runtimeerror-trying-to-backward-through-the-graph-a-second-time-but-the-buffers-have-already-been-freed/10900 "2017-12-07T17:42:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lezcano](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lezcano/32/40650_2.png) [@Lezcano](https://discuss.pytorch.org/u/Lezcano)\
**Post date:** [December 7, 2017, 5:42pm UTC](https://discuss.pytorch.org/t/debugging-runtimeerror-trying-to-backward-through-the-graph-a-second-time-but-the-buffers-have-already-been-freed/10900/1 "2017-12-07T17:42:58Z")

</div>

I am getting the following error

> 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.

in a fairly complex neural network.

Is there any way to debug this kind of errors, to see where is it being thrown?  
Using the function `register_backward_hook` via `nn.apply(fn)` where `fn` registers a hook that prints the name of the class during the backward pass, did not take me any further. It looks like the prints happen before the exception is raised, so it looks like the gradients can be computed, but after that the backward pass breaks.

---

<div class="post-metadata">

**Author:** ![jdhao](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jdhao/32/2558_2.png) [@jdhao](https://discuss.pytorch.org/u/jdhao)\
**Post date:** [December 8, 2017, 1:53am UTC](https://discuss.pytorch.org/t/debugging-runtimeerror-trying-to-backward-through-the-graph-a-second-time-but-the-buffers-have-already-been-freed/10900/2 "2017-12-08T01:53:30Z")

</div>

Have you searched the forum and read [this post](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/2), for example?

---

<div class="post-metadata">

**Author:** ![Lezcano](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lezcano/32/40650_2.png) [@Lezcano](https://discuss.pytorch.org/u/Lezcano)\
**Post date:** [December 8, 2017, 2:06am UTC](https://discuss.pytorch.org/t/debugging-runtimeerror-trying-to-backward-through-the-graph-a-second-time-but-the-buffers-have-already-been-freed/10900/3 "2017-12-08T02:06:23Z")

</div>

I have read most of the posts that talk about this issue in the forum, and even though I understand the problem, I don’t know of a way of debugging it.

For what is worth, I already solved the problem. I was indeed storing a Variable in some function deep in the code, but I would still be interested in knowing if there’s any technique to look for the possible Variable in the code that causes these kind of errors.

---

<div class="post-metadata">

**Author:** ![jdhao](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jdhao/32/2558_2.png) [@jdhao](https://discuss.pytorch.org/u/jdhao)\
**Post date:** [December 8, 2017, 2:10am UTC](https://discuss.pytorch.org/t/debugging-runtimeerror-trying-to-backward-through-the-graph-a-second-time-but-the-buffers-have-already-been-freed/10900/4 "2017-12-08T02:10:49Z")

</div>

I think right now the best way is to understand what your computation graph looks like and then find if your are trying to back-prop through the graph more than once.

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [December 8, 2017, 10:30am UTC](https://discuss.pytorch.org/t/debugging-runtimeerror-trying-to-backward-through-the-graph-a-second-time-but-the-buffers-have-already-been-freed/10900/5 "2017-12-08T10:30:14Z")

</div>

I guess a solution with the new release, if you have a network with supported ops, would be to export the network with onnx twice and the second one should contain the first one, linked via the Variable that should not be there 🙂
