Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tabfm/src/classifier_and_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3370,6 +3370,9 @@ def softmax(
Returns:
Softmax probabilities with the same shape as ``x``.
"""
if temperature <= 0:
raise ValueError('temperature must be greater than 0')

x = x / temperature
# Subtract max for numerical stability
x_max = np.max(x, axis=axis, keepdims=True)
Expand Down
18 changes: 18 additions & 0 deletions tabfm/src/classifier_and_regressor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@

# pylint: disable=invalid-name


class SoftmaxTest(absltest.TestCase):

def test_rejects_non_positive_temperature(self):
logits = np.array([[1.0, 2.0, 3.0]])

for temperature in (0.0, -1.0):
with self.assertRaisesRegex(ValueError, 'temperature must be greater than 0'):
TabFMClassifier.softmax(logits, temperature=temperature)

def test_positive_temperature_returns_probabilities(self):
logits = np.array([[1.0, 2.0, 3.0]])

probabilities = TabFMClassifier.softmax(logits, temperature=0.9)

np.testing.assert_allclose(probabilities.sum(axis=-1), 1.0)


class EnsembleGeneratorTest(absltest.TestCase):

def test_permute_categorical_structure(self):
Expand Down