code: def dice(input:Tensor, targs:Tensor, iou:bool=False)->Rank0Tensor:
      "Dice coefficient metric for binary target. If iou=True, returns iou metric, classic for segmentation problems."
      n = targs.shape[0]
      input = input.argmax(dim=1).view(n,-1)
      targs = targs.view(n,-1)
      intersect = (input * targs).sum().float()
      union = (input+targs).sum().float()
      if not iou: return (2. * intersect / union if union > 0 else union.new([1.]).squeeze())
      else: return intersect / (union-intersect+1.0f dice(input:Tensor, targs:Tensor, iou:bool=False)->Rank0Tensor:
      "Dice coefficient metric for binary target. If iou=True, returns iou metric, classic for segmentation problems."
      n = targs.shape[0]
      input = input.argmax(dim=1).view(n,-1)
      targs = targs.view(n,-1)
      intersect = (input * targs).sum().float()
      union = (input+targs).sum().float()
      if not iou: return (2. * intersect / union if union > 0 else union.new([1.]).squeeze())
      else: return intersect / (union-intersect+1.0)
docs: Dice coefficient metric for binary target. If iou=True, returns iou metric, classic for segmentation problems.