Transformer (machine learning model)

Revision as of 13:29, 21 August 2026 by WikiSysopIG (talk | contribs) (Created page with "A '''transformer''' is a type of deep learning model architecture designed to process sequential or structured data using mechanisms based primarily on attention, particularly self-attention. Transformers were introduced in the 2017 paper "Attention Is All You Need" by Google researchers. The architecture was initially developed for sequence-to-sequence tasks...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

A transformer is a type of deep learning model architecture designed to process sequential or structured data using mechanisms based primarily on attention, particularly self-attention.

Transformers were introduced in the 2017 paper "Attention Is All You Need" by Google researchers. The architecture was initially developed for sequence-to-sequence tasks such as machine translation and subsequently became widely used in natural language processing, computer vision, speech processing, and other machine-learning applications.

A simplified representation is:

Input Sequence→Embedding→Self-Attention→Feed-Forward Processing→Output

Transformer models can process relationships between elements of a sequence without relying exclusively on sequential recurrence.

Architecture

A transformer typically consists of multiple layers containing attention and feed-forward components.

A simplified transformer layer can be represented as:

Input→Self-Attention→Feed-Forward Network→Output

Modern transformer architectures may also include residual connections, normalization layers, positional representations, and other components.

The original transformer architecture contained separate encoder and decoder stacks.

Encoder and decoder

The original architecture consists of:

  • an encoder, which processes the input sequence;
  • a decoder, which generates the output sequence.

A simplified sequence-to-sequence representation is:

Input Sequence→Encoder→Decoder→Output Sequence

The encoder converts the input into contextual representations that can be used by the decoder.

The decoder generates output tokens while attending to previously generated tokens and, in the original architecture, representations produced by the encoder.

Attention mechanism

Attention allows a model to assign different weights to elements of an input sequence when constructing a representation.

A simplified attention operation is:

Attention⁡(Q,K,V)=softmax⁡(QKTdk)V

where:

  • Q = query matrix;
  • K = key matrix;
  • V = value matrix;
  • dk = dimensionality of the key vectors.

The scaling factor dk helps control the magnitude of the dot products before the softmax operation.

Self-attention

Self-attention is an attention mechanism in which the queries, keys, and values are derived from the same input sequence.

A simplified representation is:

Failed to parse (syntax error): {\displaystyle \text{Sequence} \rightarrow \begin{cases} Q\\ K\\ V \end{cases} \rightarrow \text{Self-Attention} \end{math> This allows each element of a sequence to incorporate information from other elements. For example, in a sentence, a token can assign greater attention to other tokens that provide relevant contextual information. ==Multi-head attention== Transformers commonly use [[Multi-head attention|multi-head attention]], in which several attention operations are performed in parallel. A simplified representation is: <math> \operatorname{MultiHead}(Q,K,V) = \operatorname{Concat} \left( \text{head}_1,\ldots,\text{head}_h \right) W^O }

where each attention head can learn different relationships within the input.

For each head:

headi=Attention⁡(QWiQ,KWiK,VWiV)

where WiQ, WiK, and WiV are learned projection matrices.

Positional information

Because self-attention does not inherently impose a sequential processing order, transformer models require mechanisms for representing positional information.

A simplified representation is:

Input Representation=Token Representation+Positional Representation

The original transformer architecture used sinusoidal positional encodings.

For position pos and dimension i, the original formulation includes:

PE(pos,2i)=sin⁡(pos100002i/dmodel)

and:

PE(pos,2i+1)=cos⁡(pos100002i/dmodel)

Modern transformer architectures may instead use learned positional embeddings or other positional encoding mechanisms.

Feed-forward network

Transformer layers commonly contain a position-wise feed-forward network following the attention mechanism.

A simplified formulation is:

FFN⁡(x)=σ(xW1+b1)W2+b2

where:

  • x = input representation;
  • W1 and W2 = learned weight matrices;
  • b1 and b2 = bias vectors;
  • σ = activation function.

The original transformer architecture used a ReLU activation function.

Residual connections and normalization

Transformer layers commonly use residual connections and normalization.

A simplified residual relationship is:

Output=Input+Transformation(Input)

Residual connections help information and gradients propagate through deep networks.

Normalization is used to stabilize and improve the training of deep transformer networks.

Tokenization and embeddings

In natural language applications, text is generally converted into discrete tokens before being processed by a transformer.

A simplified pipeline is:

Failed to parse (syntax error): {\displaystyle \text{Text} \rightarrow \text{Tokens} \rightarrow \text{Embeddings} \rightarrow \text{Transformer} \end{math> Each token is represented by a numerical vector, allowing the neural network to process linguistic information mathematically. ==Training== Transformer models are trained by adjusting their parameters to minimize a specified loss function. For a model with parameters <math>\theta} , a simplified objective is:

θ∗=arg⁡minθℒ(θ)

where ℒ represents the training loss.

For language modeling, a common objective is to predict a token based on contextual information.

A simplified autoregressive objective is:

P(x1,…,xn)=∏t=1nP(xt∣x1,…,xt−1)

The model is trained to assign high probability to appropriate next tokens.

Types of transformer models

Transformer architectures can be broadly categorized according to how their encoder and decoder components are used.

Encoder-only models

Encoder-only models use transformer encoders to produce contextual representations of input sequences.

They are commonly used for tasks such as:

  • text classification;
  • semantic representation;
  • information retrieval;
  • token classification;
  • sentence similarity.

A simplified representation is:

Failed to parse (syntax error): {\displaystyle \text{Input} \rightarrow \text{Encoder} \rightarrow \text{Contextual Representation} \end{math> ===Decoder-only models=== Decoder-only models use transformer decoder blocks and are commonly used for autoregressive generation. A simplified representation is: <math> \text{Previous Tokens} \rightarrow \text{Decoder} \rightarrow \text{Next Token} \end{math> Repeating this process allows the model to generate sequences of arbitrary length within its supported context. ===Encoder–decoder models=== Encoder–decoder models use both components. A simplified representation is: <math> \text{Input} \rightarrow \text{Encoder} \rightarrow \text{Decoder} \rightarrow \text{Output} }

This architecture is commonly associated with sequence-to-sequence tasks.

Causal attention

Decoder-based autoregressive transformers generally use causal or masked self-attention so that a token cannot directly attend to future tokens during training.

A simplified representation is:

Attention at Position t→Positions ≤t

This preserves the autoregressive property of next-token prediction.

Computational characteristics

For a sequence of length n, standard self-attention requires computing interactions between pairs of positions.

The attention matrix has approximately:

n×n=O(n2)

pairwise relationships.

Consequently, the computational and memory requirements of standard self-attention can increase substantially as sequence length grows.

Various transformer variants have been developed to reduce the computational cost associated with long sequences.

Applications

Transformers are used in a wide range of machine-learning applications, including:

  • Natural language processing;
  • machine translation;
  • text generation;
  • question answering;
  • information retrieval;
  • speech recognition;
  • image classification;
  • computer vision;
  • multimodal learning;
  • code generation;
  • document processing;
  • time-series modeling.

Their use has expanded beyond natural language because the attention mechanism can model relationships between elements in many types of structured data.

Advantages

Transformers have several important characteristics:

  • attention can model relationships between distant elements of a sequence;
  • training can be highly parallelizable compared with recurrent architectures;
  • the architecture can be scaled to large numbers of parameters;
  • the same general architecture can be adapted to different modalities and tasks;
  • pretrained transformer models can be fine-tuned or adapted for downstream applications.

Limitations

Transformer models also have limitations.

These can include:

  • high computational requirements;
  • substantial memory requirements;
  • increasing attention costs for long sequences;
  • dependence on large training datasets for many applications;
  • potential generation of inaccurate or fabricated information;
  • sensitivity to training data and model configuration;
  • substantial energy and infrastructure requirements for large-scale training and inference.

The limitations vary substantially among model architectures and applications.

Transformer and recurrent neural networks

Transformers differ from traditional recurrent neural networks (RNNs) in how sequence relationships are processed.

A simplified recurrent representation is:

ht=f(xt,ht−1)

where the current hidden state depends on the previous hidden state.

A simplified transformer representation is:

ht=f(x1,…,xn)

where the representation at a position can incorporate information from other positions through attention, subject to the model's attention structure.

Transformers therefore provide a different mechanism for modeling long-range relationships and enable greater parallelization during training.

Scaling

Transformer models can be scaled by increasing parameters, training data, computational resources, and other architectural dimensions.

A simplified relationship is:

Model Capability→f(Parameters,Data,Compute,Architecture)

The relationship between scale and model performance is not universally linear, and practical performance depends on model architecture, data quality, optimization, evaluation methodology, and deployment conditions.

See also