Skip to content

Input Embeddings in Transformers

The Transformer architecture, a cornerstone of modern Natural Language Processing (NLP), comprises encoder and decoder components, each containing several mechanisms and sub-layers. The very first step in processing textual input within a Transformer is the Input Embedding layer.

Input embeddings are a crucial component that converts raw text data—such as words or subword units (tokens)—into a continuous vector representation that the model can process. These dense vectors capture semantic information about the tokens, enabling the model to understand and manipulate text effectively.

This chapter explains what input embeddings are, why they are essential, and how they are implemented in Transformers, complete with a conceptual Python example to illustrate these concepts.

Input embeddings are vector representations of discrete input tokens (e.g., words, subwords, or characters). These vectors aim to capture the semantic meaning and contextual nuances of the tokens, allowing the model to discern relationships between them. For instance, words with similar meanings should have similar vector representations in the embedding space.

The role of the input embedding sublayer in a Transformer is to map these input tokens into a high-dimensional vector space, typically with a dimensionality denoted as $d_{model}$ (e.g., $d_{model} = 512$ or $d_{model} = 768$). In this space, tokens with similar semantic properties are positioned closer to each other.

Importance of Input Embeddings in Transformers

Section titled “Importance of Input Embeddings in Transformers”

Let’s understand why input embeddings are critically important in Transformer models:

Input embeddings capture semantic similarities between words or tokens from the input text. For example, the vectors for words like “king” and “queen,” or “apple” and “orange,” are likely to be relatively close in the embedding space, reflecting their semantic relationship.

Traditional methods like one-hot encoding represent each token as a sparse binary vector where only one dimension is ‘hot’ (set to 1). This becomes inefficient for large vocabularies, leading to very high-dimensional and sparse representations. Input embeddings provide a compact and dense representation (e.g., 512 dimensions instead of 50,000 for a vocabulary of 50k words), reducing computational complexity and memory requirements.

Because embeddings can capture contextual relationships and semantic nuances, they improve the model’s ability to learn patterns and generalize from the training data. This leads to better performance on various NLP tasks such as translation, summarization, and question answering.

The process of generating input embeddings in Transformers, similar to other sequence transduction models, involves the following steps:

Before input tokens can be embedded, the raw input text must be tokenized. Tokenization is the process of breaking down the text into smaller units, called tokens. Common tokenization strategies include:

  • Word-level tokenization: Splits the text into individual words. This can lead to large vocabularies and issues with out-of-vocabulary (OOV) words.
  • Subword-level tokenization: Splits text into smaller units, which can be parts of words or frequent character sequences. Algorithms like Byte Pair Encoding (BPE), WordPiece, and SentencePiece are commonly used. This approach handles OOV words effectively and manages vocabulary size. For example, the text “Transformers revolutionized NLP” might be tokenized into ["Transform", "##ers", "revolutionized", "NLP"].

The embedding layer is essentially a lookup table (or a matrix) that maps each token ID (an integer representing a unique token in the vocabulary) to a dense vector of fixed dimensions. This process involves:

  • Vocabulary: A predefined set of unique tokens that the model recognizes. Each token is assigned a unique integer ID.
  • Embedding Dimension ($d_{model}$): The size of the vector space in which tokens are represented (e.g., 512). Each token ID maps to a $d_{model}$-dimensional vector.

When a token ID is passed to the embedding layer, it retrieves the corresponding dense vector from the embedding matrix. This matrix is typically initialized randomly and then learned (updated) during the model training process.

Conceptual Implementation of Input Embeddings in Python

Section titled “Conceptual Implementation of Input Embeddings in Python”

Below is a simplified Python example using NumPy to illustrate the concept of input embeddings. In practice, deep learning frameworks like PyTorch or TensorFlow, along with libraries like Hugging Face Transformers, handle these operations much more efficiently and within a larger model architecture.

import numpy as np
# Example text and basic word tokenization
text = "Transformers revolutionized the field of NLP"
tokens = text.lower().split() # Simple tokenization by splitting on space and lowercasing
# Creating a vocabulary (mapping words to unique integer IDs)
vocab = {word: idx for idx, word in enumerate(set(tokens))} # Use set to get unique words
# Example input (sequence of token indices based on our vocabulary)
# Ensure all tokens from the original sentence are in the vocab before mapping
input_indices = np.array([vocab[word] for word in tokens if word in vocab])
print("Vocabulary:", vocab)
print("Input Indices:", input_indices)
# Parameters for the embedding layer
vocab_size = len(vocab)
embed_dim = 4 # Using a very small dimension for illustration (e.g., d_model = 512 in real Transformers)
# Initialize the embedding matrix with random values (this would be learned during training)
# Each row corresponds to the embedding of a word (token ID)
embedding_matrix = np.random.rand(vocab_size, embed_dim)
# Get the embeddings for the input indices (lookup operation)
input_embeddings = embedding_matrix[input_indices]
print("\nEmbedding Matrix (Shape: {}, {}):\n".format(vocab_size, embed_dim), embedding_matrix)
print("\nInput Embeddings (Shape: {}, {}):\n".format(len(input_indices), embed_dim), input_embeddings)
# For further learning, explore:
# - Subword tokenization (BPE, WordPiece) using libraries like Hugging Face Tokenizers.
# - torch.nn.Embedding in PyTorch or tf.keras.layers.Embedding in TensorFlow.

The Python script first tokenizes the text and creates a vocabulary mapping each unique word to an integer ID. It then initializes an embedding matrix with random values, where each row represents the embedding vector for a word ID. The embed_dim is kept small here for easy display; real Transformers use much larger dimensions (e.g., 512). Finally, it retrieves the embeddings for the input sequence.

Vocabulary: {'nlp': 0, 'field': 1, 'transformers': 2, 'of': 3, 'the': 4, 'revolutionized': 5}
# Note: Order might vary due to set usage
Input Indices: [2 5 4 1 3 0] # Corresponding to 'transformers', 'revolutionized', 'the', 'field', 'of', 'nlp'
Embedding Matrix (Shape: 6, 4):
[[0.123... 0.456... 0.789... 0.101...]
[0.234... 0.567... 0.890... 0.202...]
...
[0.789... 0.101... 0.303... 0.606...]] # Random values
Input Embeddings (Shape: 6, 4):
[[...corresponding row for 'transformers'...]
[...corresponding row for 'revolutionized'...]
...
[...corresponding row for 'nlp'...]] # Selected rows from Embedding Matrix

The actual random numbers in the output will vary each time the script is run. The key takeaway is the mapping from token IDs to dense vectors.

The input embedding sublayer is the foundational step in a Transformer model, converting raw text data into meaningful numerical representations that the model can process. By capturing semantic similarities and providing dense, efficient vectors, input embeddings enable Transformers to effectively learn complex patterns and relationships within textual data.

Understanding tokenization strategies and the concept of an embedding layer is crucial for anyone working with or building Transformer models for NLP tasks. While this chapter provided a conceptual overview and a basic Python example, real-world applications involve more sophisticated tokenizers and embedding techniques integrated within deep learning frameworks.