Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

Embedding

Maps nonnegative integer indices to dense vectors through a trainable embedding matrix. Each input position receives one vector of size output_dim.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
input_dimintegerVocabulary size; valid indices range below this value.
output_dimintegerWidth of each dense embedding vector.
embeddings_initializerinitializer or stringInitializer for the embeddings matrix; defaults to uniform initialization.
embeddings_regularizerregularizer or nullRegularizer applied to the embeddings matrix.
embeddings_constraintconstraint or nullConstraint applied to the embeddings matrix.
mask_zerobooleanWhether index 0 is treated as padding and masked.
weightsarray or nullOptional initial embedding weights.
lora_rankinteger or nullOptional low-rank adaptation rank.
lora_alphanumber or nullScaling factor for low-rank adaptation.
quantization_configconfiguration or nullOptional quantization configuration.
**kwargskeyword argumentsAdditional base-layer keyword arguments.

Returns

A 3D tensor with shape (batch_size, input_length, output_dim), containing the embedding vector for each input index.

Key points

  • Each integer index is converted into a trainable dense vector whose width is output_dim.
  • input_dim must be one greater than the largest index that the layer can receive.
  • output_dim determines the final dimension of the output tensor.
  • embeddings_initializer controls how the embedding matrix is initialized.
  • embeddings_regularizer applies regularization to the stored embedding matrix.

Common mistakes

  • Inputs must be nonnegative integers, and every index must be within the vocabulary range defined by input_dim.

Example: Embed integer sequences

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.

When to use it

Represent vocabulary items as vectors

Token IDs in text models

Learn user or item representations

Categorical IDs in recommendation systems

Embed variable-length sequences while preserving padding masks

Integer sequences with padding

Interview questions

QWhat does Keras Embedding compute?

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.

QWhat do input_dim and output_dim mean?

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.

QHow does mask_zero affect an Embedding layer?

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.TextVectorization
  • keras.layers.StringLookup
  • keras.layers.IntegerLookup
  • keras.layers.Dense

Source: Embedding layer - Keras. Checked 2026-10-15.