class_docstr: Implements the cross entropy loss function.
  
  Args:
      weight (tuple|list|ndarray|Tensor, optional): A manual rescaling weight
          given to each class. Its length must be equal to the number of classes.
          Default ``None``.
      ignore_index (int64, optional): Specifies a target value that is ignored
          and does not contribute to the input gradient. Default ``255``.
      top_k_percent_pixels (float, optional): the value lies in [0.0, 1.0].
          When its value < 1.0, only compute the loss for the top k percent pixels
          (e.g., the top 20% pixels). This is useful for hard pixel mining. Default ``1.0``.
      avg_non_ignore (bool, optional): Whether the loss is only averaged over non-ignored value of pixels. Default: True.
      data_format (str, optional): The tensor format to use, 'NCHW' or 'NHWC'. Default ``'NCHW'``.
class_docstr_tok: ["Implements","the","cross","entropy","loss","function",".","Args",":","weight","(","tuple|list|ndarray|Tensor",",","optional",")",":","A","manual","rescaling","weight","given","to","each","class",".","Its","length","must","be","equal","to","the","number","of","classes",".","Default","``","None","``",".","ignore_index","(","int64",",","optional",")",":","Specifies","a","target","value","that","is","ignored","and","does","not","contribute","to","the","input","gradient",".","Default","``","255","``",".","top_k_percent_pixels","(","float",",","optional",")",":","the","value","lies","in","[","0.0",",","1.0","]",".","When","its","value","<","1.0",",","only","compute","the","loss","for","the","top","k","percent","pixels","(","e.g.",",","the","top","20","%","pixels",")",".","This","is","useful","for","hard","pixel","mining",".","Default","``","1.0","``",".","avg_non_ignore","(","bool",",","optional",")",":","Whether","the","loss","is","only","averaged","over","non-ignored","value","of","pixels",".","Default",":","True",".","data_format","(","str",",","optional",")",":","The","tensor","format","to","use",",","'NCHW","'","or","'NHWC","'",".","Default","``","'NCHW","'","``","."]
docstr: Consider mask and top_k to calculate the final loss.
  
  Args:
      logit (Tensor): Logit tensor, the data type is float32, float64. Shape is
          (N, C), where C is number of classes, and if shape is more than 2D, this
          is (N, C, D1, D2,..., Dk), k >= 1.
      label (Tensor): Label tensor, the data type is int64. Shape is (N), where each
          value is 0 <= label[i] <= C-1, and if shape is more than 2D, this is
          (N, D1, D2,..., Dk), k >= 1.
      semantic_weights (Tensor, optional): Weights about loss for each pixels,
          shape is the same as label.
      loss (Tensor): Loss tensor which is the output of cross_entropy. If soft_label
          is False in cross_entropy, the shape of loss should be the same as the label.
          If soft_label is True in cross_entropy, the shape of loss should be
          (N, D1, D2,..., Dk, 1).
  Returns:
      (Tensor): The average loss.
docstr_tok: ["Consider","mask","and","top_k","to","calculate","the","final","loss",".","Args",":","logit","(","Tensor",")",":","Logit","tensor",",","the","data","type","is","float32",",","float64",".","Shape","is","(","N",",","C",")",",","where","C","is","number","of","classes",",","and","if","shape","is","more","than","2D",",","this","is","(","N",",","C",",","D1",",","D2",",","...",",","Dk",")",",","k",">","=","1.","label","(","Tensor",")",":","Label","tensor",",","the","data","type","is","int64",".","Shape","is","(","N",")",",","where","each","value","is","0","<","=","label","[","i","]","<","=","C-1",",","and","if","shape","is","more","than","2D",",","this","is","(","N",",","D1",",","D2",",","...",",","Dk",")",",","k",">","=","1.","semantic_weights","(","Tensor",",","optional",")",":","Weights","about","loss","for","each","pixels",",","shape","is","the","same","as","label",".","loss","(","Tensor",")",":","Loss","tensor","which","is","the","output","of","cross_entropy",".","If","soft_label","is","False","in","cross_entropy",",","the","shape","of","loss","should","be","the","same","as","the","label",".","If","soft_label","is","True","in","cross_entropy",",","the","shape","of","loss","should","be","(","N",",","D1",",","D2",",","...",",","Dk",",","1",")",".","Returns",":","(","Tensor",")",":","The","average","loss","."]
code: def _post_process_loss(self, logit, label, semantic_weights, loss):
      """
      Consider mask and top_k to calculate the final loss.
  
      Args:
          logit (Tensor): Logit tensor, the data type is float32, float64. Shape is
              (N, C), where C is number of classes, and if shape is more than 2D, this
              is (N, C, D1, D2,..., Dk), k >= 1.
          label (Tensor): Label tensor, the data type is int64. Shape is (N), where each
              value is 0 <= label[i] <= C-1, and if shape is more than 2D, this is
              (N, D1, D2,..., Dk), k >= 1.
          semantic_weights (Tensor, optional): Weights about loss for each pixels,
              shape is the same as label.
          loss (Tensor): Loss tensor which is the output of cross_entropy. If soft_label
              is False in cross_entropy, the shape of loss should be the same as the label.
              If soft_label is True in cross_entropy, the shape of loss should be
              (N, D1, D2,..., Dk, 1).
      Returns:
          (Tensor): The average loss.
      """
      if self.avg_non_ignore:
          mask = paddle.cast(
              label != self.ignore_index, dtype="float32"
          )
      else:
          mask = paddle.ones(label.shape, dtype="float32")
      mask.stop_gradient = True
      label.stop_gradient = True
  
      if loss.ndim > mask.ndim:
          loss = paddle.squeeze(loss, axis=-1)
      loss = loss * mask
      if semantic_weights is not None:
          loss = loss * semantic_weights
  
      if self.weight is not None:
          _one_hot = F.one_hot(label * mask, logit.shape[-1])
          coef = paddle.sum(_one_hot * self.weight, axis=-1)
      else:
          coef = paddle.ones_like(label)
  
      if self.top_k_percent_pixels == 1.0:
          avg_loss = paddle.mean(loss) / (
              paddle.mean(mask * coef) + self.EPS
          )
      else:
          loss = loss.reshape((-1,))
          top_k_pixels = int(self.top_k_percent_pixels * loss.numel())
          loss, indices = paddle.topk(loss, top_k_pixels)
          coef = coef.reshape((-1,))
          coef = paddle.gather(coef, indices)
          coef.stop_gradient = True
          coef = coef.astype("float32")
          avg_loss = loss.mean() / (paddle.mean(coef) + self.EPS)
  
      return avg_loss
  
file_path: PaddleSeg/paddleseg/models/losses/cross_entropy_loss.py
imports: import paddle
  from paddle import nn
  import paddle.nn.functional
  from paddleseg.cvlibs import manager
label_desc: Use image node_id 3 for calling the CrossEntropyLoss obj's underlying member method code with example usage: obj._post_process_loss(logit, label, semantic_weights, loss) and returns: avg_loss