Skip to content
class

Embedding

extends_PaddedTable
Embedding(num_embeddings: int, embedding_dim: int, padding_idx: int | None = None, max_norm: float | None = None, norm_type: float = 2.0, scale_grad_by_freq: bool = False, sparse: bool = False, _weight: Tensor | None = None, _freeze: bool = False, device: DeviceLike = None, dtype: DTypeLike = None)

Implementing kernel

C++ engine symbols that back this Python API.

Learnable dense lookup table that maps integer token indices to vectors.

An embedding table is a matrix W∈RV×DW \in \mathbb{R}^{V \times D} where VV = num_embeddings (vocabulary size) and DD = embedding_dim. The forward pass is a simple index operation:

y=W[idx]y = W[\text{idx}]

Each row W[i]W[i] is the dense representation (embedding vector) of token ii. Because indexing is not differentiable with respect to the integer indices themselves, gradients flow only into the rows of WW that were selected during the forward pass.

Padding index. When padding_idx is set, the corresponding row is initialised to zero and its gradient is masked to zero during backpropagation. This is the standard way to mark a special <PAD> token whose embedding should never influence the model.

Max-norm renormalisation. When max_norm is set, every row looked up whose LpL_p-norm exceeds max_norm is rescaled in place at each forward call before the lookup:

W[i]←W[i]⋅max_norm∥W[i]∥pif ∥W[i]∥p>max_normW[i] \leftarrow W[i] \cdot \frac{\texttt{max\_norm}}{\|W[i]\|_p} \quad \text{if } \|W[i]\|_p > \texttt{max\_norm}

Parameters

num_embeddingsint
Size of the embedding dictionary (vocabulary size VV).
embedding_dimint
Dimensionality of each embedding vector (DD).
padding_idxint or None= None
If provided, the embedding for this index is fixed at zero and receives no gradient updates. Negative values are normalised to padding_idx + num_embeddings. Default: None.
max_normfloat or None= None
If provided, the rows looked up whose LpL_p-norm exceeds this value are renormalised in place at every forward call. Default: None.
norm_typefloat= 2.0
The pp in the LpL_p-norm used by max_norm. Default: 2.0.
scale_grad_by_freqbool= False
Divide each row's gradient by the number of times that index appeared in the lookup, so a token seen fifty times in a batch does not take a fifty-times larger step than one seen once. The count belongs to each forward call: two lookups before one backward() are each scaled by their own. Default: False.
sparsebool= False
Gradients are always dense, so True raises NotImplementedError at the first forward. Default: False.
deviceDeviceLike= None
Device for the weight tensor.
dtypeDTypeLike= None
Data type for the weight tensor.

Attributes

weightParameter, shape (num_embeddings, embedding_dim)
The embedding matrix WW. Rows are initialised from N(0,1)\mathcal{N}(0, 1). If padding_idx is set, that row is zeroed immediately after initialisation.

Notes

  • Input x: (*) — integer tensor of arbitrary shape with values in [0, num_embeddings).
  • Output: (*, embedding_dim) — the input shape with an extra trailing dimension of size embedding_dim.

The weight matrix is initialised from a standard normal distribution N(0,1)\mathcal{N}(0, 1), which gives each embedding a unit-order magnitude. For downstream layers that are sensitive to input scale (e.g. transformers), consider dividing by D\sqrt{D} after construction.

An index outside [0, num_embeddings) differs by device. On the CPU it raises IndexError. On Metal the index is never read back (that would stall every step), so the row it names reads as NaN and the loss turns NaN far from its cause. Set the environment variable LUCID_STRICT_INDEX=1 (before the call, or in os.environ) to have Metal raise the same IndexError; it costs a host read of the index on every lookup, so it is meant for finding a bad label, not for the training loop. Unset, or 0, Metal behaves as before and pays nothing.

Embedding is commonly used in natural-language processing (token embeddings, position encodings), recommendation systems (item / user embeddings), and any domain where discrete categorical inputs must be projected into a continuous representation space.

Examples

Simple token embedding for a vocabulary of 100 with 16-dim vectors:
>>> import lucid, lucid.nn as nn
>>> emb = nn.Embedding(num_embeddings=100, embedding_dim=16)
>>> idx = lucid.tensor([[1, 5, 3], [0, 2, 7]], dtype=lucid.int64)
>>> y = emb(idx)
>>> y.shape    # (2, 3, 16)
(2, 3, 16)
Using padding_idx to mark a <PAD> token (index 0):
>>> emb_pad = nn.Embedding(50, 8, padding_idx=0)
>>> # Row 0 is always zero and never updated
>>> import lucid.linalg
>>> float(lucid.linalg.norm(emb_pad.weight[0])) == 0.0
True
>>> idx2 = lucid.tensor([0, 3, 0, 7], dtype=lucid.int64)
>>> emb_pad(idx2).shape
(4, 8)

See Also

  • EmbeddingBag—Efficient embedding lookup with per-bag reduction.

Used by 1

Constructors

1
dunder

__init__

→None
__init__(num_embeddings: int, embedding_dim: int, padding_idx: int | None = None, max_norm: float | None = None, norm_type: float = 2.0, scale_grad_by_freq: bool = False, sparse: bool = False, _weight: Tensor | None = None, _freeze: bool = False, device: DeviceLike = None, dtype: DTypeLike = None)

Initialise the Embedding module. See the class docstring for parameter semantics.

Class methods

1
cls

from_pretrained

→Embedding
from_pretrained(embeddings: Tensor, freeze: bool = True, padding_idx: int | None = None, max_norm: float | None = None, norm_type: float = 2.0, scale_grad_by_freq: bool = False, sparse: bool = False)

Build an Embedding whose table is embeddings.

Parameters

embeddingsTensor
A 2-D floating-point tensor; its shape is (num_embeddings, embedding_dim). The table is bound without a copy, so an in-place write through either is seen by both.
freezebool= True
True keeps the table out of training (weight.requires_grad = False). Default: True.
padding_idxint or None= None
As the constructor. It is not used to zero a row of embeddings; it only stops gradient reaching that row.
max_normfloat or None= None
As the constructor. Default: None.
norm_typefloat= 2.0
As the constructor. Default: 2.0.
scale_grad_by_freqbool= False
As the constructor. Default: False.
sparsebool= False
As the constructor. Default: False.

Returns

Embedding

The module.

Raises

ValueError
If embeddings is not 2-D.

Examples

>>> import lucid, lucid.nn as nn
>>> weight = lucid.tensor([[1.0, 2.3, 3.0], [4.0, 5.1, 6.3]])
>>> emb = nn.Embedding.from_pretrained(weight)
>>> emb(lucid.tensor([1], dtype=lucid.int64))
tensor([[4., 5.1, 6.3]])
>>> emb.weight.requires_grad
False

Instance methods

3
fn

extra_repr

→str
extra_repr()

Return a string representation of the layer's configuration.

fn

forward

→Tensor
forward(input: Tensor)

Look up embeddings for the given indices.

Parameters

inputTensor
Tensor of integer indices.

Returns

Tensor

Tensor of embedding vectors of shape (*input.shape, embedding_dim).

fn

reset_parameters

→Nonefrom _PaddedTable
reset_parameters()

Re-draw weight from N(0, 1) and zero the padding row.