webis-de / small-text

Active Learning for Text Classification in Python
https://small-text.readthedocs.io/
MIT License
585 stars 65 forks source link

Class weighting causes nan values in loss #39

Closed chschroeder closed 1 year ago

chschroeder commented 1 year ago

Bug description

When using TransformerBasedClassification with class_weight='balanced', the class_weights can get nan. This does not happen always, but only when the label distribution in the current labeled set is so skewed that all labels are from the same class.

For a multi-label problem, the encountered error is the following:

<...>
  File "/path/to/site-packages/small_text/integrations/transformers/classifiers/classification.py", line 591, in _train_single_batch
    loss.backward()
  File "/path/to/site-packages/torch/_tensor.py", line 487, in backward
    torch.autograd.backward(
  File "/path/to/site-packages/torch/autograd/__init__.py", line 200, in backward
    Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
RuntimeError: Function 'BinaryCrossEntropyWithLogitsBackward0' returned nan values in its 0th output.

Steps to reproduce

Expected behavior

All weights are unequal to nan

Environment:

Addition information

The problem is here and is caused by the scaling operation.