How to multiple a part of tensor with another tensor?

I have a tensor x size of BxCxHxW and a tensor y size of B/2 xCxHxW. I want to perform multiplication between tensor x and tensor y such as the first part of tensor x (from 0 to B/2) will be modified by the result of the multiplication during forwarding , while the last part of tensor x is unchanged as the bellow figure.

Screenshot from 2020-10-22 01-15-57

How to do it in pytorch? I have my way

import torch
bx,cx,h,w = 4,2,3,3
by,cy,h,w = 2,2,3,3
tensor_x = torch.rand((bx,cx,h,w), requires_grad=True)
tensor_y = torch.rand((by,cy,h,w), requires_grad=True)
tensor_x_first = tensor_x[:by,...]  * tensor_y
tensor_x[:by,...] = tensor_x_first

I got the error during backward

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [4, 2, 3, 3]], which is output 0 of SliceBackward, is at version 1; expected version 0 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

Hello Johnson!


tensor_z = tensor_x * ((tensor_y, torch.ones_like (tensor_y)), dim = 0)


K. Frank

1 Like