Skip to Content
OchreML
Layers & Activations

Embedding

Lookup table mapping discrete integer token or categorical indices to dense vectors.

ochreml.nn.Embedding stores a trainable lookup matrix weight of shape (num_embeddings, embedding_dim) and maps 1D (N,) or 2D (batch, seq_len) integer indices into dense tensors of shape (N, embedding_dim) or (batch, seq_len, embedding_dim).

Import & Signature

import ochreml.nn as nn

emb = nn.Embedding(
    num_embeddings,
    embedding_dim,
    dtype="float32",
    device=None,
    seed=42,
)

Parameters

ParameterTypeDefaultDescription
num_embeddingsintrequiredVocabulary / lookup table size.
embedding_dimintrequiredDimensionality of each embedding vector.
dtypestr"float32""float32", "bfloat16", or "float16".
devicestr | NoneNoneTarget device.
seedint42Initialization seed.

Usage Example

import numpy as np
import ochreml.nn as nn

emb = nn.Embedding(num_embeddings=1000, embedding_dim=64)
token_ids = np.array([[1, 42, 7, 99], [5, 10, 15, 20]], dtype=np.int64)
vectors = emb(token_ids)  # shape: (2, 4, 64)

On this page