Positional encoding is a technique that injects information about the absolute or relative position of tokens into the input embeddings of a sequence model, such as a Transformer. Because the self-attention mechanism is permutation-invariant and processes all elements in parallel, it has no inherent sense of order. Positional encoding resolves this by adding a unique signal to each token's representation, allowing the model to distinguish 'transaction A before transaction B' from the reverse, thereby preserving the critical temporal structure of a financial event stream.
Glossary
Positional Encoding

What is Positional Encoding?
A mechanism for injecting information about the position of tokens in a sequence into non-recurrent architectures, preserving the temporal order of transactions.
The most common implementation uses sinusoidal functions of varying frequencies, generating a deterministic vector for each position that the model can learn to interpret. This allows the architecture to capture relative distances between any two transactions in a sequence. In fraud detection, this is essential for modeling the sequential nature of user behavior, enabling the model to recognize that a login followed by a password change and a high-value wire transfer is a fundamentally different pattern than the same events occurring in a different order.
Key Features of Positional Encoding
Positional encoding is the mechanism that injects sequence order information into non-recurrent architectures. Without it, a Transformer perceives a sequence of transactions as a bag of events, losing the critical temporal structure needed to distinguish a legitimate spending pattern from a fraudulent one.
Sinusoidal Fixed Encoding
The original method from Attention Is All You Need uses sine and cosine functions of varying frequencies to encode position. Each dimension of the positional vector corresponds to a sinusoid.
- Formula:
PE(pos, 2i) = sin(pos / 10000^(2i/d_model)) - Key Property: Allows the model to easily learn to attend to relative positions, as
PE(pos+k)can be represented as a linear function ofPE(pos). - Benefit: No additional parameters to learn and can theoretically extrapolate to sequence lengths longer than those seen during training.
Learned Positional Embedding
An alternative approach where position representations are treated as learnable parameters, initialized randomly and updated via backpropagation. Common in BERT and GPT architectures.
- Mechanism: A standard embedding layer maps integer indices
[0, 1, ..., N]to dense vectors. - Constraint: The maximum sequence length is fixed at training time; the model cannot natively handle longer sequences without interpolation.
- Use Case: Often preferred when the training data distribution of sequence lengths is well-defined and bounded, such as processing a fixed window of 100 prior transactions.
Relative Positional Encoding
Instead of encoding absolute position, this method modifies the self-attention mechanism to depend on the pairwise distance between tokens. This directly models the intuition that the relationship between transaction t and t-1 is more important than the absolute index of t.
- Implementation: Often achieved by adding a learned bias to the attention score based on the offset between query and key positions.
- Advantage: Naturally generalizes to sequence lengths unseen during training, making it robust for variable-length transaction histories.
- Example: Transformer-XL and T5 relative position biases.
Rotary Position Embedding (RoPE)
A widely adopted method that encodes position by rotating the query and key vectors in the attention mechanism. The dot-product attention score then depends only on the relative distance between tokens.
- Mechanism: Multiplies vectors by a rotation matrix where the rotation angle is a function of the position index.
- Key Benefit: Seamlessly integrates absolute position information while naturally decaying the attention weight with increasing relative distance, providing a strong inductive bias for temporal locality.
- Relevance: Used in Llama, Mistral, and other state-of-the-art models for processing long sequences.
AliBi (Attention with Linear Biases)
A simple yet effective method that adds a static, non-learned bias to the attention scores. The bias is a linear penalty proportional to the distance between tokens.
- Formula: A constant slope
mis subtracted from the attention score for each step of separation. - Advantage: Extremely computationally efficient with no learned parameters and has demonstrated strong extrapolation to sequences 2-3x longer than training length.
- Application: Ideal for high-throughput fraud detection pipelines where inference speed on long transaction histories is critical.
Functional Time Encoding
An approach that maps continuous time stamps directly to vectors, moving beyond discrete sequence positions to model the actual inter-arrival times between transactions.
- Method: Uses functions like
cos(ω * t)or learnable neural networks to encode the real-valued timetof a transaction. - Significance: Captures the temporal density of events—a burst of 10 transactions in 1 minute is fundamentally different from 10 transactions over 10 hours, a distinction lost by discrete positional indexing.
- Integration: Often concatenated with or added to the standard positional encoding to provide both sequential order and temporal rhythm information.
Absolute vs. Relative Positional Encoding
Comparison of the two primary approaches for injecting positional information into non-recurrent sequence models like Transformers, critical for preserving temporal order in transaction streams.
| Feature | Absolute Positional Encoding | Relative Positional Encoding | Rotary Position Embedding (RoPE) |
|---|---|---|---|
Core Mechanism | Adds a fixed or learned vector to token embeddings based on absolute index position (e.g., sine/cosine functions). | Modifies the attention computation to encode the relative distance between any two tokens, independent of absolute position. | Encodes relative position by rotating query and key vectors in embedding space, blending absolute and relative approaches. |
Position Representation | Absolute index (0, 1, 2, ... n). | Relative offset (k tokens apart). | Relative offset via multiplicative rotation matrix. |
Extrapolation to Longer Sequences | |||
Translation Invariance | |||
Computational Overhead | Negligible (element-wise addition). | Moderate (recomputes pairwise biases). | Low (applied directly to Q/K vectors). |
Typical Use Case | Original Transformer, fixed-length sequences where absolute order is paramount. | Tasks requiring generalization to unseen sequence lengths, such as long document processing. | Modern large language models requiring efficient long-context scaling and stability. |
Integration Point | Input embedding layer (pre-attention). | Attention score computation (within attention). | Query/Key projection (within attention). |
Memory Footprint | O(n) for storing position vectors. | O(n^2) for pairwise bias matrix. | O(1) additional memory. |
Frequently Asked Questions
Clear, technically precise answers to the most common questions about how Transformers and other non-recurrent models preserve the sequential order of financial transactions.
Positional encoding is a technique for injecting information about the absolute or relative position of tokens into a non-recurrent sequence model. Unlike recurrent neural networks (RNNs) such as LSTMs or GRUs, which process data sequentially and inherently capture order, the Transformer architecture processes all tokens in parallel via the self-attention mechanism. This parallelism makes the model permutation-invariant—it sees a sequence as a set, not an ordered list. For financial fraud detection, where the temporal order of transactions (e.g., a small test transaction followed by a large transfer) is a critical signal, this is catastrophic. Positional encoding solves this by adding a unique positional signal to each token's embedding before it enters the attention layers, allowing the model to distinguish 'Transaction A then Transaction B' from 'Transaction B then Transaction A' and learn temporal patterns indicative of fraud.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Explore the foundational sequence modeling architectures and mechanisms that rely on or contrast with positional encoding to capture temporal dynamics in transaction data.
Transformer Architecture
The foundational architecture that necessitates positional encoding. Unlike RNNs, Transformers process all tokens in parallel via self-attention, making them inherently permutation-invariant. Positional encoding is the mechanism that injects sequence order, allowing the model to distinguish 'Transaction A before B' from 'B before A'. This enables the capture of long-range dependencies without recurrent bottlenecks.
Long Short-Term Memory (LSTM)
A recurrent architecture that processes sequences step-by-step, inherently encoding position through its recurrent state. Unlike Transformers, LSTMs do not require explicit positional encoding because their computational graph is unrolled over time. However, this sequential nature prevents parallelization and can struggle with very long-range dependencies due to vanishing gradients, motivating the shift to attention-based models for large transaction logs.
Temporal Convolutional Network (TCN)
Uses causal, dilated convolutions to model sequences. The causal constraint ensures no information leakage from the future, while dilation expands the receptive field. Positional information is implicit in the convolutional structure, as filters slide across the time dimension. TCNs offer a parallelizable alternative to RNNs and a simpler positional mechanism than Transformers, often serving as a robust baseline for sequence modeling.
Sequence Embedding
The process of mapping a variable-length sequence of discrete events (e.g., merchant codes) into a fixed-length vector. Effective sequence embeddings must preserve temporal order, often achieved by feeding sequences into a position-aware model like a Transformer or LSTM. The resulting vector captures the semantic and temporal essence of a user's behavior, enabling comparison, clustering, and anomaly detection on entire transaction histories.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us