No worries! You’re golden.
I actually didn’t get an error, but that sounds like sound advice so I’ll do that as well and post the output:
CODE without total.detach_()

def helper(x):
return 2 * x[0] + 3 * x[1]**2
x = autograd.Variable(torch.FloatTensor([1, 2]), requires_grad=True)
for i in range(3): # number of epochs
print("iteration: " + str(i))
total = autograd.Variable(torch.FloatTensor([0]))
total += helper(x)
total.backward()
x.data -= 0.1 * x.grad.data
x.grad.data.fill_(0)
print(x[0], x[1])


OUTPUT without total.detach_()
iteration: 0
iteration: 1
iteration: 2
Variable containing:
0.4000
[torch.FloatTensor of size 1]
Variable containing:
0.1280
[torch.FloatTensor of size 1]

CODE with total.detach_()
def helper(x):
return 2 * x[0] + 3 * x[1]**2
x = autograd.Variable(torch.FloatTensor([1, 2]), requires_grad=True)
for i in range(3): # number of epochs
print("iteration: " + str(i))
total = autograd.Variable(torch.FloatTensor([0]))
total += helper(x)
total.backward()
x.data -= 0.1 * x.grad.data
x.grad.data.fill_(0)
total.detach_()
print(x[0], x[1])

OUTPUT with total.detach_()
Identical to the first output.

They’re both the same, which makes sense since I initialize total as a new variable at the beginning anyway. Do you think it saves memory to detach it at the end of the loop? I’ll do it anyway since it doesn’t appear to harm anything and might make the code more readable.
Again, thank you both for all your help!