thanks for your suggestion and i have followed your words but facing now this issue.
####modification######
# # loss
# l = criterion(output, y)
# # tot_loss += l.item()
# l.backward()
# optimizer.step()
        # metrics
        x, y = output.detach().cpu(), y.detach().cpu()

        # iou_score = iou(y, x)
        # dice_score = dice_coef(y, x)
        soft_score = soft_loss(y, x)
        hard_score = hard_loss(y, x)

######metrics###########
import numpy as np
import torch.nn.functional as F
def soft_loss(y_true, y_pred, beta=0.95):
cross_entropy = F.nll_loss(y_pred.log(), y_true, size_average=False)
soft_reed = -y_pred * torch.log(y_pred + 1e-8)
return beta * cross_entropy + (1 - beta) * torch.sum(soft_reed)
#####errorrr####
File “/home/ali/BioNet_project/pytorch_version/utils.py”, line 90, in train
soft_score = soft_loss(y, x)
File “/home/ali/BioNet_project/pytorch_version/metrics.py”, line 23, in soft_loss
cross_entropy = F.nll_loss(y_pred.log(), y_true, size_average=False)
File “/home/ali/miniconda3/envs/bio/lib/python3.6/site-packages/torch/nn/functional.py”, line 2117, in nll_loss
ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: only batches of spatial targets supported (3D tensors) but got targets of dimension: 4