ML - Attention Mechanism - GeeksforGeeks
ML - Attention Mechanism - GeeksforGeeks
90% Refund @Courses Machine Learning Tutorial Data Analysis Tutorial Python – Data visualization tutorial Nu
ML – Attention mechanism
Read Courses Jobs
Let’s take a look at hearing and a case study of selective attention in the
context of a crowded cocktail party. Assume you’re at a social gathering with a
large number of people speaking at the same time. You’re also talking with a
friend, but the background noise is not recognized. You are
only paying attention to your friend’s voice and grasping their words while
filtering out background noise. In this scenario, our auditory system employs
selective attention to focus on the relevant auditory information. The
neurological system of our brain improves the representation of speech by
prioritizing relevant sounds and ignoring background noises.
Attention Mechanism
An attention mechanism is an Encoder-Decoder kind of neural network
architecture that allows the model to focus on specific sections of the input
while executing a task. It dynamically assigns weights to different elements in
the input, indicating their relative importance or relevance. By incorporating
We use cookies to ensure
attention, you have the
the model canbest browsing experience
selectively attend on to
our and
website. By using our
process the most relevant
Got It !
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/ml-attention-mechanism/ 1/18
1/10/24, 2:14 AM ML - Attention mechanism - GeeksforGeeks
Sider Open
We use cookies to ensure you have the best browsing experience on our website. By using our
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/ml-attention-mechanism/ 3/18
1/10/24, 2:14 AM ML - Attention mechanism - GeeksforGeeks
where,
hs: Encoder source hidden state at position s
yi: Encoder Target hidden state at the position i
W: Weight Matrix
v : Weight vector
5. Attention Weights Calculation: The similarity scores are passed through a
softmax function to obtain attention weights. These weights indicate the
importance or relevance of each key-value pair.
Here,
Ts: Total number of key-value pairs (source hidden states) in the encoder.
We useBycookies
incorporating an attention
to ensure you have mechanism,
the best browsing experience onthe modelBycan
our website. usingeffectively
our capture
site, you acknowledge thatemphasize
dependencies, you have read and understoodinformation,
important our Cookie Policy and
& Privacy Policy
adaptively focus on
https://www.geeksforgeeks.org/ml-attention-mechanism/ 4/18
1/10/24, 2:14 AM ML - Attention mechanism - GeeksforGeeks
Encoder
Attention
Decoder
We use cookies to ensure you have the best browsing experience on our website. By using our
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/ml-attention-mechanism/ 5/18
1/10/24, 2:14 AM ML - Attention mechanism - GeeksforGeeks
Encoder:
We use cookies to ensure you have the best browsing experience on our website. By using our
Encoder
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/ml-attention-mechanism/ 6/18
1/10/24, 2:14 AM ML - Attention mechanism - GeeksforGeeks
2. Each input goes through an Embedding Layer, It can be RNN, LSTM, GRU or
trnasformers
3. Each of the inputs generates a hidden representation.
4. This generates the outputs for the Encoder:
Attention:
Sider Open
.
This layer in turn contains 3 subparts:
Feed Forward Network
We use cookies to ensure you have the best browsing experience on our website. By using our
Softmax
site, you acknowledge thatCalculation
you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/ml-attention-mechanism/ 7/18
1/10/24, 2:14 AM ML - Attention mechanism - GeeksforGeeks
attention
We use cookies to ensure you have the best browsing experience on our website. By using our
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/ml-attention-mechanism/ 8/18
1/10/24, 2:14 AM ML - Attention mechanism - GeeksforGeeks
Feed-Forward-Network
Here,
softmax calculation
https://www.geeksforgeeks.org/ml-attention-mechanism/ 9/18
1/10/24, 2:14 AM ML - Attention mechanism - GeeksforGeeks
We find in the same way and feed it to different RNN units of the
Decoder layer. So this is the final vector which is the product of (Probability
Distribution) and (Encoder’s output) which is nothing but the attention paid to
the input words.
Decoder:
The context vector is fed into the decoder along with the current hidden state
of the decoder in order to predict the next token in the output sequence. Until
the decoder generates the entire output sequence, this process is done
recursively.
We feed these Context Vectors to the RNNs of the Decoder layer. Each decoder
produces an output which is the translation for the input words.
Conclusions
We useFrequently Asked
cookies to ensure you Questions
have the best (FAQs)
browsing experience on our website. By using our
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/ml-attention-mechanism/ 10/18
1/10/24, 2:14 AM ML - Attention mechanism - GeeksforGeeks
https://www.geeksforgeeks.org/ml-attention-mechanism/ 11/18
1/10/24, 2:14 AM ML - Attention mechanism - GeeksforGeeks
https://www.geeksforgeeks.org/ml-attention-mechanism/ 12/18
1/10/24, 2:14 AM ML - Attention mechanism - GeeksforGeeks
Whether you're preparing for your first job interview or aiming to upskill in this
ever-evolving tech landscape, GeeksforGeeks Courses are your key to success.
We provide top-quality content at affordable prices, all geared towards
accelerating your growth in a time-bound manner. Join the millions we've
already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Previous Next
Similar Reads
We use cookies to ensure you have the best browsing experience on our website. By using our
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/ml-attention-mechanism/ 13/18
1/10/24, 2:14 AM ML - Attention mechanism - GeeksforGeeks
Sliding Window Attention Self -attention in NLP
Complete Tutorials
Computer Vision Tutorial Pandas AI: The Generative AI Python
Library
K KeshavBa… Follow
Additional Information
We use cookies to ensure you have the best browsing experience on our website. By using our
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/ml-attention-mechanism/ 14/18
1/10/24, 2:14 AM ML - Attention mechanism - GeeksforGeeks
💡 Spotlight
Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Apply for Mentor
Languages DSA
Python Data Structures
Java Algorithms
C++ the best browsing experience on our website. By using
We use cookies to ensure you have DSA
ourfor Beginners
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Basic
PHP PolicyDSA Problems
https://www.geeksforgeeks.org/ml-attention-mechanism/ 15/18
1/10/24, 2:14 AM ML - Attention mechanism - GeeksforGeeks
https://www.geeksforgeeks.org/ml-attention-mechanism/ 16/18
1/10/24, 2:14 AM ML - Attention mechanism - GeeksforGeeks
https://www.geeksforgeeks.org/ml-attention-mechanism/ 17/18
1/10/24, 2:14 AM ML - Attention mechanism - GeeksforGeeks
Colleges Companies
Indian Colleges Admission & Campus Experiences IT Companies
Top Engineering Colleges Software Development Companies
Top BCA Colleges Artificial Intelligence(AI) Companies
Top MBA Colleges CyberSecurity Companies
Top Architecture College Service Based Companies
Choose College For Graduation Product Based Companies
PSUs for CS Engineers
We use cookies to ensure you have the best browsing experience on our website. By using our
site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
https://www.geeksforgeeks.org/ml-attention-mechanism/ 18/18