Wiki Foundations3 Concepts and Frameworks of AI

3.3 Transformer

Guolong Li, Ruocan Lu Updated 35 min read
Transformer: A Novel Architecture Based on Attention

From completely rewriting Natural Language Processing to reshaping Computer Vision, the triumph of the Transformer architecture is beyond doubt. Yet compared to the intuitive design philosophies of CNNs and RNNs—such as spatial locality and linear sequentiality—the Transformer comes with a daunting complexity. Composed of a series of seemingly disparate components like self-attention, multi-head mechanisms, and positional encodings, its core essence often eludes beginners.

The mission of this chapter is to cut through this fog. We will reveal that the Transformer is not a rigid “master key,” but rather a highly flexible “design platform.” Understanding its core philosophy of “weak inductive bias and strong universality” not only explains why it is so powerful, but also establishes the theoretical foundation for “transforming” and applying it to specialized domains like geospatial intelligence—reintegrating critical domain priors along the way. We hope the explorations in this chapter will inspire readers to adapt and apply this formidable architecture.

Transformer — A Novel Architecture Based on Attention#

In the previous chapter, after introducing the attention mechanism in RNNs, we left an open question: if the attention mechanism alone suffices to capture long-range dependencies, do we still need the inherent recurrent structure of RNNs?

The birth of the Transformer was the loudest and most definitive answer to this question. The seminal 2017 paper Attention Is All You Need, with its manifesto-like title, heralded the arrival of a new era. It completely discarded the sequential recurrent structure of RNNs, centering instead on a pure, highly parallelizable self-attention mechanism combined with an appropriate method for injecting positional information.

The resulting Transformer architecture not only achieved revolutionary breakthroughs in performance and efficiency on tasks like machine translation, but more importantly, demonstrated a subversive idea to the entire field: the essence of a sequence may not be “recurrence,” but simply “association.”

Building a Universal Relational Reasoning Engine: Inside the Transformer Block#

The Transformer’s formidable ability to discover “associations” stems from the ingenious design of its stackable core module—the Transformer Block.

Through the synergy of several key components, this module elegantly answers a critical question in representation learning: “How can an element efficiently discover relationships within a massive collection of information and update its own representation accordingly?”

Self-Attention: Dynamically Capturing Associations in a Global View#

This is the absolute core of the new paradigm. It endows the model with an unprecedented capability: examining all elements in a sequence within a single computation and dynamically scoring the relevance between any pair of elements.

The core idea is to have every element in the sequence simultaneously play three roles: Query (Q), Key (K), and Value (V).

We can imagine a “brainstorming” meeting scenario. When a participant (an input element \mathbf{x}_i) prepares to speak, they will:

  1. Propose a topic (Query, q_i): The information they seek to understand and associate with—asking, “What do I care about?”
  2. Display a tag (Key, k_i): The “keyword” of their own information, telling others, “What do I represent?”
  3. Prepare reference material (Value, v_i): The actual informational content they possess and are ready to share with relevant parties.

These three role vectors are all derived from the original element vector \mathbf{x}_i via independent linear transformations (multiplying by learnable weight matrices W^Q, W^K, W^V):

q_i = \mathbf{x}_i W^Q, \quad k_i = \mathbf{x}_i W^K, \quad v_i = \mathbf{x}_i W^V

Next, the process of “association” officially begins.

To update its own information, the Query vector q_i of the i-th element “inspects” the Key vectors k_j of all elements in the room (including itself), computing their dot product to determine their “compatibility.”

After scaling and Softmax normalization, these compatibility scores become a set of weights representing how much attention the i-th element should “draw” from every other element.

Finally, taking a weighted sum of the Value vectors v_j of all elements using these weights yields a brand-new representation tailored to the i-th element, now infused with global contextual information.

In matrix form, this process is executed in parallel for all elements at once, captured by a concise and powerful formula:

\text{Attention}(Q,K,V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V
Scaled Dot-Product Attention and Multi-Head Attention
Left: Scaled Dot-Product Attention, the fundamental unit of relational computation in the Transformer. Right: Multi-Head Attention runs multiple independent attention "heads" in parallel (each performing a Scaled Dot-Product Attention computation) and combines their outputs, capturing richer relational information across different representation subspaces.

Multi-Head Attention: Synthesizing Insights from Multiple Subspaces#

Real-world relationships are multidimensional. For instance, two words might simultaneously share a syntactic “subject-verb” relationship and a semantic “synonym” relationship. Relying on a single set of Q, K, and V might force the model to attend to one at the expense of the other.

The philosophy behind Multi-Head Attention is to replace solo decision-making with a “panel of experts.”

Instead of performing a single attention calculation, it linearly projects Q, K, and V and splits them along their feature dimensions into multiple independent “heads” (e.g., 8 heads).

Each head operates in a distinct “subspace,” performing its own attention computation in parallel without interference—much like having syntax experts, semantics experts, and coreference experts examine the sequence simultaneously and produce their own analytical reports.

Finally, the “analytical reports” from all experts (the output vectors of each head) are concatenated and fused through a final linear projection, yielding a richer final output that synthesizes multiple perspectives.

This mechanism vastly enhances the model’s capacity to capture diverse and complex relationships.

Information Processing and Stabilization: Feed-Forward Networks and the “Sandwich” Structure#

After gathering rich global relational information via multi-head attention, the model needs a pipeline to “digest” this information and ensure stability across the entire deep network.

This is accomplished by two other key components within the Transformer Block:

  • Feed-Forward Network (FFN): After passing through the attention layer, each element independently goes through a small feed-forward network (typically a two-layer MLP). Acting like an “information refinement station,” it applies a non-linear transformation to the context-infused representation, deepening comprehension and boosting the model’s expressive power.

  • “Add & Norm” Stabilization Structure: To allow information to flow effectively through dozens or even hundreds of layers without degradation, each major computational unit (both multi-head attention and the FFN) is wrapped in a “sandwich” structure. This structure consists of a residual connection (Add) and Layer Normalization (Norm). The residual connection builds an “information superhighway” that routes the input directly to the output so original information is never lost; Layer Normalization “calibrates” the data after each step, preventing drastic fluctuations in data distribution.


Self-attention, the multi-head mechanism, and the stable update pipeline formed by the feed-forward network and “Add & Norm” together constitute the Transformer Block—a powerful, repeatable “Lego brick” that lays the foundation for deep relational reasoning networks.

Having thoroughly understood the core components of a Transformer Block, the next step is to explore how these modules are assembled and applied.

One of the most fundamental and critical distinctions arises from the directionality of the attention mechanism itself. Depending on the rules governing information flow, attention takes two primary forms:

  • Bidirectional Attention: Any element in the sequence can attend to all other elements.
  • Unidirectional or Causal Attention: An element can only attend to elements that precede it.

To first appreciate the Transformer’s purest capability for global relational modeling, we begin with the more intuitive and general form: bidirectional attention.

The effectiveness of allowing direct interaction between arbitrary elements was quickly validated across two major AI domains.

In Natural Language Processing, models exemplified by BERT leveraged deep bidirectional attention to simultaneously fuse full contextual information from both the left and right sides of a word, achieving revolutionary breakthroughs in text understanding tasks.

Around the same time, Computer Vision welcomed the challenge of the Vision Transformer (ViT).

ViT splits an image into a sequence of “patches” and feeds them into a Transformer just like tokens in a sentence. Experiments proved that, given sufficient data scale, this pure attention model can match or even surpass the performance of CNNs that had long dominated image recognition.

The success of BERT and ViT forcefully demonstrated the remarkable universality of the Transformer’s core philosophy.

Breaking Permutation Invariance with Positional Awareness#

Behind this monumental success, however, pure attention also exposed a fundamental limitation: Permutation Invariance.

Because the model computes pairwise relationships across all elements equally, it is inherently blind to element order. To the model, “I eat an apple” and “An apple eats me” are structurally indistinguishable.

This limitation is obviously fatal, as order and position are central to semantics in the vast majority of real-world data.

Consequently, to make the Transformer effective in practice, a key challenge is breaking this permutation invariance and enabling the model to “perceive” element positions.

Researchers have explored two primary technical pathways to achieve this.

The first pathway is Positional Embedding.

Its core idea is to add a vector representing each element’s position directly to its input representation before feeding the data into the model.

For example, the original Transformer paper employed fixed Sinusoidal Positional Embeddings, computing a unique positional vector based on an element’s absolute position in the sequence using sine and cosine functions of varying frequencies.

For a position pos and dimension index i, the encoding is calculated as:

PE_{(pos,2i)} = \sin\left(\frac{pos}{10000^{2i/d_{\text{model}}}}\right)
PE_{(pos,2i+1)} = \cos\left(\frac{pos}{10000^{2i/d_{\text{model}}}}\right)

This approach scales gracefully: as sequence length changes, a corresponding positional embedding can always be computed on the fly.

Models like ViT, by contrast, adopt learnable absolute positional embeddings, letting the model decide during training how best to inject positional information for each image patch.

Vision Transformer and Positional Embeddings
Unlike text sequences of variable length, the patch sequences processed by ViT have a fixed length. This "one-off" partitioning strategy means the model does not need to adapt to unknown sequence lengths, so ViT originally learned a dedicated positional embedding vector for each of its finite patch positions. To improve adaptability across varying image resolutions, many subsequent vision models have adopted more flexible, scalable schemes such as 2D RoPE or linear biases, as introduced below.

The second pathway is modifying the attention computation directly.

Since the ultimate goal is for attention scores to vary with position, why not build positional awareness directly into the attention calculation itself?

Rather than altering the input, these methods incorporate relative positional information between elements directly when computing attention scores. Two prominent representatives are ALiBi and RoPE.

ALiBi (Attention with Linear Biases)

ALiBi adds a fixed bias term directly to the attention scores—proportional to the distance between elements—causing the model to naturally and implicitly favor nearby tokens.

Conceptually, its attention score computation can be expressed as:

\text{AttentionScores} = \frac{QK^T}{\sqrt{d_k}} + \text{Bias Proportional to Relative Distance}

RoPE (Rotary Positional Embedding)

RoPE encodes positional information as a rotation matrix, “rotating” the Query and Key vectors before computing their dot product.

Its elegance lies in a mathematical property: the dot product of two vectors rotated by their respective absolute positions is equivalent to rotating one vector by a matrix that depends solely on their relative position.

For two elements at positions m and n, the score calculation satisfies:

\begin{aligned} \text{Score}(\mathbf{q}_m,\mathbf{k}_n) &= (\mathbf{R}_m\mathbf{q}_m)^T (\mathbf{R}_n\mathbf{k}_n) \\ &= \mathbf{q}_m^T\mathbf{R}_{m-n}\mathbf{k}_n \end{aligned}

This means the attention score naturally becomes a function of relative position, and as distance increases, the periodic nature of the inner product naturally introduces a long-term decay effect.

Together, these methods resolve the core challenge of positional awareness.

At a fundamental level, however, whether modifying the inputs or modifying the attention calculation, both pathways serve to “patch” the permutation invariance of pure attention.

This prompted a third, more radical pathway: if the underlying data exhibits exceptionally strong intrinsic regularities (such as pronounced periodicity), why not replace the general-purpose attention engine altogether with a more specialized global information-mixing mechanism?

Drawing inspiration from signal processing, these approaches turn their gaze to the frequency domain.

For example, Autoformer, designed specifically for time-series data with clear periodicity, replaces self-attention with a novel Auto-Correlation mechanism. This mechanism efficiently discovers dominant periods in the frequency domain and aggregates information accordingly—putting the right specialist on the job.

Another class of methods, such as AFNO (Adaptive Fourier Neural Operator), goes even further by applying Fast Fourier Transforms to shift data into the frequency domain, directly processing and mixing global frequency components. It excels at handling smooth, continuous gridded data like meteorological observations.

These methods trade away some universality in exchange for substantially higher efficiency and performance on specific data modalities.

When the core engine of the Transformer is no longer the familiar “dot-product attention,” but is instead replaced by auto-correlation or Fourier transforms, we find ourselves in a “Ship of Theseus” philosophical conundrum: if you replace the core engine of a Transformer, should it still be called a Transformer?

Perhaps the label itself does not matter.

What matters is that explorations across all three pathways jointly demonstrate that the Transformer is more than a fixed model—it is an extraordinarily successful general-purpose design platform.

It provides a proven modular architecture (residual connections, layer normalization, multi-head projections, etc.) that allows researchers to flexibly plug in, patch, or even swap out core components like assembling a PC, striking an ideal balance between universality and domain specificity.

Evolution of FFN and Normalization Layers#

Having addressed the core issue of positional awareness, let us temporarily shift our gaze away from the attention mechanism.

A complete Transformer Block comprises not only the attention engine, but also two vital supporting components: the Feed-Forward Network (FFN) and the Normalization layer.

Though less flashy than attention, their continuous refinement plays an equally crucial role in boosting model performance and stabilizing deep training—reflecting researchers’ relentless pursuit of architectural perfection.

Optimizing the Feed-Forward Network (FFN)#

The standard Transformer FFN is a simple two-layer MLP, typically using ReLU as its activation function.

Subsequent research revealed that introducing smoother activation functions and gating structures can significantly elevate model performance.

1. From ReLU to Smoother Activation Functions

Researchers discovered that smoother activation functions like GELU (Gaussian Error Linear Unit) yield better optimization dynamics than the sharp kink of ReLU.

GELU quickly became the standard configuration in early large-scale models such as BERT and the GPT series.

2. The Gated Linear Units (GLU) Family

This represents the most significant evolutionary direction for FFNs in recent years.

Its core idea is to introduce a “gating” mechanism into the information flow, allowing the network to dynamically control which information passes through based on the input content.

Among these variants, SwiGLU stands out as one of the most successful and widely adopted. It linearly projects the input into two paths, applying the Swish activation function to one path so it acts as a “gate” that multiplies the other.

This gated structure has proven more effective than traditional ReLU or GELU networks—achieving superior performance with fewer parameters—and has become standard equipment in leading modern LLMs (such as the Llama series).

3. Mixture of Experts (MoE)

This is a far more radical architectural transformation.

Instead of routing every input token through the exact same FFN, MoE sets up multiple parallel FFN “experts” within each layer.

For each incoming token, a lightweight “router” network decides which expert (or subset of experts) should process it.

Core Advantage: MoE allows a model’s total parameter count to scale to staggering sizes while keeping the active computational cost per token constant. It is a highly effective way to massively expand model capacity without increasing inference cost.

In recent years, many leading open-weights models have embraced this architecture; DeepSeek-V3, for instance, features an ultra-efficient MoE design as one of its core innovations.

Reinventing Normalization Layers#

Standard Layer Normalization (LayerNorm) is essential for stabilizing Transformer training.

Nevertheless, researchers have explored alternatives that are either computationally leaner or functionally more expressive.

RMSNorm (Root Mean Square Norm)

RMSNorm is a streamlined simplification of LayerNorm.

It eliminates the mean-centering step, scaling inputs solely by their root mean square.

This simplifies the math and reduces runtime overhead, while empirical results show it matches or even surpasses LayerNorm in model performance and training stability—leading to its widespread adoption across modern LLMs.

Adaptive Normalization

Rather than a brand-new normalization algorithm, this class of methods is a functional extension of existing normalization layers, primarily designed for conditioning injection.

Take adaLN (Adaptive LayerNorm) as an example. Instead of using fixed, static learnable scale and shift parameters, it employs a small network to dynamically predict these parameters based on external conditions (such as the timestep t or class label c in diffusion models).

This turns the normalization layer itself into a “control valve” capable of receiving instructions and modulating model behavior—an exceptionally efficient technique for conditional generation.

Causal Attention: The Cornerstone of Generative Tasks#

Having demonstrated the versatile power of bidirectional attention, we now return to the second form mentioned earlier: Unidirectional Attention.

Its core mechanism is remarkably simple: by introducing a “mask” during the attention computation, it forces every element to “see” only preceding elements that already exist, completely blocking access to any future elements.

This unidirectional structure aligns naturally with the demands of tasks like language generation.

Language generation is largely an autoregressive process: building upon words already written, we predict and write the next word one step at a time.

Causal attention is the exact mathematical realization of this process. Its masking mechanism guarantees that when predicting the t-th token, the model can only draw upon information from the first t-1 tokens—directly modeling the conditional probability P(x_t \mid x_{<t}).

Moreover, this autoregressive paradigm driven by causal attention is general-purpose and extends beyond one-dimensional text.

In image generation, for instance, models like iGPT (Image GPT) instantiate this exact concept: they flatten image pixels or patches into a sequence following a fixed raster-scan order (e.g., top-left to bottom-right), and then use causal attention to predict the next visual element one by one, just like generating text.

At first glance, this unidirectional mechanism—incapable of “seeing the whole picture”—might seem weaker than bidirectional attention, which fuses full global context.

Yet this very informational “shackle” turns out to be its unmatched advantage in generative tasks.

Here is why:

From “Comprehender” to “Creator”

Bidirectional attention is a powerful “relational comprehender,” excelling at filling in blanks or refining representations across an existing panorama of information.

The restriction of causal attention, by contrast, forces the model to learn how to “create” from scratch.

At every step, it must make probabilistic predictions about the unknown based on the known. By nature, it is learning a generative process—capturing the causal dependencies of the underlying data. Hence, it is widely known as Causal Attention.

Beyond this fundamental philosophical shift, we can also appreciate the unique training advantages of causal attention from two concrete technical perspectives:

1. Higher Training Efficiency

In BERT-style bidirectional Masked Language Modeling (MLM), typically only a small fraction of input tokens (around 15% in text, though up to 75% in vision) are masked out and contribute to the training loss.

In autoregressive modeling, thanks to a technique called teacher forcing, nearly every single element in the sequence serves as an independent “prediction exercise,” providing dense gradient signals for model optimization.

Consequently, given the exact same volume of training data, causal attention models extract a much denser learning signal than masked models.

2. Harder Task, Stronger Representations

Because causal attention has less context to rely on when making predictions, its prediction task is inherently harder.

Training under such “shackled” high-difficulty conditions is widely believed to compel the model to learn deeper, more generalizable feature representations.

While this remains an active area of research, a notable trend in recent years is the growing adoption of causally trained decoder models as powerful general-purpose embedding models—even outperforming traditional bidirectional MLM models on several retrieval and representation benchmarks.

This indirectly corroborates the idea that a more challenging causal prediction objective can indeed forge superior underlying representations.

The Miracle of Scale: From Causal Attention to Emergent Intelligence#

In the previous section, we explored why causal attention serves as the cornerstone of generative tasks.

Now, we turn our focus to the most glorious culmination of this paradigm: Large Language Models (LLMs).

It is precisely these massive models built around causal attention that have propelled artificial intelligence to unprecedented heights.

Original Transformer Encoder-Decoder Architecture
The original Transformer model proposed in the seminal paper inherited and advanced the Encoder-Decoder architecture common in machine translation tasks.

Before diving into the rise of LLMs, it helps to clarify some historical terminology.

Originally designed for machine translation, the first Transformer inherited the mainstream Encoder-Decoder architecture of its era.

In that setup, the Encoder uses bidirectional attention to “read” and “comprehend” the entire source sentence, while the Decoder uses causal attention to generate the target translation word by word while cross-attending to the Encoder’s representations.

Subsequent researchers realized that these two halves could be decoupled and used independently, giving rise to two primary technical lineages:

  • Encoder-only lineage: Exemplified by BERT, its core is bidirectional attention. Naturally suited for deep contextual comprehension, it came to dominate Natural Language Understanding (NLU).

  • Decoder-only lineage: Exemplified by the GPT series, its core is causal attention. Naturally suited for autoregressive creation, it came to dominate Natural Language Generation (NLG).

Although the terms “Encoder-only” and “Decoder-only” remain ubiquitous, their literal “encoding/decoding” connotations no longer fully reflect what modern models actually do. In the discussion that follows, we will primarily frame them through the lens of “bidirectional” versus “causal” attention.

Why Generative Models?#

As model scale continued to expand, researchers noticed that generative models built on causal attention began exhibiting astonishing leaps in performance and versatility that far exceeded expectations.

Not only could they generate long-form text with unprecedented fluency, coherence, and logical rigor, but more importantly, without any task-specific fine-tuning, they began spontaneously demonstrating the capacity to solve a diverse array of unseen problems.

The shockwave of this phenomenon was immense, prompting both academia and industry to shift their primary focus from bidirectional comprehension models toward causal generative models.

This shift raises a profound and critical question: why did “generative” models based on causal attention—rather than “comprehension” models based on bidirectional attention—ultimately pave the path toward general intelligence and display such extraordinary versatility?

The answer likely lies in the universality and open-endedness of the objective itself.

BERT’s “fill-in-the-blank” objective is relatively closed-ended; it excels at inferring missing pieces within an existing contextual frame. GPT’s “text completion” objective—predicting the next token—is fundamentally an infinitely open-ended framework.

Whether answering a question, translating a sentence, writing code, or performing logical deduction, all of these complex tasks can be seamlessly cast into a single unified format: “given a preceding context (prompt), predict the most plausible continuation (completion).”

At a deeper level, the seemingly mundane task of “next-token prediction” actually serves as a remarkably powerful Proxy Task.

To accurately predict the next word across vast, diverse corpora of human text, the model is forced to learn and internalize the underlying rules, world knowledge, and logical structures that produced those texts in the first place.

To complete a sentence describing a law of physics, it must “understand” physics; to continue a block of source code, it must “learn” programming.

Thus, the causal generative objective compels the model to internally construct a vast and intricate World Model.

The impressive capabilities we observe on the surface are the external manifestations of this internal world model.

Scaling Laws and Emergent Abilities#

The true catalyst of the LLM revolution was not a brand-new algorithm, but an astonishing empirical discovery known as Scaling Laws.

Research revealed that when a Transformer’s parameter count, training dataset size, and compute budget are scaled up in tandem past a certain critical threshold, model performance not only improves predictably, but the model also spontaneously “unlocks” brand-new capabilities that are completely absent in smaller models.

This phenomenon—where quantitative scaling triggers a qualitative leap—is known as Emergent Abilities.

Marked by the release of GPT-3, researchers discovered that without any task-specific fine-tuning, ultra-large models trained purely on “next-token prediction” spontaneously acquired the ability to perform:

  • In-Context Learning: Without updating a single model weight, simply providing a few input-output examples (few-shot) inside the prompt allows the model to grasp and execute a brand-new task on the fly.

  • Chain-of-Thought (CoT) Reasoning: When confronted with complex problems, the model can autonomously break the problem down into intermediate reasoning steps before arriving at a final answer, dramatically boosting its logical and mathematical accuracy.

  • Instruction Following: The model can comprehend and execute complex, multi-step instructions expressed in natural language.

These emergent abilities proved that large-scale causal attention models had undergone a phase transition—from mere “text generators” into “proto-agents” equipped with rudimentary general problem-solving skills.

Yet this raw intelligence remains passive and unpolished; while brimming with latent potential, it does not yet proactively or reliably assist humans in the ways we expect.

To bridge this gap and sculpt “raw intelligence” into a dependable tool, researchers developed a core technical pipeline known as Alignment.

Alignment: Sculpting Emergent Intelligence into Usable Tools#

The first step of alignment is Instruction Tuning.

Through Supervised Fine-Tuning (SFT) on tens of thousands of high-quality “instruction–response” pairs, this stage teaches the model to understand and follow human interaction formats.

Its core objective is to transform a pretrained base model that only knows how to “autocomplete text” into a helpful “assistant” capable of engaging through Q&A, summarization, and creative writing.

Building on this foundation, the second step—Reinforcement Learning from Human Feedback (RLHF)—aims to align the model’s outputs with subtle, complex human preferences.

This process trains a separate “Reward Model” to mimic human evaluative judgment, and then uses reinforcement learning algorithms to optimize the language model toward maximizing that reward—teaching it to generate higher-quality, safer, and more human-preferred responses.

The ultimate goal of alignment is to turn the model into an Agent capable of solving real-world problems.

Beyond precise instruction following, this is typically achieved by equipping the model with the ability to use external Tools.

A well-aligned model can recognize the boundaries of its own internal knowledge and emit Function Calling commands to query search engines, run code interpreters, or interact with external APIs—evolving from an isolated “language specialist” into an active “doer” that interacts with the digital world to execute complex workflows.

From Chain-of-Thought to the Art of Decision-Making: Exploring Superior Reasoning Paths#

Even so, between an instruction-following, tool-using “doer” and a truly master problem solver lies one crucial gap: the depth of Reasoning.

The starting point of this exploration grew out of a simple yet profound discovery: Chain-of-Thought (CoT).

Researchers found that simply prompting a model to “think step by step” dramatically elevates its performance on logic, math, and coding benchmarks.

CoT demonstrated that the “process of thinking” is just as vital as the “final answer”—serving as a powerful way to elicit a model’s latent reasoning capacity.

This mirrors how humans tackle hard problems: jotting down intermediate steps helps us spot non-obvious connections and makes our final conclusions far more reliable.

Yet the linear nature of CoT is also its Achilles’ heel: like a one-way street, once an early step goes astray, the model struggles to recover.

To overcome this limitation, more comprehensive frameworks like Tree of Thoughts (ToT) emerged, allowing the model to branch out and explore multiple reasoning paths simultaneously.

However, branching introduces a new dilemma: when the reasoning “tree” grows lush and exponentially large, how does the model decide which branch to prioritize? How does it avoid getting lost in endless deliberation—becoming a “giant in thought, but a dwarf in action”?

This is precisely where Reinforcement Learning (RL) re-enters the stage in a transformative way.

RL is brought in to solve the meta-problem of “how to think.”

Under this paradigm, the model’s “action” is no longer just emitting a single conversational response, but making strategic reasoning decisions (e.g., “Which branch should I explore next?”, “Wait, let me double-check that step”). Only when a complete reasoning trajectory successfully arrives at the verified ground-truth answer does the model receive a positive “reward.”

Through extensive trial-and-error exploration, the model moves beyond blind search and internalizes a high-efficiency reasoning policy.

In doing so, reinforcement learning elevates the LLM from a mere “executor of reasoning steps” into a higher-order “decision-maker” capable of planning, critiquing, and optimizing its own thought processes.

Scaling Laws and the Evolution of LLM Reasoning Paradigms
Left: Scaling Laws established a clear trajectory for boosting model capabilities by expanding scale; the timeline illustrates the rapid iteration of frontier models across major tech companies and research labs since 2023. Right: The evolution of LLM reasoning paradigms, progressing from (a) direct input-output mapping, to (b) Chain-of-Thought (CoT) simulating linear step-by-step thinking, and further to (d) Tree of Thoughts (ToT), which explores, evaluates, and selects among multiple branching reasoning paths.

Cross-Disciplinary Applications: From Scientific Computing to Geospatial Intelligence#

The Transformer’s reach did not stop at language and vision; it rapidly penetrated the core domains of scientific computing.

Across these scientific arenas, although the physical subjects and tasks differ dramatically, the underlying logic of applying Transformers remains remarkably consistent and can be distilled into a universal three-step workflow:

  1. Cast domain-specific objects (such as molecules, proteins, or physical fields) into a unified sequence or set representation.
  2. Leverage the attention mechanism to capture long-range, non-local dependencies among elements in that sequence.
  3. Project the resulting relationally enriched representations back into the physical, geometric, or symbolic space required by the target task.

Transformers in Practice: Scientific Computing#

In protein structure prediction, the central challenge is that amino acid residues far apart along the one-dimensional sequence may fold close together and interact strongly in three-dimensional space.

AlphaFold feeds amino acid sequences and pairwise residue representations into an attention-based architecture, allowing attention mechanisms to autonomously discover these critical long-range evolutionary and spatial couplings—ultimately decoding them into atomic-resolution 3D coordinates.

Its successor, AlphaFold 3, brings proteins, nucleic acids, and small molecules into a single unified diffusion-and-attention framework, achieving cross-molecular complex structure prediction.

In chemical reaction prediction, the challenge stems from the long-range electronic and steric influences of functional groups.

A functional group located far from the reactive center can dictate the final reaction product through electronic effects or steric hindrance.

By casting reactants and reagents into SMILES text sequences, the Molecular Transformer uses self-attention to automatically model the intricate interactions between distant functional groups and reactive centers without hand-crafted rules—achieving roughly 90% prediction accuracy on benchmark datasets.

Both examples vividly illustrate the power of our three-step workflow: whether mapping a 1D amino acid sequence to 3D atomic coordinates, or mapping a 1D molecular string to a product string, the secret lies in using attention to effectively capture long-range dependencies—the very bedrock of the Transformer’s universality.

Transformers in GeoAI#

Now, let us bring this winning paradigm back to the core domain of this book: Geospatial Artificial Intelligence (GeoAI).

Before diving into concrete applications, it is essential to appreciate the uniqueness and diversity of the data handled by GeoAI.

Geospatial data extends far beyond static maps and satellite images; it is a multimodal, multi-scale, spatiotemporally intertwined ecosystem of information.

Common geospatial data modalities include:

  • Remote Sensing Imagery: Captured by satellites or UAVs, providing a macro-level “bird’s-eye view” of the Earth’s surface—the cornerstone for monitoring land cover, urban expansion, and environmental change.

  • Street View Imagery: Close-range observations from a “human-eye view,” capturing fine-grained details of the built environment, commercial vitality, and neighborhood character.

  • Points of Interest (POIs): Georeferenced point locations carrying semantic categories and attributes—such as restaurants, shopping malls, and subway stations—forming the functional skeleton of a city.

  • Trajectory Data: Mobility traces from GPS devices, cell tower signals, and transit cards that record the spatiotemporal flows of people, vehicles, and goods—key to understanding traffic dynamics and human activity patterns.

  • Social Media Check-ins: Geotagged user-generated content reflecting spatial vibrancy, popularity, and human sentiment or perception toward specific places.

  • Other Vector Data: Road networks, administrative boundaries, and building footprints represented as points, polylines, and polygons that describe the geometry and topology of geographic entities.

Confronted with such heterogeneous data, the sheer complexity of geographic phenomena—such as how a major transit hub influences commuting flows several kilometers away, or how commercial centers functionally couple with residential districts—demands models capable of modeling interactions across long distances and diverse spatial features.

This is precisely where the local receptive fields of CNNs and the sequential linearity of RNNs fall short.

The advent of the Transformer provides a brand-new toolkit for tackling this central challenge.

Following the thread of tasks and data modalities, let us examine how Transformers are put into practice in GeoAI.

Multimodal Geospatial Representation Learning (Aligning Imagery, Text, and Attributes)#

Transformers enable remote sensing/street view imagery, textual descriptions, statistical attributes, spatial coordinates, and temporal encodings to be aligned within a shared semantic space—powering natural-language place search, zero-shot retrieval, and cross-domain transfer.

In practice, researchers frequently adopt CLIP-style contrastive learning: an image tower (ViT or hierarchical ViT) and a text tower (Transformer) separately encode visual scenes and natural language descriptions. Through large-scale image-text pairing, the model learns cross-modal alignment, while geographic coordinates, map projections, and temporal features are injected into the encoding or fusion layers to imbue representations with spatial awareness.

For example, research on RSVG (Remote Sensing Visual Grounding) constructed a large-scale geospatial image-text alignment dataset and proposed a Transformer-based Multi-Granularity Visual-Language Fusion (MGVLF) module. This enables the model to ground complex natural language spatial queries—such as “the elongated white roof near the river channel in this urban scene”—directly onto precise pixels and bounding boxes.

MGVLF Multi-Granularity Visual-Language Fusion Module
Schematic diagram of the MGVLF module.

Topology-Explicit Mapping and Graph Completion (From Pixels to Routable Networks)#

The global receptive field of the Transformer provides the missing link for moving “from raw pixels to routable vector networks.”

When tree canopies, shadows, or tall buildings occlude roads and cause fragmented segments, a Transformer does more than pixel-level semantic segmentation. Leveraging its long-range dependency modeling, it infers connectivity across occluded gaps and combines topological constraints to automatically bridge missing edges while suppressing false spurs.

The result is a structurally sound road or river network ready for navigation and spatial simulation, rather than a disconnected collection of “road-like” pixels.

Compared to pure CNN approaches, this paradigm substantially boosts network connectivity metrics such as APLS (Average Path Length Similarity).

Recent works (such as MDANet) combine deformable/multi-scale attention with road geometry priors, achieving marked improvements in both completeness and topological connectivity for road extraction from high-resolution remote sensing imagery.

Spatiotemporal Dependency Modeling (From “Spatial Snapshots” to “Process Simulation”)#

Transformers unify “long-range spatial dependencies” and “causal temporal evolution” under one roof, making them ideally suited for complex dynamical systems like human mobility, urban traffic states, and numerical weather prediction.

Take Pangu-Weather as a landmark example: using a 3D Earth-Specific Transformer (3DEST) trained on global ERA5 reanalysis data for medium-range weather forecasting, it matches or surpasses operational numerical weather prediction systems across multiple meteorological variables and lead times (from 1 hour to 1 week) while running orders of magnitude faster.

This demonstrates that attention mechanisms can integrate the long-range interactions of complex physical processes across the planetary system “all at once” into data-driven spatiotemporal representations—simultaneously elevating forecasting accuracy and computational efficiency.

Back to Basics: The Deeper Logic Behind the Transformer’s Success#

Up to this point, we have witnessed a technological revolution spearheaded by the Transformer: from disruptive breakthroughs in language and vision, to emergent intelligence in LLMs, and onward to frontier applications in scientific computing and geospatial intelligence.

Why has a single, seemingly unified architecture managed to make waves across such vastly different domains?

The answer lies not in any single clever component, but in its most fundamental design philosophy.

To grasp the root of its universality, we must return to the essence of all machine learning model design: Inductive Bias.

An inductive bias is the set of prior assumptions about the structure of data that we bake into a model before it ever begins learning.

It acts like a shortcut guiding the learning process: when our built-in assumptions match the true underlying structure of the data, learning becomes vastly more sample-efficient.

Both CNNs and RNNs are textbook examples of architectures with strong inductive biases:

  • CNNs are built on the conviction that meaningful patterns are spatially local and translationally invariant. Consequently, their core operation—the convolutional kernel—is strictly confined to computing within a small local window of neighboring pixels. This strong prior makes CNNs natural specialists for grid-like imagery, but it also handicaps their ability to directly perceive long-range spatial relationships.

  • RNNs are built on the conviction that information flows sequentially and linearly across time. Thus, they process sequence elements one step at a time through a recurrent hidden state. While this makes them adept at sequential modeling, it imposes a representation bottleneck and suffers from long-term memory decay. Even when augmented with attention, an RNN merely bolts an extra “shortcut” onto its underlying sequential chain without altering its fundamental structure.

The design philosophy behind these classical models is to craft highly efficient, specialized tools for specific problem structures.

Yet the very strength of their built-in “prejudices” also defines their boundaries.

A more unified perspective is to view all of these neural architectures as performing message passing over different types of Graphs:

  • CNN: Its worldview is a Grid Graph, where edges only connect immediate spatial neighbors.
  • RNN: Its worldview is a directed Chain Graph, where edges only connect adjacent time steps.
Comparison of Graph Structures and Complexities across CNN, RNN, and Self-Attention
Table: Computational complexity per layer and maximum path length between any two nodes across architectures. Self-attention enables "direct connections" with a maximum path length of $O(1)$, whereas restricting attention to a local neighborhood of size $r$ yields a path length of $O(n/r)$. Bottom diagrams: The underlying graph topologies corresponding to Convolutional Neural Networks (CNNs) and Recurrent Neural Networks (RNNs). Source: Stanford CS224W.

The worldview of the Transformer, by contrast, is their theoretical superset: a Fully Connected Graph.

It adopts the weakest, most egalitarian inductive bias possible—harboring no preconceived notions about where “important relationships” ought to reside. Instead, it preserves the potential for direct connections between every pair of elements, letting the attention mechanism discover “who matters to whom” purely from data.

The immediate advantage of this design is that long-range dependencies—which would be missed by the local window of a CNN or diluted along the sequential chain of an RNN—can be modeled directly in a single computational step by a Transformer.

This capacity to capture complex global context in O(1) path length is one of the primary engines behind its extraordinary performance.

All the architectural enhancements we explored earlier (such as positional encodings) represent the process of selectively reintroducing necessary inductive biases back onto this general-purpose platform to suit specific tasks.

However, supreme flexibility comes at a steep price, most visibly manifested in the model’s unprecedented “data hunger.”

We can gain a deep theoretical appreciation of this trade-off through the classic lens of the Bias–Variance Dilemma in statistical learning theory.

A model’s inductive bias dictates the types of errors it is prone to make when approximating real-world laws:

  • Where an inductive bias conflicts with the true underlying data-generating process, it introduces systematic Bias.
  • Where an inductive bias is overly permissive, leaving too vast a hypothesis space for the model to search through from scratch, the model becomes hypersensitive to sample noise and spurious correlations—resulting in high Variance.

Generally speaking, strong inductive biases (like CNNs and RNNs) drastically compress the model’s search space, though their built-in assumptions may clash with complex realities—exhibiting high bias and low variance.

In practice, this means such models have a lower performance ceiling, but can be trained effectively with relatively modest amounts of data.

Conversely, a weak inductive bias (like the Transformer) makes far fewer systematic assumptions and thus carries very low bias; yet its immense degrees of freedom bring extremely high variance. This translates into an exceptionally high performance ceiling, but demands massive volumes of training data to “tame” the variance and uncover true underlying regularities.

The Transformer’s fully connected graph topology and its notorious appetite for data are the direct real-world manifestations of this principle.

This also illuminates the ultimate grail of neural architecture design: discovering and applying inductive biases that are simultaneously strong and aligned with reality.

Our earlier explorations into tailoring mechanisms to specific data structures—such as injecting positional awareness for sequences or introducing frequency-domain mixers for periodic series—are vivid examples of striving toward this ideal on top of the Transformer platform.

Beyond data hunger, the dense nature of a fully connected graph poses another pressing engineering challenge: O(n^2) computational complexity.

For a sequence of length n, standard self-attention must compute an n \times n pairwise interaction matrix, causing both compute and memory footprints to explode quadratically as sequence length grows.

To break through this quadratic bottleneck, researchers have pursued three major technical avenues:

1. Finesse Over Force: Approximate Attention#

By introducing reasonable mathematical assumptions—such as low-rank structure or sparsity—these methods avoid materializing the full n \times n attention matrix.

Linear Attention and Sparse Attention are the flagship representatives of this approach.

Linear Attention and Sparse Attention Mechanisms
Top (Linear Attention): Approximates Softmax via kernel feature maps and reorders matrix multiplication from $(QK^T)V$ to $Q(K^TV)$, bypassing the explicit computation of the quadratic $QK^T$ attention matrix. Bottom (Sparse Attention): Leverages sparsity assumptions on the attention matrix to compute only a subset of critical Query-Key pairs, pruning away vast numbers of weak or irrelevant connections.

2. A Fresh Start: Replacing Dot-Product Attention#

This pathway swaps out standard dot-product attention altogether for alternative global mixing mechanisms that possess inherently lower computational complexity while remaining adept at relational modeling.

Methods we encountered earlier like AFNO and Autoformer, along with recent structured State Space Models (such as Mamba) that have achieved remarkable success, all belong to this category.

3. Penny-Pinching Engineering: Hardware-Aware Optimization#

Rather than approximating the math, this approach computes exact, full self-attention, but employs hardware-aware systems optimizations like FlashAttention to minimize slow GPU High Bandwidth Memory (HBM) reads and writes via tiling in fast SRAM—achieving orders-of-magnitude speedups and memory savings in practice.

Getting to the Root: Returning to the Building Blocks — “Nodes” and “Edges”#

Thus far, we have thoroughly examined the Transformer’s architectural philosophy, domain adaptations, and engineering optimizations.

Yet throughout all our discussions, one fundamental question—which we have hitherto implicitly sidestepped—now rises to the surface: are the very “building blocks” we use to construct this grand edifice—the basic units of analysis fed into the model—optimally defined in the first place?

Translated into the language of graphs, we must squarely confront two sides of the same coin:

  • What is a “Node”? How should continuous or complex raw data be partitioned into meaningful elementary units (tokens) for the model to process?
  • What is an “Edge”? Once those elementary units are defined, what is the most appropriate way to characterize and compute the relationships between them?

As we have seen, researchers across different modalities have arrived at distinct practical answers.

In the text domain, the definition of a token has evolved from whole “words,” to flexible “sub-words” (via BPE or WordPiece), and even down to raw “bytes.”

In the vision domain, ViT pioneered splitting images into non-overlapping rectangular “patches”—where choosing the patch size is itself a profound trade-off: smaller patches yield longer sequences and finer spatial detail, but trigger a quadratic surge in computational cost.

Even when attention mechanisms are integrated into classic convolutional architectures (CNNs), the definition of an elementary “unit” is far from unique:

  • Spatial Attention: Treats each “spatial pixel location” across the feature map as a unit.
  • Channel Attention: Treats each entire “feature channel” as a unit, allowing the model to weigh the importance of different semantic feature maps.

Defining a “node” is far more than a mundane preprocessing step; it is a foundational modeling decision that sets the upper bound on a model’s representational capacity.

Nowhere is this more critical than in Geospatial Artificial Intelligence (GeoAI).

How should we define the “elementary token” of a city? Should it be an individual building footprint, a land parcel, a street block, or a uniform grid cell?

This choice connects directly to the classic Modifiable Areal Unit Problem (MAUP) in geography, fundamentally shaping and potentially biasing any downstream analytical conclusions.

When we fuse multimodal geospatial information—combining remote sensing imagery, discrete POI locations, and continuous mobility trajectories—the question grows deeper still: do we need a brand-new, unified cross-modal tokenization paradigm for geographic space?

We will unpack these foundational choices that govern the ceiling of geospatial models in dedicated chapters ahead.

Author Information & References#

Author Guolong Li
Guolong Li
Received his bachelor's degree from Shandong University and is currently a Master's student (Class of 2023) in Artificial Intelligence at the Department of Urban Spatial Information Engineering, Shenzhen University.
Research Interests: GeoAI, Geospatial Data Representation Learning.
Author Ruocan Lu
Ruocan Lu
Ph.D. student (Class of 2023) at the Department of Urban Spatial Information Engineering, Shenzhen University.
Research Interests: Urban Industrial Space.

This chapter was written by Guolong Li and Ruocan Lu with the assistance of AI tools (GPT-5, Gemini 2.5 Pro). Some illustrations used in the text are sourced from the internet.

The cover image was generated by Jimeng AI with the prompt: Picture book illustration, vintage texture, front view, tactile grain; a robot holding a telescope and looking through it, extreme close-up macro shot depicting the telescope lens, specular reflection, interstellar galaxy, flowing information, connected stars, a butterfly transformed from starlight, fantastical and mysterious; the background is the vast universe filling the entire frame.

References#

  1. A. Vaswani et al., “Attention is all you need,” Advances in neural information processing systems, vol. 30, 2017.

  2. A. Dosovitskiy et al., “An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale,” arXiv:2010.11929. DOI: 10.48550/arXiv.2010.11929.

  3. N. L. Le and M.-H. Abel, “How Well Do LLMs Predict Prerequisite Skills? Zero-Shot Comparison to Expert-Defined Concepts,” arXiv:2507.18479, 2025.

  4. S. Yao et al., “Tree of thoughts: Deliberate problem solving with large language models,” Advances in neural information processing systems, vol. 36, pp. 11809-11822, 2023.

  5. P. W. Battaglia et al., “Relational inductive biases, deep learning, and graph networks,” arXiv:1806.01261, 2018.

  6. S. Geman, E. Bienenstock, and R. Doursat, “Neural networks and the bias/variance dilemma,” Neural Computation, vol. 4, no. 1, pp. 1-58, 1992.

  7. J. Jumper et al., “Highly accurate protein structure prediction with AlphaFold,” Nature, vol. 596, no. 7873, pp. 583-589, 2021.

  8. P. Schwaller et al., “Molecular transformer: a model for uncertainty-calibrated chemical reaction prediction,” ACS Central Science, vol. 5, no. 9, pp. 1572-1583, 2019.

  9. Y. Zhan, Z. Xiong, and Y. Yuan, “RSVG: Exploring data and models for visual grounding on remote sensing data,” IEEE Transactions on Geoscience and Remote Sensing, vol. 61, pp. 1-13, 2023.

  10. S. Jiang, H. Lin, H. Ren, Z. Hu, L. Weng, and M. Xia, “MDANet: A high-resolution city change detection network based on difference and attention mechanisms under multi-scale feature fusion,” Remote Sensing, vol. 16, no. 8, p. 1387, 2024.

  11. K. Bi, L. Xie, H. Zhang, X. Chen, X. Gu, and Q. Tian, “Pangu-Weather: A 3D high-resolution model for fast and accurate global weather forecast,” arXiv:2211.02556, 2022.

  12. J. Leskovec, CS224W: Machine Learning with Graphs. Stanford University.

Proofreader: Fei Huang (黄菲) Authors: Guolong Li (李国隆), Ruocan Lu (卢若灿)