@@ -159,6 +159,14 @@ def test_invalid_options_and_shapes(self):
159159 loss_type (other_act = 1 ) # type: ignore[arg-type]
160160 with self .assertRaises (ValueError ):
161161 loss_type (weight = [1.0 , - 1.0 ])
162+ for invalid_weight in (float ("nan" ), float ("inf" ), - float ("inf" )):
163+ with self .subTest (invalid_weight = invalid_weight ):
164+ with self .assertRaises (ValueError ):
165+ loss_type (weight = invalid_weight )
166+ with self .assertRaises (ValueError ):
167+ loss_type (weight = [1.0 , invalid_weight ])
168+ with self .assertRaises (ValueError ):
169+ loss_type (weight = torch .tensor ([invalid_weight ]))
162170 with self .assertRaises (ValueError ):
163171 loss_type (weight = [[1.0 ]])
164172 with self .assertRaises (ValueError ):
@@ -171,8 +179,9 @@ def test_invalid_options_and_shapes(self):
171179 loss_type ()(torch .ones (1 , 2 ), torch .ones (1 , 2 ))
172180 with self .assertRaises (TypeError ):
173181 loss_type ()(torch .ones (1 , 2 , 2 , dtype = torch .int64 ), torch .ones (1 , 2 , 2 ))
174- with self .assertRaises (ValueError ):
175- SoftL1ACELoss (empty_weight = - 1 )
182+ for invalid_empty_weight in (- 1.0 , float ("nan" ), float ("inf" ), - float ("inf" )):
183+ with self .subTest (invalid_empty_weight = invalid_empty_weight ), self .assertRaises (ValueError ):
184+ SoftL1ACELoss (empty_weight = invalid_empty_weight )
176185
177186 def test_single_channel_warnings (self ):
178187 prediction = torch .tensor ([[[0.2 , 0.8 ]]])
0 commit comments