How to remove the grad_fn=<SelectBackward> in output array

I am trying to put all my model’s output in an array, though I am using it in eval mode,
computing the output with torch.no_grad(): and detaching it the array is showing
grad_fn=<SelectBackward>
My code

m.eval() # m is my model
for vec,ind in loaderx:

    with torch.no_grad():
        opp,_,_ = m(vec)
        opp = opp.detach().cpu()
    for i in range(ind.shape[0]):
        ar[i] = torch.cat((torch.tensor([ind[i]]),opp[i]),dim =0)
    break

when I try to output the array where my outputs are
ar[0][0] #shown only one element since its a big array
output →
tensor(3239., grad_fn=<SelectBackward>)

Hi,

The detach() in the no_grad block is not needed.
You will need to move all the ops into the no_grad block though to make sure no gradient is tracked :slight_smile:

1 Like

Hi!
I did it like this, it worked. Thanks

m.eval()
for vec,ind in loaderx:
    with torch.no_grad():
        opp,_,_ = m(vec)    
    for i in range(ind.shape[0]):
        ans = torch.cat((torch.tensor([ind[i]]),opp[i]),dim =0)
        ar[ind[i]] = ans

But can you solve another query, detach() should have been cut it in the computational graph then why it didn’t work?

At the place where you detach() is, there is no computational graph because you are running in no_grad mode. So it is just a no-op where it is.

1 Like