Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Class weighting causes nan values in loss #39

Closed
chschroeder opened this issue Jul 21, 2023 · 0 comments
Closed

Class weighting causes nan values in loss #39

chschroeder opened this issue Jul 21, 2023 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@chschroeder
Copy link
Contributor

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

  • TransformerBasedClassification with class_weight='balanced'.
  • All labels of the current labeled set need to come from the same class. (Initialize active learning with such a set to quickly encounter the error.)
  • The error occurs during the first backpropagation.

Expected behavior

All weights are unequal to nan

Environment:

  • small-text v1.3.0

Addition information

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

@chschroeder chschroeder added the bug Something isn't working label Jul 21, 2023
@chschroeder chschroeder added this to the small-text-1.3.1 milestone Jul 21, 2023
@chschroeder chschroeder self-assigned this Jul 21, 2023
chschroeder added a commit that referenced this issue Jul 22, 2023
Signed-off-by: Christopher Schröder <chschroeder@users.noreply.github.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant