An attention mechanism is a deep learning component that performs a linearly weighted sum of value vectors, where the weights (alphas) are computed using a query and key vectors through functions like dot product followed by softmax normalization, and learnable parameters are introduced via linear transformations of keys, values, and queries to enable model training.
Attention Mechanism Explained: Deep Learning Basics
Added:[Music] before we dive in let's start off with a generic deep learning model we have an input that is transformed to an output if you want to be theoretical you can understand a model as a probability distribution over the output given the input but for now let's just think of it as something which goes from an input to an output this could be for any task image classification exam grade prediction sentiment analysis dog bar classifier whatever you want an attention mechanism is just another lego block that can be used in any deep learning model that one builds in december 2017 when the paper attention is all you need was released it was shown that an attention mechanism is apparently the only lego block you need okay by this i mean you can do away with a more traditional rnns recurrent neural networks these lego blocks can be entirely replaced by an attention mechanism lego block when dealing with sequential data in fact attention mechanisms are so powerful that they allow models to not only perform better but also train faster which in our world of finite computational resources is a great gift so that we can all actually benefit from this magical lego block let's understand what an attention mechanism actually is we have sequential data each data point in our sequence is a vector of numbers this for example could be word embeddings in a natural language processing nlp task let's call these input vectors values so we have vectors v 1 to v n where we don't actually know the length of the sequence so we are going to use some unknown variable n we are however fortunate in that the vectors are of a fixed dimension which we can call dv the aim of an attention mechanism is to simply perform a linearly weighted sum of the value vectors we would ideally want to train the model to learn the optimal weights the alphas to perform the best the task the model is being useful however we cannot simply learn the optimal alphas because the input is variable length n can change from one input sequence to the next so we wouldn't even have a fixed number of alphas to train another restriction on the alpha weights is that we don't want to scale the input vectors in any manner i.e we need our alphas to be normalized which means they must sum to one thus to obtain these alpha weights we cleverly make use of something called a key and a query like in a dictionary in python every value has an associated key often the key is the value itself and this is called self-attention however we will talk about attention generically so let's have our sequence of key vectors from k1 to kn the query q is also a vector but for a single particular attention mechanism there will only be one query depending on the task the query can be constructed from a range of different places now that we have a query and a key we can obtain the weightings for the values the alphas there will be some function that combines the query and the respective key to obtain the linear weighting for the corresponding value as an example a popular example is dot product attention where we simply perform the dot product between the key and the query apply a non-linearity often the hyperbolic tan function and then apply the softmax function to ensure the alphas are still normalized and sum to 1.
so if we try to summarize the attention mechanism we have some simple equations to describe our lego block at the input we have a query keys and values the output y is a linear weighted sum of the values the weights alphas are computed using the query and the keys however there is something huge missing here you may have noticed that all the equations here are very deterministic we don't have any learnable parameters that can be updated during the training of a model that uses attention we can introduce fixed size matrices of learnable parameters by performing linear transformations of our keys values and query the q k dash and v dash denote the query keys and values that we have so far been using in our equations the fixed size matrices wq wk and wv are then the parameters that can be learnt by the model and can be trained to perform as well as possible on the task at hand by updating these parameters and that is attention for you
Up Next

Temporal Fusion Transformers Explained | Time Series Forecasting
@DataHeroes
18K views•2024-09-29

Building Real-Time ML Pipelines with Feature Stores and MLOps Frameworks
@ODSCAI
5.1K views•2022-02-20

Bypassing Tor Censorship: Bridges and Pluggable Transport Guide
@Coding_ForEveryone
397 views•2024-06-11

Neural Networks Explained: Math, Layers, and Learning Fundamentals
@3blue1brown
21.9M views•2017-10-05
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Artificial Intelligence




































![[SPCL_Bcast #50] Hardware-aware Algorithms for Language Modeling](https://i.ytimg.com/vi/SyB-GVnCX9Q/maxresdefault.jpg)







