Represent vocabulary items as vectors
Token IDs in text models
Keras · Class
Maps nonnegative integer indices to dense vectors through a trainable embedding matrix. Each input position receives one vector of size output_dim.
Embedding stores a trainable matrix with one vector per vocabulary index. When called with integer sequences, it gathers the corresponding rows and returns a vector at each sequence position; the last output dimension equals output_dim.
keras.layers.Embedding(input_dim, output_dim, embeddings_initializer="uniform", embeddings_regularizer=None, embeddings_constraint=None, mask_zero=False, weights=None, lora_rank=None, lora_alpha=None, quantization_config=None, **kwargs)| Name | Type | Description |
|---|---|---|
input_dim | integer | Vocabulary size; valid indices range below this value. |
output_dim | integer | Width of each dense embedding vector. |
embeddings_initializer | initializer or string | Initializer for the embeddings matrix; defaults to uniform initialization. |
embeddings_regularizer | regularizer or null | Regularizer applied to the embeddings matrix. |
embeddings_constraint | constraint or null | Constraint applied to the embeddings matrix. |
mask_zero | boolean | Whether index 0 is treated as padding and masked. |
weights | array or null | Optional initial embedding weights. |
lora_rank | integer or null | Optional low-rank adaptation rank. |
lora_alpha | number or null | Scaling factor for low-rank adaptation. |
quantization_config | configuration or null | Optional quantization configuration. |
**kwargs | keyword arguments | Additional base-layer keyword arguments. |
A 3D tensor with shape (batch_size, input_length, output_dim), containing the embedding vector for each input index.
import keras
import numpy as np
layer = keras.layers.Embedding(input_dim=1000, output_dim=64)
inputs = np.array([[4, 20, 7], [3, 0, 12]], dtype="int32")
outputs = layer(inputs)
print(outputs.shape) # (2, 3, 64)Creates a vocabulary lookup layer and applies it to two integer sequences, producing one 64-dimensional vector for each input index.
Token IDs in text models
Categorical IDs in recommendation systems
Integer sequences with padding
The layer maps each nonnegative integer to a dense vector by looking up a row in its embedding matrix. With input shape (batch_size, input_length), the output shape is (batch_size, input_length, output_dim).
Follow-up: How would you represent padding? Set mask_zero=True, reserve index 0, and ensure downstream layers support masking.
input_dim is the vocabulary size, expressed as the maximum integer index plus one. output_dim is the size of every returned embedding vector.
Follow-up: If the largest valid token ID is 4999, input_dim must be 5000, while output_dim can be selected according to the model design.
mask_zero=True marks the value 0 as padding and causes the layer to generate a mask for supported downstream layers. Because zero is then reserved, index 0 should not represent a real token.
Follow-up: Why can masking fail to affect a later layer? A downstream layer may not support or propagate masks, so verify mask compatibility throughout the network.
keras.layers.TextVectorizationkeras.layers.StringLookupkeras.layers.IntegerLookupkeras.layers.Dense