Skip to content

Commit c472755

Browse files
Raise clear error for problem_type="single_label_classification" with num_labels=1 (huggingface#45611)
* Raise clear error for problem_type="single_label_classification" with num_labels=1 This combination is mathematically degenerate: applying cross-entropy loss to a single logit always yields zero loss, so training silently accomplishes nothing. Validate the combination in PreTrainedConfig.__post_init__ so users get a clear error at config construction with a pointer to the correct setup (num_labels=2 for binary classification, or problem_type="regression" for a single-output regression head). Closes huggingface#45479 * Update src/transformers/configuration_utils.py * Update tests/utils/test_configuration_utils.py * Update src/transformers/configuration_utils.py --------- Co-authored-by: Matt <Rocketknight1@users.noreply.github.com>
1 parent 678e871 commit c472755

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

src/transformers/configuration_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ def __post_init__(self, **kwargs):
265265
# Keys are always strings in JSON so convert ids to int
266266
self.id2label = {int(key): value for key, value in self.id2label.items()}
267267

268+
if self.problem_type == "single_label_classification" and self.num_labels == 1:
269+
raise ValueError(
270+
'`problem_type="single_label_classification"` requires `num_labels > 1`. For binary '
271+
'classification use `num_labels=2`, or use `problem_type="regression"` for a '
272+
"single-output regression head."
273+
)
274+
268275
# BC for rotary embeddings. We will pop out legacy keys from kwargs and rename to new format
269276
if hasattr(self, "rope_parameters"):
270277
kwargs = self.convert_rope_params_to_dict(**kwargs)

0 commit comments

Comments
 (0)