I know this error is because inplace operation, but I don’t know where is the operation in my code.
I define my model forward function as follow:
def forward(self, input):
x = input[0]
y = input[1]
x = self.feat(x)
x_proj = self.proj(x)
x = x_proj
x = self.toimg(x, y)
x = self.concat(x, y)
x = self.resnet(x)
return x, x_proj
And I want to use x_proj to compute a extra loss:
def feature_regularizer(x_proj):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
min = torch.zeros(x_proj.shape).to(device).to(torch.float32)
max = torch.ones(x_proj.shape).to(device).to(torch.float32)
max[:, 0, :] = max[:, 0, :] * 1920
max[:, 1, :] = max[:, 1, :] * 1200
max[:, 2, :] = max[:, 2, :] * 200
min = min - x_proj
max = x_proj - max
loss_min = F.relu(min)
loss_max = F.relu(max)
loss = torch.sum(loss_min) + torch.sum(loss_max)
return loss
Finally I add this extra loss to the main loss:
pred, x_proj = classifier(input)
loss = scheduler(pred, target)
reg = feature_regularizer(x_proj) * 1e-8
loss = loss + reg
I’m very certain the inplace operation is in def feature_regularizer(x_proj), because when I delete this loss, I can run the code.
But I can’t find the inplace operation in def feature_regularizer(x_proj), so I’m confused, pls help me