Skip to content

Update array_utils.py#303

Merged
Dref360 merged 1 commit intobaal-org:masterfrom
averyma:patch-1
Dec 3, 2025
Merged

Update array_utils.py#303
Dref360 merged 1 commit intobaal-org:masterfrom
averyma:patch-1

Conversation

@averyma
Copy link
Copy Markdown
Contributor

@averyma averyma commented Dec 3, 2025

Increase tolerance from 1e-5 (default) to 1e-4 when checking sum_to_one

Summary:

The PR relaxes the tolerance used to check whether an input vector is a valid probability distribution. The current check uses np.allclose(probabilities.sum(1), 1), which relies on the default rtol=1e-5. Vectors produced by torch.nn.Softmax in float32 often sum to around 1 ± 1e-4, so valid probability vectors are incorrectly rejected.

Features:

To avoid complicating the existing code, this pr simply increases the tolerance to 1e-4 so that softmax outputs are correctly treated as probability distributions.

Checklist:

  • [x ] Your code is documented (To validate this, add your module to tests/documentation_test.py).
  • [x ] Your code is tested with unit tests.
  • [x ] You moved your Issue to the PR state.

Increase tolerance from 1e-5 (default) to 1e-4 when checking sum_to_one
not_bounded = np.min(probabilities) < 0 or np.max(probabilities) > 1.0
multiclass = probabilities.shape[1] > 1
sum_to_one = np.allclose(probabilities.sum(1), 1)
sum_to_one = np.allclose(probabilities.sum(1), 1, rtol=1e-4)
Copy link
Copy Markdown
Member

@Dref360 Dref360 Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on your explanation, should this uses atol instead of rtol? I guess in this case since b==1, it doesn't matter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the response. You are correct it does not matter in this case.

Copy link
Copy Markdown
Member

@Dref360 Dref360 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thank you!

@Dref360 Dref360 merged commit 2309c97 into baal-org:master Dec 3, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants