Computes cross-entropy loss for multi-class classification.
Usage
mlx_cross_entropy(logits, targets, reduction = c("mean", "sum", "none"))Arguments
- logits
A matrix of unnormalized predictions (logits), with one row per sample and one column per class, as an mlx array or R matrix.
- targets
Target class indices as an mlx array or integer vector. Indices are 1-based, with one non-missing index per sample.
- reduction
Type of reduction: "mean" (default), "sum", or "none".
Examples
# Logits for 3 samples, 4 classes
logits <- mlx_matrix(rnorm(12), 3, 4)
targets <- as_mlx(c(1, 3, 2))
mlx_cross_entropy(logits, targets)
#> mlx array []
#> dtype: float32
#> values:
#> [1] 2.319621