Close Menu
    Trending
    • Credit Risk Scoring for BNPL Customers at Bati Bank | by Sumeya sirmula | Jul, 2025
    • The New Career Crisis: AI Is Breaking the Entry-Level Path for Gen Z
    • Musk’s X appoints ‘king of virality’ in bid to boost growth
    • Why Entrepreneurs Should Stop Obsessing Over Growth
    • Implementing IBCS rules in Power BI
    • What comes next for AI copyright lawsuits?
    • Why PDF Extraction Still Feels LikeHack
    • GenAI Will Fuel People’s Jobs, Not Replace Them. Here’s Why
    AIBS News
    • Home
    • Artificial Intelligence
    • Machine Learning
    • AI Technology
    • Data Science
    • More
      • Technology
      • Business
    AIBS News
    Home»Artificial Intelligence»Hands-On Attention Mechanism for Time Series Classification, with Python
    Artificial Intelligence

    Hands-On Attention Mechanism for Time Series Classification, with Python

    Team_AIBS NewsBy Team_AIBS NewsMay 30, 2025No Comments10 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    is a recreation changer in Machine Studying. In truth, within the latest historical past of Deep Learning, the concept of permitting fashions to concentrate on essentially the most related elements of an enter sequence when making a prediction fully revolutionized the best way we have a look at Neural Networks.

    That being mentioned, there’s one controversial take that I’ve concerning the consideration mechanism:

    One of the best ways to be taught the eye mechanism is not by Pure Language Processing (NLP)

    It’s (technically) a controversial take for 2 causes.

    1. Folks naturally use NLP circumstances (e.g., translation or NSP) as a result of NLP is the explanation why the eye mechanism was developed within the first place. The unique objective was to overcome the constraints of RNNs and CNNs in dealing with long-range dependencies in language (in case you haven’t already, it is best to actually learn the paper Attention is All You Need).
    2. Second, I can even should say that so as to perceive the overall thought of placing the “consideration” on a selected phrase to do translation duties may be very intuitive.

    That being mentioned, if we need to perceive how consideration REALLY works in a hands-on instance, I imagine that Time Series is the perfect framework to make use of. There are a lot of the explanation why I say that.

    1. Computer systems aren’t actually “made” to work with strings; they work with ones and zeros. All of the embedding steps which can be essential to convert the textual content into vectors add an additional layer of complexity that’s not strictly associated to the eye thought.
    2. The eye mechanism, although it was first developed for textual content, has many different functions (for instance, in pc imaginative and prescient), so I like the concept of exploring consideration from one other angle as effectively.
    3. With time sequence particularly, we are able to create very small datasets and run our consideration fashions in minutes (sure, together with the coaching) with none fancy GPUs.

    On this weblog put up, we’ll see how we are able to construct an consideration mechanism for time sequence, particularly in a classification setup. We are going to work with sine waves, and we’ll attempt to classify a standard sine wave with a “modified” sine wave. The “modified” sine wave is created by flattening a portion of the unique sign. That’s, at a sure location within the wave, we merely take away the oscillation and exchange it with a flat line, as if the sign had briefly stopped or change into corrupted.

    To make issues extra spicy, we’ll assume that the sine can have no matter frequency or amplitude, and that the location and extension (we name it size) of the “rectified” half are additionally parameters. In different phrases, the sine will be no matter sine, and we are able to put our “straight line” wherever we like on the sine wave.

    Effectively, okay, however why ought to we even trouble with the eye mechanism? Why are we not utilizing one thing easier, like Feed Ahead Neural Networks (FFNs) or Convolutional Neural Networks (CNNs)?

    Effectively, as a result of once more we’re assuming that the “modified” sign will be “flattened” all over the place (in no matter location of the timeseries), and it may be flattened for no matter size (the rectified half can have no matter size). Which means that a regular Neural Community will not be that environment friendly, as a result of the anomalous “half” of the timeseries will not be all the time in the identical portion of the sign. In different phrases, if you’re simply attempting to take care of this with a linear weight matrix + a non linear operate, you’ll have suboptimal outcomes, as a result of index 300 of time sequence 1 will be fully completely different from index 300 of time sequence 14. What we’d like as an alternative is a dynamic strategy that places the eye on the anomalous a part of the sequence. For this reason (and the place) the eye technique shines.

    This weblog put up can be divided into these 4 steps:

    1. Code Setup. Earlier than entering into the code, I’ll show the setup, with all of the libraries we’ll want.
    2. Information Era. I’ll present the code that we are going to want for the info era half.
    3. Mannequin Implementation. I’ll present the implementation of the eye mannequin
    4. Exploration of the outcomes. The advantage of the eye mannequin can be displayed by the eye scores and classification metrics to evaluate the efficiency of our strategy.

    It looks as if now we have loads of floor to cowl. Let’s get began! 🚀


    1. Code Setup

    Earlier than delving into the code, let’s invoke some buddies that we are going to want for the remainder of the implementation.

    These are simply default values that can be utilized all through the challenge. What you see under is the quick and candy necessities.txt file.

    I prefer it when issues are simple to vary and modular. For that reason, I created a .json file the place we are able to change every little thing concerning the setup. A few of these parameters are:

    1. The variety of regular vs irregular time sequence (the ratio between the 2)
    2. The variety of time sequence steps (how lengthy your timeseries is)
    3. The scale of the generated dataset
    4. The min and max places and lengths of the linearized half
    5. Rather more.

    The .json file appears like this.

    So, earlier than going to the following step, ensure you have:

    1. The constants.py file is in your work folder
    2. The .json file in your work folder or in a path that you just keep in mind
    3. The libraries within the necessities.txt file had been put in

    2. Information Era

    Two easy capabilities construct the traditional sine wave and the modified (rectified) one. The code for that is present in data_utils.py:

    Now that now we have the fundamentals, we are able to do all of the backend work in information.py. That is meant to be the operate that does all of it:

    1. Receives the setup data from the .json file (that’s why you want it!)
    2. Builds the modified and regular sine waves
    3. Does the practice/check cut up and practice/val/check cut up for the mannequin validation

    The info.py script is the next:

    The extra information script is the one which prepares the info for Torch (SineWaveTorchDataset), and it appears like this:

    If you wish to have a look, it is a random anomalous time sequence:

    Picture generated by creator

    And it is a non-anomalous time sequence:

    Picture generated by creator

    Now that now we have our dataset, we are able to fear concerning the mannequin implementation.


    3. Mannequin Implementation

    The implementation of the mannequin, the coaching, and the loader will be discovered within the mannequin.py code:

    Now, let me take a while to elucidate why the eye mechanism is a game-changer right here. Not like FFNN or CNN, which might deal with all time steps equally, consideration dynamically highlights the elements of the sequence that matter most for classification. This enables the mannequin to “zoom in” on the anomalous part (no matter the place it seems), making it particularly highly effective for irregular or unpredictable time sequence patterns.

    Let me be extra exact right here and discuss concerning the Neural Community.
    In our mannequin, we use a bidirectional LSTM to course of the time sequence, capturing each previous and future context at every time step. Then, as an alternative of feeding the LSTM output immediately right into a classifier, we compute consideration scores over your complete sequence. These scores decide how a lot weight every time step ought to have when forming the ultimate context vector used for classification. This implies the mannequin learns to focus solely on the significant elements of the sign (i.e., the flat anomaly), irrespective of the place they happen.

    Now let’s join the mannequin and the info to see the efficiency of our strategy.


    4. A sensible instance

    4.1 Coaching the Mannequin

    Given the large backend half that we develop, we are able to practice the mannequin with this tremendous easy block of code.

    This took round 5 minutes on the CPU to finish.
    Discover that we applied (on the backend) an early stopping and a practice/val/check to keep away from overfitting. We’re accountable youngsters.

    4.2 Consideration Mechanism

    Let’s use the next operate right here to show the eye mechanism along with the sine operate.

    Let’s present the eye scores for a traditional time sequence.

    Picture generated by creator utilizing the code above

    As we are able to see, the eye scores are localized (with a form of time shift) on the areas the place there’s a flat half, which might be close to the peaks. Nonetheless, once more, these are solely localized spikes.

    Now let’s have a look at an anomalous time sequence.

    Picture generated by creator utilizing the code above

    As we are able to see right here, the mannequin acknowledges (with the identical time shift) the world the place the operate flattens out. Nonetheless, this time, it isn’t a localized peak. It’s a complete part of the sign the place now we have greater than typical scores. Bingo.

    4.3 Classification Efficiency

    Okay, that is good and all, however does this work? Let’s implement the operate to generate the classification report.

    The outcomes are the next:

    Accuracy : 0.9775
    Precision :
    0.9855
    Recall :
    0.9685
    F1 Rating :
    0.9769
    ROC AUC Rating
    : 0.9774

    Confusion Matrix:
    [[1002 14]
    [ 31 953]]

    Very excessive efficiency when it comes to all of the metrics. Works like a appeal. 🙃


    5. Conclusions

    Thanks very a lot for studying by this text ❤️. It means quite a bit. Let’s summarize what we discovered on this journey and why this was useful. On this weblog put up, we utilized the eye mechanism in a classification job for time sequence. The classification was between regular time sequence and “modified” ones. By “modified” we imply {that a} half (a random half, with random size) has been rectified (substituted with a straight line). We discovered that:

    1. Consideration mechanisms have been initially developed in NLP, however additionally they excel at figuring out anomalies in time sequence information, particularly when the placement of the anomaly varies throughout samples. This flexibility is tough to realize with conventional CNNs or FFNNs.
    2. By utilizing a bidirectional LSTM mixed with an consideration layer, our mannequin learns what elements of the sign matter most. We noticed {that a} posteriori by the eye scores (alpha), which reveal which era steps had been most related for classification. This framework offers a clear and interpretable strategy: we are able to visualize the eye weights to know why the mannequin made a sure prediction.
    3. With minimal information and no GPU, we educated a extremely correct mannequin (F1 rating ≈ 0.98) in only a few minutes, proving that focus is accessible and highly effective even for small initiatives.

    6. About me!

    Thanks once more in your time. It means quite a bit ❤️

    My title is Piero Paialunga, and I’m this man right here:

    I’m a Ph.D. candidate on the College of Cincinnati Aerospace Engineering Division. I discuss AI and Machine Studying in my weblog posts and on LinkedIn, and right here on TDS. If you happen to preferred the article and need to know extra about machine studying and comply with my research, you may:

    A. Comply with me on Linkedin, the place I publish all my tales
    B. Comply with me on GitHub, the place you may see all my code
    C. For questions, you may ship me an e-mail at [email protected]

    Ciao!



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleApplying Physical Constraints in Physics-Informed Neural Networks (PINNs): Methods and Examples | by Noor Fatima | May, 2025
    Next Article Top Colleges Now Value What Founders Have Always Hired For
    Team_AIBS News
    • Website

    Related Posts

    Artificial Intelligence

    Implementing IBCS rules in Power BI

    July 1, 2025
    Artificial Intelligence

    Become a Better Data Scientist with These Prompt Engineering Tips and Tricks

    July 1, 2025
    Artificial Intelligence

    Lessons Learned After 6.5 Years Of Machine Learning

    July 1, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    Credit Risk Scoring for BNPL Customers at Bati Bank | by Sumeya sirmula | Jul, 2025

    July 1, 2025

    I Tried Buying a Car Through Amazon: Here Are the Pros, Cons

    December 10, 2024

    Amazon and eBay to pay ‘fair share’ for e-waste recycling

    December 10, 2024

    Artificial Intelligence Concerns & Predictions For 2025

    December 10, 2024

    Barbara Corcoran: Entrepreneurs Must ‘Embrace Change’

    December 10, 2024
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    Most Popular

    ‘I always have a vague, abstract idea of things that I want to do’: Peter Berg on how staying busy fuels his creativity

    February 18, 2025

    OpenAI Unveils GPT-4.5 for More ‘Natural Conversation’

    February 27, 2025

    How JavaScript is Powering the Future of AI and Machine Learning | by Rahul Kaklotar | Mar, 2025

    March 12, 2025
    Our Picks

    Credit Risk Scoring for BNPL Customers at Bati Bank | by Sumeya sirmula | Jul, 2025

    July 1, 2025

    The New Career Crisis: AI Is Breaking the Entry-Level Path for Gen Z

    July 1, 2025

    Musk’s X appoints ‘king of virality’ in bid to boost growth

    July 1, 2025
    Categories
    • AI Technology
    • Artificial Intelligence
    • Business
    • Data Science
    • Machine Learning
    • Technology
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2024 Aibsnews.comAll Rights Reserved.

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