Skip to content
Open
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
7 changes: 6 additions & 1 deletion optax/losses/_smoothing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# ==============================================================================
"""Smoothing functions."""

from typing import Union

from jax import typing
import jax.numpy as jnp
from optax._src import utils
Expand All @@ -22,6 +24,8 @@
def smooth_labels(
labels: typing.ArrayLike,
alpha: float,
*,
axis: Union[int, tuple[int, ...], None] = -1,
) -> jnp.ndarray:
"""Apply label smoothing.

Expand All @@ -32,6 +36,7 @@ def smooth_labels(
Args:
labels: One hot labels to be smoothed.
alpha: The smoothing factor.
axis: Axis or axes along which to compute.

Returns:
a smoothed version of the one hot input labels.
Expand All @@ -41,5 +46,5 @@ def smooth_labels(
<https://arxiv.org/abs/1906.02629>`_, 2019
"""
utils.check_subdtype(labels, jnp.floating)
num_categories = labels.shape[-1]
num_categories = jnp.size(labels, axis)
return (1.0 - alpha) * labels + alpha / num_categories
Loading