@@ -69,9 +69,7 @@ def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor:
6969 if n_pred_ch == 1 :
7070 warnings .warn ("single channel prediction, `to_onehot_y=True` ignored." )
7171 else :
72- if self .ignore_index is not None and (
73- self .ignore_index < 0 or self .ignore_index >= n_pred_ch
74- ):
72+ if self .ignore_index is not None and (self .ignore_index < 0 or self .ignore_index >= n_pred_ch ):
7573 # Replace sentinel ignore_index with a valid class before one_hot
7674 y_true = torch .where (y_true == self .ignore_index , torch .tensor (0 , device = y_true .device ), y_true )
7775 y_true = one_hot (y_true , num_classes = n_pred_ch )
@@ -148,9 +146,7 @@ def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor:
148146 if n_pred_ch == 1 :
149147 warnings .warn ("single channel prediction, `to_onehot_y=True` ignored." )
150148 else :
151- if self .ignore_index is not None and (
152- self .ignore_index < 0 or self .ignore_index >= n_pred_ch
153- ):
149+ if self .ignore_index is not None and (self .ignore_index < 0 or self .ignore_index >= n_pred_ch ):
154150 # Replace sentinel ignore_index with a valid class before one_hot
155151 y_true = torch .where (y_true == self .ignore_index , torch .tensor (0 , device = y_true .device ), y_true )
156152 y_true = one_hot (y_true , num_classes = n_pred_ch )
@@ -278,14 +274,10 @@ def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor:
278274 if not (self .to_onehot_y and orig_pred_ch != 1 ):
279275 if not (orig_pred_ch == 1 and y_true .shape [1 ] == 1 ):
280276 if y_true .shape != y_pred .shape :
281- raise ValueError (
282- f"ground truth has different shape ({ y_true .shape } ) from input ({ y_pred .shape } )"
283- )
277+ raise ValueError (f"ground truth has different shape ({ y_true .shape } ) from input ({ y_pred .shape } )" )
284278
285279 if self .to_onehot_y and orig_pred_ch != 1 :
286- if self .ignore_index is not None and (
287- self .ignore_index < 0 or self .ignore_index >= self .num_classes
288- ):
280+ if self .ignore_index is not None and (self .ignore_index < 0 or self .ignore_index >= self .num_classes ):
289281 # Replace sentinel ignore_index with a valid class before one_hot
290282 y_true = torch .where (y_true == self .ignore_index , torch .tensor (0 , device = y_true .device ), y_true )
291283 y_true = one_hot (y_true , num_classes = self .num_classes )
@@ -296,9 +288,7 @@ def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor:
296288
297289 mask = create_ignore_mask (original_y_true , self .ignore_index )
298290
299- use_mask = self .ignore_index is not None and (
300- self .ignore_index < 0 or self .ignore_index >= self .num_classes
301- )
291+ use_mask = self .ignore_index is not None and (self .ignore_index < 0 or self .ignore_index >= self .num_classes )
302292 if use_mask :
303293 y_pred_masked , y_true_masked = mask_loss_inputs (y_pred , y_true , self .ignore_index , mask = mask )
304294 else :
0 commit comments