Operation on the part of data

I am a newer to the pytorch. And i want repeat the experiment of a paper.
That is, I want to implement like this:
for input x
h = x
h[:,:,1:,:]=weight[:,:,1:,:]*h[:,:,:-1,:]+(1-weight[:,:,1:,:])*h[:,:,1:,:]
this would cause a runtime error: a leaf Variable that requires grad has been used in an in-place operation.
I have made some other alternatives,such as o = copy.deepcopy(x), but all not work. Any one can help me?

How about this?

tmp = weight[:,:,1:,:]*h[:,:,:-1,:]+(1-weight[:,:,1:,:])*h[:,:,1:,:]
h = torch.cat((h[:,:,0:1,:], tmp), dim=2)

Thanks for your reply. I have tried this, it not work. I have found the method resolved it.
first create a new Variable, tmp = Variable(torch.zeros(h.size())), then do the operation.