Attention
Attention
Neighbor Information
We can aggregate the neighbor information using the average value of the encoding neighbor tokens
We can also consider the distance of target token against the neighbor (i.e., if the neighbor is very close to the token, then it has a higher weight), which is the idea of Attention
where $\alpha(x, x_i)$ is related to similarity or distance

Attention Machanism

Hidden state of each word: considering interactions with all the other words directly
Query hidden state:
- $x$: the encoding of query token
- $x_i$: the encoding of all neighbor tokens (within the sentence)
Difference between output hidden state and predefined state: we do not consider the context information as the interactions between the query and the keys for the predefined state, but output hidden state are considered all the tokens
Three questions:
- Character encoding (keys): How to encode character?
We can use one-hot encoding or FCNet (one-hot) to convert one-hot encoding of the tokens to some fixed length vectors - Predefined state (values): How to define the predefined state?
We can use FCNet (one-hot) to convert one-hot encoding of the tokens into some fixed length vectors - Attention weights (dot-product): How to define the attention weight function?
Loss function: Cross-entropy for each word prediction
Training: The common optimizers and Back-propagation
Learnable parameters: The parameters within the FCNet
Example

- Components:
- Input sequence and the corresponding input embedding
- 3 temporary embeddings: queries, keys and values
- queries and keys: used to calculate the values of the importance of specific tokens for the query token
- values encoding: used to get the final output embedding of the query token
We have two inputs “Thinking” and “Machines”, we can convert them into input embedding vectors with length 4 which represent those inputs
For the queries embedding of input token, we have a weight matrix $\mathbf{W}^Q$ from the fully-connected neural network (FCNet) and queries $\mathbf{q}$ with length 3 that $\mathbf{X}_1 = \mathbf{W}^Q\mathbf{q}^T$
For the keys embedding of input token, we have a similar weight matrix $\mathbf{W}^K$ and keys $\mathbf{k}$
For the values embedding of input token, we have a similar weight matrix $\mathbf{W}^V$ and values $\mathbf{v}$
Scaled dot-product attention
Self-attention
For each input $x_i$ in the sentence:
- $x_i$: the original encoding of the token $i$
- $x_j$: the encoding of the token $j$
- $v_j$: the predefined value of the token $j$

Self-attention will consider the attention of the target token against all the tokens including the target token itself, which means all the token can be computed in parallel
Position encoding
Considering the problem of order information in long-term dependency (the time series, sequences)
- $\mathbf{X} \in \mathbb{R}^{x \times d}$: $d$-dimensional embeddings for $n$ tokens of a sequence
- $\mathbf{P} \in \mathbb{R}^{x \times d}$: The value of the $i$-th row and the $2j$-th or the $2j+1$-th column
- $p_{i,2j} = \sin \big( \frac{i}{10000^{\frac{2j}{d}}} \big)$
- $p_{i,2j+1} = \cos \big( \frac{i}{10000^{\frac{2j}{d}}} \big)$

Increase complexity

Multi-head attention
- the final application of the attention model as a pre-train model. The demonstrated Multi-head attention is still used to control the dimension, not for the downstream application and final output
- Similar to GoogLeNet, multiple pastes and do the concatenation.

An FCNet after concatenation of attention output can control the dimension (tokenwize: we apply to each token individually)
Stack more attention layers
Transformer

Within each block:
- Multi-head attention to learn the hidden state
- FCNet to control dimension (for easy implementation)
- Multiple blocks: increase complexity
- Masked attention: do not consider padding position
Function of Transformer
- Machine translation
- Token-level prediction
- Sentence-level prediction
- Text generation
Questions
Attention has a much better computational parallel property than RNN (true)
The number of parameters in the attention model depends on the maximum length of the input sequences (false: The number of parameters in the attention model is the parameters in the FCNet, use the FCNet to compute the encoding (inputs to the attention model) and to control the embadding)
The traditional attention mechanism does not consider the local pattern explicitly (true: because in an attention model, it calculate the token based on all the tokens within the all the tokens, which does not emphasize the local pattern. Instead, attention learn the relations)
For the sequence to sequence model, we can use attention as an alternative to RNN (true)


