images are from https://zh-v2.d2l.ai/
-
an architecture commonly used in NLP and other types of tasks
-
Encoder: take raw input and represent the input as tensors after processing(could be word2vec, neural layers, attention...)
-
Decoder: mainly for outputting the result to desire form([0, 1], probability distribution, classification, etc)
from torch import nn class Encoder(nn.Module): def __init__(self, **kwargs): super(Encoder, self).__init__(**kwargs) def forward(self, X, *args): raise NotImplementedError class Decoder(nn.Module): def __init__(self, **kwargs): super(Decoder, self).__init__(**kwargs) def init_state(self, encoder_outputs, *args): raise NotImplementedError def forward(self, X, state): raise NotImplementedError
-
A specific type of tasks whose input and output are both sequences of any length
-
Ex. Machine Translation
-
Common arch of seq2seq models:
-
Machine Translation using RNN
-
BLEU(Bilingual Evaluation Understudy) for machine translation
-
formula: $\exp\left(\min\left(0, 1 - \frac{\mathrm{len}{\text{label}}}{\mathrm{len}{\text{pred}}}\right)\right) \prod_{n=1}^k p_n^{1/2^n}$
-
where
$p_n$ represent then-gramaccuracy
-
-
Attention Mechanism, KVQ
-
Key: what is presented -
Value: sensory inputs(?) -
Query: what we are interested - The idea is to using Query to find "important"
Keys
-
-
Attention Score,
$\alpha(x, x_i)$ - model the relationship(importance, similarity) of
Keys&Querys - Kernel Regression
$\alpha(x, x_i) = \frac{K(x - x_i)}{\sum_{j=1}^n K(x - x_j)}$
- Additive Attention
$a(\mathbf q, \mathbf k) = \mathbf w_v^\top \text{tanh}(\mathbf W_q\mathbf q + \mathbf W_k \mathbf k) \in \mathbb{R},$
- Scaled Dot-Product Attention
$a(\mathbf q, \mathbf k) = \mathbf{q}^\top \mathbf{k} /\sqrt{d}$ - Matrix form:
$\mathrm{softmax}\left(\frac{\mathbf Q \mathbf K^\top }{\sqrt{d}}\right) \mathbf V \in \mathbb{R}^{n\times v}.$
- model the relationship(importance, similarity) of