SeriesDeep Learning11 / 20

Neural Attention Models (Meta)

Module 11 of CS 7643 - Deep Learning @ Georgia Tech.

Introduction#

Attention is a neural mechanism that weights elements of a set for use by the network in the current computation. It enables a network to dynamically attend to different portions of the input depending on relevance to the current information being passed through the network.

Attention is a powerful technique used across subfields of deep learning, including computer vision and natural language processing.

Softmax#

Recall that the Softmax function converts a set of numbers into a probability distribution.

Softmax({x1,,xn})={ex1Σiexi,,exnΣiexi}\text{Softmax}(\begin{Bmatrix}x_1, \ldots, x_n \end{Bmatrix}) = \begin{Bmatrix} \frac{e^{x_1}}{\Sigma_i e^{x_i}}, \ldots, \frac{e^{x_n}}{\Sigma_i e^{x_i}} \end{Bmatrix}

Sigmoid vs. Softmax#

Softmax is a generalization of the sigmoid function, which is used in the case of binary classification. As part of binary classification, we have two scores: the score obtained from our classification model for the positive class s+s_+, and the implicit score of 0 for the negative class. Consider the case of s+=6s_+ = 6.

Softmax({0,6})={e0e0+e6,e6e0+e6}={0.0025,0.9975}\text{Softmax}(\begin{Bmatrix} 0, 6 \end{Bmatrix}) = \begin{Bmatrix} \frac{e^0}{e^0 + e^6}, \frac{e^6}{e^0 + e^6} \end{Bmatrix} = \begin{Bmatrix} 0.0025, 0.9975 \end{Bmatrix}

σ(6)=11+e6=0.9975\sigma(6) = \frac{1}{1 + e^{-6}} = 0.9975

We can also show that the sigmoid function is mathematically equivalent to softmax in the case of a single positive class score, and negative class score of 0.

Softmax(s+)=es+es++e0=es+es+es+es++1es+=11+es+\text{Softmax}(s_+) = \frac{e^{s_+}}{e^{s_+} + e^0} = \frac{\frac{e^{s_+}}{e^{s_+}}}{\frac{e^{s_+}}{e^{s_+}} + \frac{1}{e^{s_+}}} = \frac{1}{1 + e^{-s_+}}

Softmax Characteristics#

Greater input scores result in a greater difference in the resulting probability distribution. For example, the probability corresponding to the argmax class will be higher when all input scores are doubled.

The most important property of softmax is that it is differentiable. This is why the function is called “soft”max, since it is a software version of argmax.

By generating a probability distribution over the input elements, softmax can be used to randomly sample from the inputs. This is particularly useful in the case of attention.

Attention#

Attention refers to the process of weighting a set of vectors to be used in combination with some input for a prediction, within the context of a neural network. Mathematically, attention calculates a distribution over its inputs depending on similarity between some query vector, a set of keys, and a set of values.

In the case of self-attention, the KK and VV are calculated within the same attention layer as qq. Cross-attention pulls KK and VV from another network or layer.

Differentiable Motivation#

In the case of attention, we want to identify the most similar vector to an input (query) vector. Given our set of key vectors {k1,,kn}\begin{Bmatrix} k_1, \ldots, k_n \end{Bmatrix} and a query vector qq, we can select the most similar vector by maximizing the dot product (recall that the dot product is unscaled cosine similarity).

j^=argmaxjujq\hat{j} = \arg \max_j u_j \cdot q

However, since the argmax function is not differentiable, we cannot use it as part a neural network (since we can’t apply backpropagation). Due to its differentiable nature, softmax can be used instead! Attention calculates the similarity scores between each key vector and query, then converts the resulting scores to a probability distribution via softmax.

Let K represent the set of vectors\text{Let} ~ K~ \text{represent the set of vectors}

p=Softmax(qKT)p = \text{Softmax}(q \cdot K^T)

Implementation within Neural Networks#

Attention as a neural network layer is a relatively new development (~2013). As mentioned earlier, attention is computed as follows:

  1. Calculate the dot product between a set of key vectors and a query vector.
  2. Pass the resulting scores through the softmax function.
  3. Use the resulting probability distribution to perform a weighted sum over value vectors.

Assume that K=V={u1,,uN}K = V = \begin{Bmatrix} u_1, \ldots, u_N \end{Bmatrix}, and that aa represents the attention weights.

attention-viz

aj=eujqΣkeukqa_j = \frac{e^{u_j \cdot q}}{\Sigma_k e^{u_k \cdot q}}

output=Σkakuk\text{output} = \Sigma_k a_k u_k

There are certain variants of attention commonly used in practice:

Transformers#

A Transformer is a neural network which implements multi-layer attention, and is currently state-of-the-art for most language modeling tasks (as well as other subfields of deep learning). Transformers have the following key components:

  1. Self Attention: attention where input to the layer is also considered the set of keys.
    • qiK,  K=Vq_i \in K, ~~ K = V (lecturer refers to K=VK = V as set of controller states UU)
  2. Multi-Headed Attention: splits attention calculation into multiple heads - subdivisions of the key set - that work in parallel. Each head independently attends to the input, but learns to focus on a different type of information or relationship.
  3. Residual Connections: not specific to transformers, but used to help stabilize the gradient when backpropagating through deep neural networks.

We can think of the combination of self-attention and multi-headed attention as an N x MN ~ \text{x} ~ M situation:

For each of the NN positions, the MM context vectors are concatenated and passed through a linear layer. This produces a rich output vector for each of the original NN terms, which may then be passed to the next layer of the network.


(all images obtained from Georgia Tech DL course materials)

License

CC BY-NC-SA 4.0 This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Related Posts