My task is a  binary segmentation (foreground and background)
If you don’t reduce the loss, you would have to pass the gradient into  backward  with the same shape as your output. Please, could you help me with an example?
I have read the work you recommended and I have modified my codes as follows:
Epoch_num=5
for e in range(Epoch_num):
print('Epoch', e+1)

train_loss = 0

model= model.train()



for idx, data in enumerate(train_loader):

#for idx, data in tqdm (enumerate(train_loader)):

    optimizer.zero_grad()

    x, y_true = data

    if torch.cuda.is_available():

        x, y_true = x.cuda(), y_true.cuda()

    # forward

    out = model(x)

    out = F.log_softmax(out, dim=1)  # (b, n, h, w)

    #loss = criterion(out[0,0,:,:], y_true[0,:,:].type_as(out))

    loss = criterion(out, y_true)

    # backward

    optimizer.zero_grad()

    loss.backward()

    optimizer.step()

    train_loss += loss.item()

    if idx % 100 == 0 and idx != 0:

        print('[{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(

             idx , len(train_loader.dataset),

            100. * idx / len(train_loader), train_loss/ idx))

       

label_pred = out.max(dim=1)[1].data.cpu()

label_true= y_true.unsqueeze(1)

label_true = label_true .data.cpu()

acc= get_accuracy(label_true,label_pred)

print("Epoch {}/{}, Loss: {:.5f}, Accuracy: {:.5f}".format(e+1,Epoch_num, train_loss/len(train_loader.dataset), acc))

As it can be seen, I have divided the total training loss by the number of samples in the training data. Please, kindly check and see whether doing so is logical or not.
Some statistics of the training process are given below
Epoch 1
[100/1104 (9%)] Loss: 0.700105
[200/1104 (18%)]    Loss: 0.696637
[300/1104 (27%)]    Loss: 0.695482
[400/1104 (36%)]    Loss: 0.694904
[500/1104 (45%)]    Loss: 0.694557
[600/1104 (54%)]    Loss: 0.694326
[700/1104 (63%)]    Loss: 0.694161
[800/1104 (72%)]    Loss: 0.694037
[900/1104 (82%)]    Loss: 0.693941
[1000/1104 (91%)]   Loss: 0.693863
[1100/1104 (100%)]  Loss: 0.693800
Epoch 1/5, Loss: 0.69317, Accuracy: 0.48804
Epoch 2
[100/1104 (9%)] Loss: 0.700100
[200/1104 (18%)]    Loss: 0.696634
[300/1104 (27%)]    Loss: 0.695478
[400/1104 (36%)]    Loss: 0.694899
[500/1104 (45%)]    Loss: 0.694552
[600/1104 (54%)]    Loss: 0.694321
[700/1104 (63%)]    Loss: 0.694156
[800/1104 (72%)]    Loss: 0.694032
[900/1104 (82%)]    Loss: 0.693935
[1000/1104 (91%)]   Loss: 0.693858
[1100/1104 (100%)]  Loss: 0.693795
Epoch 2/5, Loss: 0.69317, Accuracy: 0.48364
Epoch 3
[100/1104 (9%)] Loss: 0.700095
[200/1104 (18%)]    Loss: 0.696631
[300/1104 (27%)]    Loss: 0.695475
[400/1104 (36%)]    Loss: 0.694896
[500/1104 (45%)]    Loss: 0.694549
[600/1104 (54%)]    Loss: 0.694317
[700/1104 (63%)]    Loss: 0.694152
[800/1104 (72%)]    Loss: 0.694028
[900/1104 (82%)]    Loss: 0.693932
[1000/1104 (91%)]   Loss: 0.693855
[1100/1104 (100%)]  Loss: 0.693791
Epoch 3/5, Loss: 0.69316, Accuracy: 0.49097
Epoch 4
[100/1104 (9%)] Loss: 0.700089
[200/1104 (18%)]    Loss: 0.696625
[300/1104 (27%)]    Loss: 0.695470
[400/1104 (36%)]    Loss: 0.694892
[500/1104 (45%)]    Loss: 0.694545
[600/1104 (54%)]    Loss: 0.694314
[700/1104 (63%)]    Loss: 0.694149
[800/1104 (72%)]    Loss: 0.694025
[900/1104 (82%)]    Loss: 0.693928
[1000/1104 (91%)]   Loss: 0.693851
[1100/1104 (100%)]  Loss: 0.693788
Epoch 4/5, Loss: 0.69316, Accuracy: 0.50049