Whereas digging into basis fashions for time sequence, I spotted that I may not likely perceive them with out first understanding transformers. I didn’t wish to use these fashions as black packing containers, so I began tracing the concepts backward, from basis fashions to transformers, and from transformers to self-attention. What made the transition fascinating is that though transformers have been initially constructed for language, the core thought carries naturally to time sequence. The 2 modalities are very completely different, however they share one thing basic: each are sequences, and in each circumstances, order modifications which means.
In language, canine bites man could be very completely different from man bites canine.
Time sequence aren’t any completely different. A temperature of yesterday and right this moment tells a distinct story from yesterday and right this moment. The values stands out as the identical, however their order modifications the which means of the sequence.
The query is that if self-attention seems to be in any respect observations directly, how does a transformer know which remark got here first, which got here later, or how far aside two observations are?
That query led me to positional encoding.
What stunned me most was how such a easy mathematical thought may give a Transformer a way of order. The precise strategies have developed significantly since then, however the underlying drawback stays the identical.
This text is my try to construct that instinct from the bottom up, beginning with a easy time sequence and following the trail from uncooked observations to self-attention and eventually to positional encoding.
From scalar observations to vector representations
Take into account a easy time sequence containing the temperature recorded over 5 weekdays:
Every remark is barely a scalar. A transformer, nevertheless, operates on vectors of dimensionality . The scalar observations subsequently must be mapped into that illustration house first.
A easy method to do that is thru a discovered linear projection i.e. embedding:
giving us a sequence of vector representations:
An embedding is a deep, summary illustration of the sequence within the type of a multidimensional numerical vector that encodes its options and that the mannequin understands. [1]
Every captures details about the noticed worth at that timestep, however at this level, it’s only a illustration of the remark.
The necessary phrase right here is discovered. The mannequin will not be given a predefined vector illustration for a temperature equivalent to . The parameters and are discovered throughout coaching in order that the ensuing representations grow to be helpful for the duty.
At this level, represents what was noticed. It doesn’t but inform the mannequin the place that remark occurred within the sequence.
How self-attention builds context?
Self-attention permits every remark to make use of info from the remainder of the sequence.
Suppose we wish to replace Friday’s illustration. The mannequin first creates three discovered projections from each :
The matrices , and are additionally discovered throughout coaching. The mannequin will not be advised beforehand what a helpful question, key, or worth ought to appear to be.
For Friday, its question is in contrast with the keys of all observations: .
Every comparability produces an consideration rating:
which measures how related remark ‘j’ is when updating Friday’s illustration. The scaling issue prevents the dot merchandise from rising too massive because the dimensionality of the question and key vectors will increase.
These scores are handed by a softmax perform to transform them into consideration weights:
Lastly, these weights are used to mix the worth vectors:
So is Friday’s illustration earlier than incorporating info from the remainder of the sequence, whereas is its context-aware illustration after self-attention.
Briefly:
Queries and keys study which observations are related to at least one one other. Values carry the knowledge that’s mixed to type the brand new illustration.
What occurs if we shuffle the sequence?
Now comes the necessary query.
Suppose the identical 5 temperature observations are rearranged.
The values themselves haven’t modified; solely the order has. After the discovered projection, we nonetheless have the identical set of worth representations, simply rearranged.
Self-attention can nonetheless examine every illustration with all of the others. The identical question, key, and worth projections are utilized, and the identical sorts of pairwise relationships can nonetheless be computed.
What has disappeared is the temporal construction.
Nothing inside says that it initially got here from Thursday. Nothing inside says that it occurred after . Additionally, if Wednesday and Friday have the identical temperature worth, the discovered projection will map them to the identical embedding vector. With out positional info, the mannequin subsequently has no option to distinguish which embedding got here from Wednesday and which got here from Friday.
That is the important thing limitation:
Self-attention can study which observations are associated, however with out an extra positional sign, it has no built-in option to know the place these observations occurred within the sequence.
What ought to positional info inform the mannequin?
If self-attention doesn’t know the order of the observations, then the following query is: what sort of positional info could be helpful?
At a minimal, we’d need the mannequin to know:
-
Which place an remark belongs to?
Place 2 needs to be distinguishable from place 20. -
Which remark got here earlier than or after one other?
The mannequin ought to have the ability to distinguish from . -
How far aside are two observations?
In time sequence, the distinction between , and may be necessary. -
That close by positions are associated in a structured method.
Place 10 and place 11 mustn’t appear to be two fully unrelated identifiers. -
That the illustration stays helpful over longer sequences.
Ideally, the positional scheme ought to nonetheless present significant construction because the sequence grows.
For time sequence, the third property is very helpful. A mannequin might care about an remark one step in the past due to short-term dependence, or seven steps in the past due to a weekly seasonal sample.
So positional info ought to do greater than merely assign a novel label to every timestep. It ought to give the mannequin a structured illustration of order and relative distance.
How can we signify place?
We now know what info is lacking. The following query is learn how to signify it.
A easy option to signify place could be to assign every timestep a quantity:
However feeding the uncooked place immediately into the mannequin will not be perfect. The values continue to grow with sequence size, and a single quantity doesn’t give the mannequin a wealthy illustration of positional relationships.
One of many authentic Transformer’s options was sinusoidal positional encoding, the place every place is represented utilizing sine and cosine capabilities at completely different frequencies.
Why sine and cosine?
Begin with the best two-dimensional instance:
As modifications, the positional vector strikes easily round a circle that permits close by positions to have completely different however nonetheless associated representations.
Extra importantly, shifting ahead by the identical variety of steps produces the identical type of change within the positional illustration. For instance, an offset of seven positions has the identical mathematical relationship whether or not we transfer from place 3 to 10 or from place 20 to 27. That’s helpful for time sequence as a result of relative distance typically issues:
can signify very completely different temporal relationships.
The total sinusoidal positional encoding extends this concept throughout many dimensions:
Totally different dimensions use completely different frequencies. Some change shortly throughout close by positions, whereas others change far more slowly.
One helpful method to consider that is as many clocks operating at completely different speeds. Collectively, their readings give each place a structured positional signature.
So as a substitute of giving timestep (t) solely a quantity, we give it a vector:
that accommodates details about its place and its relationship to different positions.
Combining worth and place
After including positional info, every timestep is represented as:
the place represents the noticed worth and represents its place within the sequence.
Self-attention now builds its queries and keys from this mixed illustration:
So when the mannequin computes an consideration rating,
the comparability is now not based mostly solely on the noticed values. The question and key vectors have been created from representations that already comprise positional info.
In consequence, the mannequin can study relationships that rely on each:
-
what was noticed, and
-
the place the observations occurred within the sequence.
For a time sequence, this implies the mannequin can probably study that an remark one timestep in the past needs to be handled in another way from one seven timesteps in the past, even when their values are comparable.
Positional encoding subsequently doesn’t inform the mannequin explicitly which lags are necessary. It offers self-attention the knowledge wanted to study which positional relationships matter for the duty.
Abstract
We began with a easy drawback: self-attention can examine each remark with each different remark, however by itself it doesn’t know the order wherein these observations occurred.
The answer is to counterpoint every worth illustration with a positional illustration :
Self-attention then builds its queries, keys, and values from fairly than from the worth illustration alone. This implies the eye mechanism can study relationships based mostly not solely on what was noticed, but in addition on the place that remark occurred within the sequence.
For usually sampled time sequence, this makes relationships equivalent to t−1, t−7, or t−24 accessible to the mannequin. A lag of 1 step might seize short-term dependence, whereas a lag of seven or twenty-four steps might correspond to a seasonal sample.
Positional encoding, nevertheless, represents sequence place, not essentially real-world time. If observations are irregularly spaced, being one place aside doesn’t at all times imply being one hour or at some point aside. That is the place richer temporal encodings and time options grow to be necessary.
Sinusoidal positional encoding is just one option to inject order right into a Transformer. Different approaches embrace discovered positional embeddings, the place the place vectors themselves are discovered throughout coaching, and relative positional encodings, which focus extra immediately on the space between two observations fairly than their absolute positions.
For time sequence, the issue can grow to be even richer. Sequence place is probably not sufficient: the mannequin may must know the precise timestamp, calendar results, periodicity, or irregular gaps between observations.
So the broader query will not be merely:
How will we inform a Transformer that that is place 7?
however fairly:
What notion of time does the mannequin really need for the duty?
···
And this brings me again to the place this exploration began: time-series basis fashions. Trendy architectures might use extra subtle approaches equivalent to rotary or discovered positional representations fairly than the unique sinusoidal formulation. However understanding the easy sinusoidal building offers us the muse for understanding why these strategies exist within the first place.
We now have now constructed the muse of why positional info is required, how it’s represented, and the way self-attention makes use of it. With this instinct in place, extra superior positional encoding strategies ought to really feel a lot simpler to know once we encounter them in future.
···
Observe: The figures on this article have been conceptually designed by the writer and generated and refined with the help of an AI image-generation device.
References
[1] Peixeiro, Marco. Time Sequence Forecasting Utilizing Basis Fashions: Learn how to Construct Excessive Accuracy Predictive Fashions. Manning, 2025.
[2] Davidson, Graeme, and Lei Ma. Time Sequence with PyTorch: Trendy Deep Studying Toolkit for Actual-World Forecasting Challenges. Packt Publishing, 2026.






