softmax_jax ¶
log_softmax, softmax and cross_entropy without Softmax Collapse, in JAX.
Reference: J. T. Barron, D. Gnanapragasam, M. Tschannen, "A Fix for Softmax and Cross-Entropy",
arXiv 2610.31642, Section 2. Same math as softmax_torch.py:
s_k = 1[k != argmax] (z_k - z_max), log_softmax_k = s_k - softplus(LSE_{j != argmax} s_j)
Everything is jit / vmap / grad compatible.
log_softmax ¶
Drop-in replacement for jax.nn.log_softmax that does not collapse at large margins.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
Array
|
Logits of any shape and floating dtype. |
required |
axis
|
int
|
Axis to normalise over. |
-1
|
Returns:
| Type | Description |
|---|---|
Array
|
Log-probabilities with the shape and dtype of |
Source code in softmax_jax.py
softmax ¶
exp(log_softmax(z, axis)); drop-in replacement for jax.nn.softmax.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
Array
|
Logits of any shape and floating dtype. |
required |
axis
|
int
|
Axis to normalise over. |
-1
|
Returns:
| Type | Description |
|---|---|
Array
|
Probabilities with the shape and dtype of |
Source code in softmax_jax.py
log_prob_of ¶
Log-probability of one class per row, log_softmax(z)[..., target], without forming the
full log-softmax. Useful for policy gradients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
Array
|
Logits |
required |
target
|
Array
|
Integer class indices of shape |
required |
Returns:
| Type | Description |
|---|---|
Array
|
|
Source code in softmax_jax.py
cross_entropy ¶
cross_entropy(
logits: Array,
target: Array,
ignore_index: int = -100,
reduction: str = "mean",
) -> jax.Array
Hard-target cross-entropy -log_prob_of(logits, target), like
optax.softmax_cross_entropy_with_integer_labels plus ignore_index and reduction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits
|
Array
|
Logits |
required |
target
|
Array
|
Integer class indices |
required |
ignore_index
|
int
|
Rows with this target contribute 0 and are left out of the |
-100
|
reduction
|
str
|
|
'mean'
|
Returns:
| Type | Description |
|---|---|
Array
|
The loss; |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |