Re-assign value to a slice of Variable

Hi there,

I have a question about how to re-assign value to a slice of Variable.

For example, I have a variable A, and A.size()=[2,3,4], and A.requirs_grad is True. Now I have an all-zero dummy 2d variable B, and B.size() is (2,4), and B.requirs_grad is False. Currently, I only want to set A[:1,:]=B and do not change any other slices of A, so A[:0,:] and A[:2,:] are unchanged. I also want to change the A[:1,:].requirs_grad to False since B.requirs_grad is False and keep A[:0,:].requirs_grad and A[:2,:].requirs_grad still True.

Is there a way to do this?

I tried to set A[:1,:] to zero with “index_fill” and “detach()”, but A[:1,:].requirs_grad is still True.


The entire Variable either requires grad or not. There is no notion of part of it requiring grad.

I see… Thanks for the information!