LLM Algorithms · 3 of 10

Positional Encoding tutorial

Adds a position-dependent signal so identical tokens at different places remain distinguishable. Learn the concept, implement it from scratch, and apply it with PyTorch.

Category
Sequence representation
Core idea
PE(pos,2i) = sin(pos / 10000²ⁱ⁄ᵈ); PE(pos,2i+1) = cos(pos / 10000²ⁱ⁄ᵈ)
Typical use
Preserving word order inside transformer models.
Practical tool
PyTorch

What is Positional Encoding?

Positional Encoding is a sequence representation technique. Adds a position-dependent signal so identical tokens at different places remain distinguishable. Its central idea is summarized by PE(pos,2i) = sin(pos / 10000²ⁱ⁄ᵈ); PE(pos,2i+1) = cos(pos / 10000²ⁱ⁄ᵈ).

This guide connects theory to practice. You will trace the input, intermediate process, and output; decode the notation; run a dependency-light implementation; then repeat the workflow with PyTorch and evaluate the result.

By the end of this tutorial, you will be able to

  • Explain when Positional Encoding is appropriate and what assumptions it makes.
  • Read its mathematical notation or complexity statement without guessing what the symbols mean.
  • Follow and modify a from-scratch Python implementation.
  • Build a practical workflow with PyTorch and choose useful evaluation checks.

Prerequisites and tools

You do not need an advanced software stack. Start with a recent Python environment and the fundamentals below, then install only the packages used by the practical example.

  • Comfort with Python, tensor shapes, matrix multiplication, and probability distributions.
  • A working understanding of token sequences, embeddings, batches, and attention masks.
  • Enough memory to run the small tensor example; pretrained-model tutorials may also download model weights.
LanguagePython 3
Primary libraryPyTorch
Learning modeFrom scratch + library

How Positional Encoding works

  1. 1

    Input

    Token vectors and their positions in the sequence.

  2. 2

    Prepare and configure

    Check shapes, value ranges, ordering assumptions, missing values, and the parameters that control the algorithm’s behavior.

  3. 3

    Algorithm process

    Generate sinusoidal coordinates or rotate vector pairs by position.

  4. 4

    Output

    Representations that carry both token meaning and order.

  5. 5

    Validate

    Verify output shape, finite values, and distinct encodings across positions.

Formula and intuition

PE(pos,2i) = sin(pos / 10000²ⁱ⁄ᵈ); PE(pos,2i+1) = cos(pos / 10000²ⁱ⁄ᵈ)

pos is the token position, i indexes a sine–cosine pair, and d is the model dimension.

The notation captures the main operation or complexity statement behind Positional Encoding. Read it together with the symbol key above and the step-by-step process in this guide.

When to use Positional Encoding

Preserving word order inside transformer models.

Learning tip

Change one parameter at a time in the interactive lesson, replay the animation, and connect the visible change to the input, process, and output described above.

Positional Encoding from scratch in Python

This dependency-light example emphasizes the algorithm’s mechanics so each important step remains visible.

PythonEducational implementation
import numpy as np

length, dimensions = 8, 8
position = np.arange(length)[:, None]
frequency = 1 / (10000 ** (np.arange(0, dimensions, 2) / dimensions))
encoding = np.zeros((length, dimensions))
encoding[:, 0::2] = np.sin(position * frequency)
encoding[:, 1::2] = np.cos(position * frequency)

position_aware_tokens = token_embeddings + encoding
How to use this example

Run it once unchanged, inspect the output, and then alter one input or parameter. The compact implementation is designed for learning; use the tested library workflow below for real projects.

Build Positional Encoding with PyTorch

PyTorch tensor operations make the sine and cosine frequency construction efficient and reusable.

Recommended environment

JupyterLab, Google Colab, or VS Code

Create an isolated virtual environment for a local project, or paste the cells into a hosted notebook. Pin package versions before deploying a reproducible application.

Install the required package python -m pip install torch
  1. Prepare the data.Token vectors and their positions in the sequence. Validate its shape, type, range, and ordering before training or execution.
  2. Configure the algorithm.Begin with explicit, conservative parameters and a fixed random seed whenever the library supports one.
  3. Fit or execute.Generate sinusoidal coordinates or rotate vector pairs by position.
  4. Inspect the result.Representations that carry both token meaning and order. Then apply the evaluation checks in the next section.
PythonPyTorch workflow
import math
import torch

def sinusoidal_encoding(length, dimensions):
    if dimensions % 2:
        raise ValueError("dimensions must be even")
    position = torch.arange(length).unsqueeze(1)
    frequency = torch.exp(
        torch.arange(0, dimensions, 2) * (-math.log(10000.0) / dimensions)
    )
    encoding = torch.zeros(length, dimensions)
    encoding[:, 0::2] = torch.sin(position * frequency)
    encoding[:, 1::2] = torch.cos(position * frequency)
    return encoding

token_vectors = torch.randn(2, 12, 64)
position = sinusoidal_encoding(12, 64)
positioned = token_vectors + position.unsqueeze(0)
print(positioned.shape)

API details and version-specific options: official PyTorch reference →

How to evaluate the result

A successful run is not enough. Evaluate the output against the intended use, compare it with a simple baseline, and preserve a genuinely unseen test case whenever the task involves learned parameters.

  • Verify output shape, finite values, and distinct encodings across positions.
  • Test sequence lengths beyond those used during training when extrapolation matters.
Reproducibility check

Record the data version, package versions, parameters, random seeds, and evaluation procedure. Re-run the same workflow before publishing a benchmark or deploying a model.

Pitfalls and how to avoid them

These failure modes are common in tutorials and production systems. Treat them as review questions, not just after-the-fact debugging advice.

  • The positional tensor must match the embedding dimension, device, and dtype.
  • Odd dimensions need an explicit convention or validation error.
  • Padding tokens still require an attention mask after position information is added.

Before using Positional Encoding in a project

  • Write down every tensor shape and mask convention before composing layers.
  • Test a tiny deterministic case against a simple reference implementation.
  • Separate training behavior from autoregressive inference and disable dropout for evaluation.
  • Measure quality, latency, peak memory, and sequence-length scaling together.
  • Pin model, tokenizer, framework, and generation-configuration versions.

Official documentation and next steps

Primary software reference PyTorch

Use the official documentation to confirm supported parameters, current defaults, input requirements, and version changes.

Read official documentation →

This guide is an educational introduction, not a substitute for domain validation. For consequential applications, review the source documentation, test against representative data, and involve a subject-matter expert.

See Positional Encoding in motion

Open the interactive lesson to adjust parameters, scrub through the process, replay the animation, and compare the explanation with the Python code.

Launch visualization →