logo

ルオン・アテンション 📂機械学習

ルオン・アテンション

導入

それぞれクエリ/キー/バリューと呼ばれる三つの行列$\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} $$


  1. 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. ↩︎