chickadee » nanograd » cross-entropy-loss

cross-entropy-loss pred target #!key (reduction 'mean) (from-logits #f)procedure

Cross-entropy loss with batch support.

pred
predictions tensor
If from-logits=#f
probabilities (softmax already applied)
If from-logits=#t
logits (raw scores, log-softmax applied internally)
target
target tensor
One-hot
same shape as pred
Class indices
(batch_size,) with integer class labels
reduction
'mean (average over batch) or 'sum
from-logits
if true, apply log-softmax to pred first

Input shapes:

  • 1D pred (n_classes,): single sample
  • 2D pred (batch_size, n_classes): batch of samples
; Single sample with one-hot target
(define loss (cross-entropy-loss probs target))

; Batch with one-hot targets
(define batch-probs (softmax logits axis: -1))
(define batch-loss (cross-entropy-loss batch-probs targets reduction: 'mean))

; Batch with class indices (more memory efficient)
(define class-indices (make-tensor32 (f32vector 0.0 2.0 1.0) '(3)))
(define batch-loss (cross-entropy-loss logits class-indices 
                                       from-logits: #t 
                                       reduction: 'mean))