Close Menu
AI News TodayAI News Today

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    Explore the globe in field recordings

    iPhone Handoff will seamlessly share one number between two phones

    Hikers rescued after using Google Gemini for planning

    Facebook X (Twitter) Instagram
    • About Us
    • Contact Us
    Facebook X (Twitter) Instagram Pinterest Vimeo
    AI News TodayAI News Today
    • Home
    • AI News
    • AI Reviews
    • AI Tools
    • AI Tutorials
    • Chatbots
    • Free AI Tools
    • Artificial Intelligence
    AI News TodayAI News Today
    Home»AI Tools»Why Transformers Need Positional Encoding For Time Series: A Visual Guide
    AI Tools

    Why Transformers Need Positional Encoding For Time Series: A Visual Guide

    By No Comments10 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Why Transformers Need Positional Encoding For Time Series: A Visual Guide
    Share
    Facebook Twitter LinkedIn Pinterest Email

    While digging into foundation models for time series, I realized that I could not really understand them without first understanding transformers. I did not want to use these models as black boxes, so I started tracing the ideas backward, from foundation models to transformers, and from transformers to self-attention. What made the transition interesting is that although transformers were originally built for language, the core idea carries naturally to time series. The two modalities are very different, but they share something fundamental: both are sequences, and in both cases, order changes meaning.

    In language, dog bites man is very different from man bites dog.

    Time series are no different. A temperature of 30∘30^circ30∘ yesterday and 20∘20^circ20∘ today tells a different story from 20∘20^circ20∘yesterday and 30∘30^circ30∘ today. The values may be the same, but their order changes the meaning of the sequence.

    The question is that if self-attention looks at all observations at once, how does a transformer know which observation came first, which came later, or how far apart two observations are?

    That question led me to positional encoding.

    What surprised me most was how such a simple mathematical idea could give a Transformer a sense of order. The exact techniques have evolved considerably since then, but the underlying problem remains the same.

    This article is my attempt to build that intuition from the ground up, starting with a simple time series and following the path from raw observations to self-attention and finally to positional encoding.

    From scalar observations to vector representations

    Consider a simple time series containing the temperature recorded over five weekdays:

    Example of time series: 5-day temperature history

    Each observation xtx_txt​ is only a scalar. A transformer, however, operates on vectors of dimensionality dmodeld_{model}dmodel​. The scalar observations therefore need to be mapped into that representation space first.

    A simple way to do this is through a learned linear projection i.e. embedding:

    et=Wext+bee_t = W_e x_t + b_eet​=We​xt​+be​

    giving us a sequence of vector representations: e1,e2,e3,e4,e5e_1, e_2, e_3, e_4, e_5e1​,e2​,e3​,e4​,e5​

    An embedding is a deep, abstract representation of the series in the form of a multidimensional numerical vector that encodes its features and that the model understands. [1]

    Each time series token is represented by a learned embedding

    Each ete_tet​ captures information about the observed value at that timestep, but at this point, it is just a representation of the observation.

    The important word here is learned. The model is not given a predefined vector representation for a temperature such as 18∘18^circ18∘. The parameters WeW_eWe​ and beb_ebe​ are learned during training so that the resulting representations become useful for the task.

    At this point, ete_tet​ represents what was observed. It does not yet tell the model where that observation occurred in the sequence.

    How self-attention builds context?

    Self-attention allows each observation to use information from the rest of the sequence.

    Suppose we want to update Friday’s representation. The model first creates three learned projections from every ete_tet​:

    qt=WQet,kt=WKet,vt=WVetq_t = W_Q e_t,qquad k_t = W_K e_t,qquad v_t = W_V e_tqt​=WQ​et​,kt​=WK​et​,vt​=WV​et​
    Query, key, and value vectors are learned in the self-attention block

    The matrices WQW_QWQ​, WKW_KWK​ and WVW_VWV​ are also learned during training. The model is not told beforehand what a useful query, key, or value should look like.

    For Friday, its query q5q_5q5​ is compared with the keys of all observations: k1,k2,k3,k4,k5k_1, k_2, k_3, k_4, k_5k1​,k2​,k3​,k4​,k5​.

    Each comparison produces an attention score:

    s5,j=q5⊤kjdks_{5,j} = frac{q_5^top k_j}{sqrt{d_k}}s5,j​=dk​​q5⊤​kj​​

    which measures how relevant observation ‘j’ is when updating Friday’s representation. The scaling factor dksqrt{d_k} dk​​prevents the dot products from growing too large as the dimensionality of the query and key vectors increases.

    These scores are passed through a softmax function to convert them into attention weights:

    α5,j=exp⁡(s5,j)∑j′exp⁡(s5,j′)alpha_{5,j} = frac{exp(s_{5,j})}{sum_{j’} exp(s_{5,j’})}α5,j​=∑j′​exp(s5,j′​)exp(s5,j​)​

    Finally, those weights are used to combine the value vectors:

    z5=∑jα5,jvj.z_5 = sum_j alpha_{5,j}v_j.z5​=j∑​α5,j​vj​.

    So e5e_5e5​ is Friday’s representation before incorporating information from the rest of the sequence, while z5z_5z5​ is its context-aware representation after self-attention.

    In short:

    Queries and keys learn which observations are relevant to one another. Values carry the information that is combined to form the new representation.

    What happens if we shuffle the sequence?

    Now comes the important question.

    Suppose the same five temperature observations are rearranged.

    The values themselves have not changed; only the order has. After the learned projection, we still have the same set of value representations, just rearranged.

    Self-attention can still compare each representation with all the others. The same query, key, and value projections are applied, and the same kinds of pairwise relationships can still be computed.

    What has disappeared is the temporal structure.

    Nothing inside e(27∘)e(27^circ)e(27∘) says that it originally came from Thursday. Nothing inside e(18∘)e(18^circ)e(18∘) says that it occurred after e(27∘)e(27^circ)e(27∘). Also, if Wednesday and Friday have the same temperature value, the learned projection will map them to the same embedding vector. Without positional information, the model therefore has no way to distinguish which embedding came from Wednesday and which came from Friday.

    This is the key limitation:

    Self-attention can learn which observations are related, but without an additional positional signal, it has no built-in way to know where those observations occurred in the sequence.

    What should positional information tell the model?

    If self-attention does not know the order of the observations, then the next question is: what kind of positional information would be useful?

    At a minimum, we would want the model to know:

    • Which position an observation belongs to?
      Position 2 should be distinguishable from position 20.

    • Which observation came before or after another?
      The model should be able to distinguish t−1t-1t−1 from t+1t+1t+1.

    • How far apart are two observations?
      In time series, the difference between t−1t-1t−1, t−7,t-7,t−7, and t−30t-30t−30 can be important.

    • That nearby positions are related in a structured way.
      Position 10 and position 11 should not look like two completely unrelated identifiers.

    • That the representation remains useful over longer sequences.
      Ideally, the positional scheme should still provide meaningful structure as the sequence grows.

    For time series, the third property is especially useful. A model may care about an observation one step ago because of short-term dependence, or seven steps ago because of a weekly seasonal pattern.

    So positional information should do more than simply assign a unique label to each timestep. It should give the model a structured representation of order and relative distance.

    How can we represent position?

    We now know what information is missing. The next question is how to represent it.

    A simple way to represent position would be to assign each timestep a number:

    1,2,3,…1, 2, 3, ldots1,2,3,…

    But feeding the raw position directly into the model is not ideal. The values keep growing with sequence length, and a single number does not give the model a rich representation of positional relationships.

    One of the original Transformer’s solutions was sinusoidal positional encoding, where each position is represented using sine and cosine functions at different frequencies.

    Why sine and cosine?

    Start with the simplest two-dimensional example:

    pt=[sin⁡(t) cos⁡(t)]p_t = begin{bmatrix} sin(t) cos(t) end{bmatrix}pt​=[sin(t) cos(t)​]

    As ttt changes, the positional vector moves smoothly around a circle that allows nearby positions to have different but still related representations.

    More importantly, moving forward by the same number of steps produces the same kind of change in the positional representation. For example, an offset of 7 positions has the same mathematical relationship whether we move from position 3 to 10 or from position 20 to 27. That is useful for time series because relative distance often matters:

    t−1,t−7,t−30t-1,qquad t-7,qquad t-30t−1,t−7,t−30

    can represent very different temporal relationships.

    The full sinusoidal positional encoding extends this idea across many dimensions:

    PE(t,2i)=sin⁡(t100002i/dmodel)PE(t,2i)= sinleft( frac{t}{10000^{2i/d_{text{model}}}} right)PE(t,2i)=sin(100002i/dmodel​t​)
    PE(t,2i+1)=cos⁡(t100002i/dmodel)PE(t,2i+1)= cosleft( frac{t}{10000^{2i/d_{text{model}}}} right)PE(t,2i+1)=cos(100002i/dmodel​t​)

    Different dimensions use different frequencies. Some change quickly across nearby positions, while others change much more slowly.

    One useful way to think about this is as many clocks running at different speeds. Together, their readings give every position a structured positional signature.

    So instead of giving timestep (t) only a number, we give it a vector:

    pt∈Rdmodelp_t in mathbb{R}^{d_{text{model}}}pt​∈Rdmodel​

    that contains information about its position and its relationship to other positions.

    Combining value and position

    After adding positional information, each timestep is represented as:

    ht=et+pth_t = e_t + p_tht​=et​+pt​

    where ete_tet​ represents the observed value and ptp_tpt​ represents its position in the sequence.

    Self-attention now builds its queries and keys from this combined representation:

    Q=HWQ,K=HWKQ = HW_Q,qquad K = HW_KQ=HWQ​,K=HWK​

    So when the model computes an attention score,

    score(i,j)=qi⊤kj,text{score}(i,j)=q_i^top k_j,score(i,j)=qi⊤​kj​,

    the comparison is no longer based only on the observed values. The query and key vectors were created from representations that already contain positional information.

    As a result, the model can learn relationships that depend on both:

    • what was observed, and

    • where the observations occurred in the sequence.

    For a time series, this means the model can potentially learn that an observation one timestep ago should be treated differently from one seven timesteps ago, even if their values are similar.

    Positional encoding therefore does not tell the model explicitly which lags are important. It gives self-attention the information needed to learn which positional relationships matter for the task.

    Summary

    We started with a simple problem: self-attention can compare every observation with every other observation, but by itself it does not know the order in which those observations occurred.

    The solution is to enrich each value representation ete_tet​​ with a positional representation ptp_tpt​​:

    ht​=et​+pt​h_t​ = e_t ​+ p_t​ht​​=et​​+pt​​

    Self-attention then builds its queries, keys, and values from hth_tht​​ rather than from the value representation alone. This means the attention mechanism can learn relationships based not only on what was observed, but also on where that observation occurred in the sequence.

    For regularly sampled time series, this makes relationships such as t−1, t−7, or t−24 accessible to the model. A lag of one step may capture short-term dependence, while a lag of seven or twenty-four steps may correspond to a seasonal pattern.

    Positional encoding, however, represents sequence position, not necessarily real-world time. If observations are irregularly spaced, being one position apart does not always mean being one hour or one day apart. This is where richer temporal encodings and time features become important.

    Sinusoidal positional encoding is only one way to inject order into a Transformer. Other approaches include learned positional embeddings, where the position vectors themselves are learned during training, and relative positional encodings, which focus more directly on the distance between two observations rather than their absolute positions.

    For time series, the problem can become even richer. Sequence position may not be enough: the model may also need to know the actual timestamp, calendar effects, periodicity, or irregular gaps between observations.

    So the broader question is not simply:

    How do we tell a Transformer that this is position 7?

    but rather:

    What notion of time does the model actually need for the task?

    ···

    And this brings me back to where this exploration started: time-series foundation models. Modern architectures may use more sophisticated approaches such as rotary or learned positional representations rather than the original sinusoidal formulation. But understanding the simple sinusoidal construction gives us the foundation for understanding why those methods exist in the first place.

    We have now built the foundation of why positional information is needed, how it is represented, and how self-attention uses it. With this intuition in place, more advanced positional encoding techniques should feel much easier to understand when we encounter them in future.

    ···

    Note: The figures in this article were conceptually designed by the author and generated and refined with the assistance of an AI image-generation tool.

    References

    [1] Peixeiro, Marco. Time Series Forecasting Using Foundation Models: How to Build High Accuracy Predictive Models. Manning, 2025.

    [2] Davidson, Graeme, and Lei Ma. Time Series with PyTorch: Modern Deep Learning Toolkit for Real-World Forecasting Challenges. Packt Publishing, 2026.

    Encoding Guide Positional Series time Transformers Visual
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleOura is going public, but these smart ring companies are coming for its crown
    Next Article Content creators drop the ball
    • Website

    Related Posts

    AI Tools

    DALL·E, Demystified: How OpenAI’s Image Model Works and When to Use It

    AI Tools

    Dynamical System Transfer Learning with Reduced Order Models

    AI Tools

    Cursor Isn’t Just an Autocomplete. It’s a Codebase-Aware AI Editor.

    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    Explore the globe in field recordings

    0 Views

    iPhone Handoff will seamlessly share one number between two phones

    0 Views

    Hikers rescued after using Google Gemini for planning

    0 Views
    Stay In Touch
    • Facebook
    • YouTube
    • TikTok
    • WhatsApp
    • Twitter
    • Instagram
    Latest Reviews
    AI Tutorials

    Quantization from the ground up

    AI Tools

    David Sacks is done as AI czar — here’s what he’s doing instead

    AI Reviews

    Judge sides with Anthropic to temporarily block the Pentagon’s ban

    Subscribe to Updates

    Get the latest tech news from FooBar about tech, design and biz.

    Most Popular

    Explore the globe in field recordings

    0 Views

    iPhone Handoff will seamlessly share one number between two phones

    0 Views

    Hikers rescued after using Google Gemini for planning

    0 Views
    Our Picks

    Quantization from the ground up

    David Sacks is done as AI czar — here’s what he’s doing instead

    Judge sides with Anthropic to temporarily block the Pentagon’s ban

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    Facebook X (Twitter) Instagram Pinterest
    • About Us
    • Contact Us
    • Terms & Conditions
    • Privacy Policy
    • Disclaimer

    © 2026 ainewstoday.co. All rights reserved. Designed by DD.

    Type above and press Enter to search. Press Esc to cancel.