Hi, I used CrossEntropyLoss() for segmentation but I have this error. Please help me
"“RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [4, 512, 1, 1]], which is output 0 of ReluBackward1, is at version 6; expected version 2 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).”
I can’t loss.backward(),
j=0
criterion1 = nn.CrossEntropyLoss()
best_loss = 1000000000
for epoch in range(2):
model.train()
for i, dat in enumerate(train_generator):
j+=1
features, masks,labels = dat
features = features.to(device).float()
masks = masks.to(device)
labels = labels.to(device)
optimizer.zero_grad()
### FORWARD AND BACK PROP
features = features.permute(0, 3, 1,2)
#print(features.shape)
logits_labels,logits_masks = model((features))
#masks=masks[...,0].squeeze()
masks = masks.squeeze_()
#Loss
loss1 = criterion1(logits_masks,masks)
loss2 = criterion1(logits_labels,labels)
loss=loss1+loss2
loss.backward()
running_loss += loss.item()
### UPDATE MODEL PARAMETERS
optimizer.step()