chickadee » nanograd » l2-normalize

l2-normalize tensor #!key (axis #f) (epsilon 1e-08)procedure

L2 normalization.

axis
#f to normalize the entire tensor, or an axis to normalize along

For 2-D tensors:

  • axis 0: each column becomes a unit vector
  • axis 1: each row becomes a unit vector
; Normalize the entire tensor
(define normalized (l2-normalize x))

; Normalize each row of a batch
(define batch (make-tensor32 (make-f32vector 200) '(10 20)))
(define row-normalized (l2-normalize batch axis: 1))  ; Each row has L2 norm 1