Index of the closest codebook entry for each row of x.
The search is the non-differentiable half of vector quantisation:
the result is an integer field, so no gradient flows through it and
none is defined. vector_quantize pairs it with the
straight-through estimator to make the surrounding network trainable.
Parameters
Returns
Tensorint64 index field of shape (*) with values in [0, K).
Notes
Distances go through lucid.cdist, whose p=2 path uses the
stable expansion
rather than materialising an (N, K, D) difference. It does
still build the (N, K) matrix, which at a large latent grid and
a large codebook is the dominant allocation of a quantiser; a fused
engine kernel that reduces over K without materialising it is the
natural next step and would slot in behind this exact signature.
Examples
>>> import lucid
>>> import lucid.nn.functional as F
>>> codebook = lucid.tensor([[0.0, 0.0], [1.0, 1.0]])
>>> x = lucid.tensor([[0.9, 1.1], [0.1, 0.0]])
>>> F.nearest_codebook(x, codebook).tolist()
[1, 0]