Skip to content

Instantly share code, notes, and snippets.

@bhpfelix
Created April 4, 2020 23:25
Show Gist options
  • Save bhpfelix/2e9cbb8df73eae389478e4c4cdd13326 to your computer and use it in GitHub Desktop.
Save bhpfelix/2e9cbb8df73eae389478e4c4cdd13326 to your computer and use it in GitHub Desktop.
RuntimeError: Trying to backward through the graph a second time
import torch
import numpy as np
# This will be share by both iterations and will make the second backward fail !
a = torch.ones(3, requires_grad=True) * 4
# Instead, do
# a = torch.tensor(np.ones(3) * 4, requires_grad=True)
for i in range(10):
d = torch.sum(a)
# The first here will work but the second will not !
d.backward()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment