루옹 어텐션
도입
각각 쿼리/키/밸류라 불리는 세 행렬 $\mathbf{Q} \in M^{d_{k} \times n}$, $\mathbf{K} \in M^{d_{k} \times m}$, $\mathbf{V} \in M^{d_{v} \times m}$과 쿼리와 키에 대한 스코어 함수 $f : M^{d_{k} \times n} \times M^{d_{k} \times m} \to M^{m \times n}$이 주어졌다고 하자. 어텐션이란 다음과 같이 정의되는 함수를 말한다.
$$ \begin{align*} \operatorname{Attention}: M^{d_{k} \times n} \times M^{d_{k} \times m} \times M^{d_{v} \times m} &\to M^{d_{v} \times n} \\ (\mathbf{Q},\mathbf{K},\mathbf{V}) &\mapsto \mathbf{V} \operatorname{Softmax}\left(f(\mathbf{Q},\mathbf{K})\right) \end{align*} $$
여기서 $\operatorname{Softmax}$는 주어진 행렬 $\mathbf{X} = \begin{bmatrix} \mathbf{x}_{1} & \cdots & \mathbf{x}_{N}\end{bmatrix}$에 대해서 각 열벡터에 소프트맥스 $\operatorname{softmax}$를 취해서 각 열의 합이 $1$이 되게 하는 함수를 말한다.
$$ \operatorname{Softmax}(\mathbf{X}) := \begin{bmatrix} \underset{\vert}{\overset{\vert}{\operatorname{softmax}(\mathbf{x}_{1})}} & \cdots & \underset{\vert}{\overset{\vert}{\operatorname{softmax}(\mathbf{x}_{N})}} \end{bmatrix} $$
$f$를 어떻게 정의하느냐에 따라 어텐션의 종류가 구분되는데, Luong 외 2명이 논문 『Effective approaches to attention-based neural machine translation』1 에서 제안한 형태의 어텐션은 아래와 같다.
정의
닷프로덕트 어텐션:
아래 형태의 어텐션 함수를 닷프로덕트 어텐션dot-product attention이라 한다.
$$ \operatorname{Attention}(\mathbf{Q},\mathbf{K},\mathbf{V}) = \mathbf{V} \operatorname{Softmax} (\mathbf{K}^{\mathsf{T}} \mathbf{Q}) $$
일반 어텐션:
아래 형태의 어텐션 함수를 루옹 일반 어텐션Luong general attention이라 한다.
$$ \operatorname{Attention}(\mathbf{Q},\mathbf{K},\mathbf{V}) = \mathbf{V} \operatorname{Softmax} (\mathbf{K}^{\mathsf{T}} \mathbf{W} \mathbf{Q}) $$
설명
Luong의 논문에서는 각각 'dot', 'general'이라는 이름으로 구분하고 있다. 'concat'이라 소개된 형태는 Bahdanau 외 2명이 소개한 어텐션에 이름을 붙인 것이다.
닷프로덕트 어텐션
쿼리 벡터 $\mathbf{q}$와 키 벡터 $\mathbf{k}$의 스코어 함수를 내적으로 둔 것이다.
$$ \operatorname{score}(\mathbf{q}, \mathbf{k}) = \mathbf{k}^{\mathsf{T}} \mathbf{q} $$
$$ f(\mathbf{Q}, \mathbf{K}) = \mathbf{K}^{\mathsf{T}} \mathbf{Q} =\begin{bmatrix} \mathbf{k}_{1} \cdot \mathbf{q}_{1} & \mathbf{k}_{1} \cdot \mathbf{q}_{2} & \cdots & \mathbf{k}_{1} \cdot \mathbf{q}_{n} \\ \mathbf{k}_{2} \cdot \mathbf{q}_{1} & \mathbf{k}_{2} \cdot \mathbf{q}_{2} & \cdots & \mathbf{k}_{2} \cdot \mathbf{q}_{n} \\ \vdots & \vdots & \ddots & \vdots \\ \mathbf{k}_{m} \cdot \mathbf{q}_{1} & \mathbf{k}_{m} \cdot \mathbf{q}_{2} & \cdots & \mathbf{k}_{m} \cdot \mathbf{q}_{n} \end{bmatrix} $$
트랜스포머 논문에서 소개된 스케일드 닷프로덕트 어텐션은 아래와 같이 여기에 스케일링 팩터 $1/\sqrt{d_{k}}$를 곱한 것이다.
$$ \operatorname{Attention}(\mathbf{Q},\mathbf{K},\mathbf{V}) = \mathbf{V} \operatorname{Softmax} \left( \dfrac{\mathbf{K}^{\mathsf{T}} \mathbf{Q}}{\sqrt{d_{k}}} \right) $$
일반 어텐션
쿼리 벡터 $\mathbf{q}$와 키 벡터 $\mathbf{k}$의 스코어 함수를 쌍선형 변환으로 둔 것이다.
$$ \operatorname{score}(\mathbf{q}, \mathbf{k}) = \mathbf{k}^{\mathsf{T}} \mathbf{W} \mathbf{q} $$
$$ f(\mathbf{Q}, \mathbf{K}) = \mathbf{K}^{\mathsf{T}} \mathbf{W} \mathbf{Q} =\begin{bmatrix} \mathbf{k}_{1}^{\mathsf{T}} \mathbf{W} \mathbf{q}_{1} & \mathbf{k}_{1}^{\mathsf{T}} \mathbf{W} \mathbf{q}_{2} & \cdots & \mathbf{k}_{1}^{\mathsf{T}} \mathbf{W} \mathbf{q}_{n} \\ \mathbf{k}_{2}^{\mathsf{T}} \mathbf{W} \mathbf{q}_{1} & \mathbf{k}_{2}^{\mathsf{T}} \mathbf{W} \mathbf{q}_{2} & \cdots & \mathbf{k}_{2}^{\mathsf{T}} \mathbf{W} \mathbf{q}_{n} \\ \vdots & \vdots & \ddots & \vdots \\ \mathbf{k}_{m}^{\mathsf{T}} \mathbf{W} \mathbf{q}_{1} & \mathbf{k}_{m}^{\mathsf{T}} \mathbf{W} \mathbf{q}_{2} & \cdots & \mathbf{k}_{m}^{\mathsf{T}} \mathbf{W} \mathbf{q}_{n} \end{bmatrix} $$
Minh-Thang Luong et al. Effective approaches to attention-based neural machine translation. Proceedings of the 2015 conference on empirical methods in natural language processing. 2015. ↩︎

저희들의 저서 「줄리아 프로그래밍」이 2024 세종도서 학술부문에 선정되었습니다!

