Why am I getting this runtime error

class SoftArgmax(nn.Module):

def __init__(self):
    super(SoftArgmax, self).__init__()

    self.x_weights = torch.Tensor(linspace_2d(64,64))
    self.y_weights = torch.Tensor(linspace_2d(64,64,dim=1))

    self.conv1 = nn.Conv2d(16,16,kernel_size=64,groups = 16,bias=False)
    self.conv2 = nn.Conv2d(16,16,kernel_size=64,groups = 16,bias=False)

def normalize(self,x):
  x = x.clone()
  for b in range(x.size()[0]):
    for h in range(x.size()[1]):
      x[b,h,:,:] = torch.div(x[b,h,:,:],torch.sum(x[b,h,:,:]))
  return x

def forward(self,x):

    x = self.normalize(x)

    for i in range(16):
      self.conv1.weight.data[i,0,:,:] = self.x_weights

    for i in range(16):
      self.conv2.weight.data[i,0,:,:] = self.y_weights

    return torch.cat((self.conv1(x).squeeze()*64,self.conv2(x).squeeze()*64),dim=1)

The above code is giving me this runtime error -

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

Can anyone provide me feedback? Thanks

x[b,h,:,:] = torch.div(x[b,h,:,:],torch.sum(x[b,h,:,:]))

is always an inplace operation whether you operate on a clone of the tensor or not.

You can replace those for loops with this.

x = x / x.sum(dim=3).sum(dim=2).unsqueeze(2).unsqueeze(3)
1 Like