Hi,
The problem is that you call it on grad[i,label,:,:] which is not a view of the original grad. So the masked_fill_ does not modify what you want.
You wither want do do it with a single indexing ope as grad[foo] = 333. Or use only view operations like (I did not test the code, but you want something similar) grad.select(0, 1).index_copy_(0, label, grad[i,label,:,:].masked_fill_(mask, 333))