Hi people,
I am facing a problem related to indexing variables elements in my loss function;
How can I get only few position from my vector with no interference in the graph?
model = Foo();
y_pred = model(X)
dist = criterion(y_pred, y)
#Apparently this line breaks the graph;
# How can I get only few position from my vector with no interferring the graph?
dist = dist[dist > 0.]
loss = MarginRankingLoss(dist, 0, 1)

optimize.zero_grad()
loss.backward()
optimize.step()


File “/home/rfelixmg/anaconda2/lib/python2.7/site-packages/torch/nn/_functions/loss.py”, line 155, in backward
input1, input2, y = self.saved_tensors
RuntimeError: Trying to backward through the graph second time, but the buffers have already been freed. Please specify retain_variables=True when calling backward for the first time.


Traceback (most recent call last):
File “/home//anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2869, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “”, line 1, in 
loss1.backward()
File "/home//anaconda2/lib/python2.7/site-packages/torch/autograd/variable.py”, line 146, in backward
self._execution_engine.run_backward((self,), (gradient,), retain_variables)
File “/home/*/anaconda2/lib/python2.7/site-packages/torch/autograd/function.py”, line 137, in backward
raise NotImplementedError
NotImplementedError