Pytorch setting part of the training value into zero
When I am training network, I want to assign a specific part of the output into zero. For example, temp = nn.conv2d(3,6) in 3-channel / 16 batch size
would have tensor value like this.

temp = tensor(16,6,36,36)


I want half of the channel value into zero.

temp[:,:3,:,:] = 0


But I got this error.

RuntimeError: one of the variables needed for gradient computation 
has been modified by an inplace operation: [torch.cuda.FloatTensor 
[1, 128, 15, 15]], which is output 0 of ReluBackward1, is at version 
2; expected version 1 instead. Hint: the backtrace further above 
shows the operation that failed to compute its gradient. The variable 
in question was changed in there or anywhere later. Good luck!      


How can I approach this problem?

I've tried with this code

def weight_init(self,x,label):
    if label.data[0]:
        x[:,:64,:,:] =0
        return x
    else:
        x[:,64:,:,:] =0
        return x




You are doing operation on tensor, and this operation will be tracked for the future gradient computation. you just need to temporally switch off the changes tracking with torch.no_grad():

Code:

def weight_init(self,x,label):
    with torch.no_grad():
        if label.data[0]:
            x[:,:64,:,:] =0
            return x
        else:
            x[:,64:,:,:] =0
            return x

