Transformers are a type of neural network architecture designed for sequence-to-sequence tasks such as machine translation, text summarization, and text generation. They were first introduced in the paper "Attention Is All You Need" by Vaswani et al. in 2017.
Key Components
Self-Attention Mechanism: This is the core component of a transformer. It allows the model to focus on different parts of the input sequence when computing its representation. The self-attention mechanism is based on the idea of spatial attention, where the model computes the attention weights between all pairs of elements in the input sequence.
Encoder: The encoder is responsible for taking the input sequence and generating a continuous representation of it. This representation is called the encoder output.
Decoder: The decoder takes the encoder output and generates the output sequence.
Transformer Architecture
The transformer architecture consists of an encoder and a decoder. The encoder has two main components:
Encoder Layers: Each encoder layer consists of two sub-layers:
Self-Attention Mechanism: Computes the attention weights between all pairs of elements in the input sequence.
Feed-Forward Network (FFN): Computes a non-linear transformation of the output of the self-attention mechanism.
Layer Normalization: Normalizes the output of each sub-layer to have a standard normal distribution.
The decoder has a similar architecture to the encoder, but with an additional sub-layer:
Decoder Layers: Each decoder layer consists of two sub-layers:
Self-Attention Mechanism: Computes the attention weights between all pairs of elements in the input sequence.
Encoder-Decoder Attention Mechanism: Computes the attention weights between the encoder output and the decoder input.
Feed-Forward Network (FFN): Computes a non-linear transformation of the output of the encoder-decoder attention mechanism.
Layer Normalization: Normalizes the output of each sub-layer to have a standard normal distribution.
Example using NumPy
We will implement a simple transformer architecture using NumPy. This example will be a simplified version of the original transformer architecture.
python
import numpy as np
# Define the input sequence
input_sequence = np.array([[1, 2, 3], [4, 5, 6]])
# Define the number of attention heads
num_heads = 2
# Define the embedding size
embedding_size = 10
# Define the feed-forward network size
ffn_size = 20
# Define the number of encoder layers
num_encoder_layers = 2
# Define the number of decoder layers
num_decoder_layers = 2
# Define the self-attention mechanism
def self_attention(query, key, value):
# Compute the attention weights
attention_weights = np.matmul(query, key.T) / np.sqrt(embedding_size)
attention_weights = np.exp(attention_weights - np.max(attention_weights, axis=1, keepdims=True))
attention_weights = attention_weights / np.sum(attention_weights, axis=1, keepdims=True)
# Compute the attention output
attention_output = np.matmul(attention_weights, value)
return attention_output
# Define the encoder
def encoder(input_sequence):
encoder_output = input_sequence
for _ in range(num_encoder_layers):
# Compute the self-attention mechanism
attention_output = self_attention(encoder_output, encoder_output, encoder_output)
# Compute the feed-forward network
ffn_output = np.matmul(attention_output, np.random.rand(embedding_size, ffn_size)) + np.matmul(attention_output, np.random.rand(embedding_size, ffn_size))
ffn_output = np.tanh(ffn_output)
# Normalize the output
encoder_output = 0.5 * (encoder_output + ffn_output)
return encoder_output
# Define the decoder
def decoder(encoder_output):
for _ in range(num_decoder_layers):
# Compute the self-attention mechanism
attention_output = self_attention(encoder_output, encoder_output, encoder_output)
# Compute the encoder-decoder attention mechanism
encoder_decoder_attention_output = self_attention(attention_output, encoder_output, encoder_output)
# Compute the feed-forward network
ffn_output = np.matmul(encoder_decoder_attention_output, np.random.rand(embedding_size, ffn_size)) + np.matmul(encoder_decoder_attention_output, np.random.rand(embedding_size, ffn_size))
ffn_output = np.tanh(ffn_output)
# Normalize the output
encoder_output = 0.5 * (encoder_output + ffn_output)
return encoder_output
# Run the encoder
encoder_output = encoder(input_sequence)
# Run the decoder
decoder_output = decoder(encoder_output)
print(decoder_output)
This code defines a simple transformer architecture using NumPy. The self_attention function computes the self-attention mechanism, the encoder function defines the encoder layers, and the decoder function defines the decoder layers. The encoder function takes the input sequence and generates a continuous representation of it, and the decoder function takes the encoder output and generates the output sequence. The code then runs the encoder and decoder to generate the output sequence.
“Explain easy to understand transformers and show me an example using only numpy.” Conversation with llama-3.1-8b-instruct, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m851hrvqp48xb38ydsbg86ep