深層学習で使用される手法の1つである「Attention」について、書いた記事をまとめた.
- #まとめ編 一覧
Index
- Index
- Attention とは
- データ分野
- 用語定義
- スコア関数 / Score Function
- 基本的な Attention
- Attention の応用
- Transformer / 2017
- Recurrent Independent Mechanisms / RIMs / 2019
- Staircase Attention / 2021
- Rectified Linear Attention / ReLA / 2021
- FlashAttention / 2022
- Attention Manipulation / ATMAN / 2023
- Exponential Signal Preserving Attention / E-SPA / 2023
- Intention / 2023
- PagedAttention / 2023
- Attention Sinks / 2023
- Griffin / 2024
- テクニック
- 参考
Attention とは
深層学習を用いた特徴量を抽出する仕組み.
- 深層学習 #まとめ編
データのどこに注目するべきかを含め学習することで、特徴量を抽出する.
- Attention #概念編
データ分野
「時系列データ」の「生成モデル」 (seq2seq / Encoder-Decoder) への改善手法として提案された「Attention」だが、 「時系列データ」の「予測モデル」や「画像データ」への適用も行われている.
画像データへの応用
時系列データへの応用
Multi Modal への応用
- Multi Modal
用語定義
入力系列
- (Encoder)入力系列 :
- 入力系列の長さ :
- Decoder入力系列 :
- 入力系列の長さ :
Encoder - Decoder 出力
- Encoder 出力 (隠れ状態) :
- Memory に相当
- Decoder 出力 (隠れ状態) :
Attention
- 重要度 / Attention Weight :
- alignment score function / アラインメント スコア 関数
- 重要度を計算するための関数
- コンテキストベクトル / 重み付き平均 :
または、
- Query : 入力情報 (q)
- (Decoder の隠れ状態 / 入力系列などにあたる)
- Memory : 情報源
- Key : Query と一緒に、「重要度・関連度」の計算に使用するための Memory (k)
- Value : Attention Weight と計算して、コンテキストベクトルを取得するための Memory (v)
- (Encoder の全時系列の隠れ状態などにあたる)
スコア関数 / Score Function
Query と Key が、どの程度似ているか、どの程度関連があるかを計算する関数.
内積が使用されるケースが多いが、複数の関数が手法として提案されている.
- スコア関数 / Score Function
基本的な Attention
Self vs Not Self
情報の抽出を自分自身 (self) から行うか別の情報から行うかどうか
Self Attention
入力Queryと索引Memoryが同じAttention.
- Self Attention
Source Target Attention
入力Queryと索引Memoryが別物の場合のAttention.
Soft vs Hard
コンテキストベクトルを求める際、どのような方法で求めるか.
Soft Attention
複数の情報源のベクトル に対して、それぞれの重要度
を別々のネットワークで計算し、その重み付き平均を使うのが Soft Attention です. Soft Attention の計算はすべて微分可能な関数のみで構成されているので、
通常の誤差逆伝播法で勾配を計算することができる.
(「ゼロから作るDeep Learning 2」より)
- seq2seq
Hard Attention
Soft Attention の場合は、確率 をそのまま、重み付き平均、すなわち期待値をとっていた.
Hard Attention の場合は、その確率 に従って、ベクトル(情報)を選ぶ.
Local vs Global
Local Attention
- Local Attention
- 注意する範囲を選択する Attention
- yhayato1320.hatenablog.com
Global Attention
- Global Attention
- 注意できる範囲すべてを注意する Attention
- yhayato1320.hatenablog.com
Multi Head Attention
複数のAttention を並べる.
Transformer で利用.
- Multi Head Attention
Attention の応用
Transformer / 2017
- Transformer
Recurrent Independent Mechanisms / RIMs / 2019
- Recurrent Independent Mechanisms
- [2019]
- arxiv.org
Staircase Attention / 2021
時間方向 (シーケンス全体にわたる) と深さ方向 (層をまたぐ) の両方でリカレンスを活用. これにより、従来の Transformer では困難だった状態追跡タスクを解決する. また、パラメータ数が同じであれば、Transformer と比較して言語モデリングにおいてより優れたパフォーマンス (低いパープレキシティ) を発揮する.
Staircase Attention for Recurrent Processing of Sequences
- [2021]
- arxiv.org
Which one is more important: more parameters or more computation?
Rectified Linear Attention / ReLA / 2021
従来のTransformerモデルで使われるSoftmax関数の代わりにReLUを採用した新しいアテンション機構「Rectified Linear Attention (ReLA)」を提案.
ReLUを用いることで、不要な情報を完全に遮断する高いスパース性 (疎性) が自然に形成され、モデルの解釈性が向上.
学習を安定させるためにレイヤー正規化やゲーティング機能を導入しており、従来のスパースな手法よりも高い計算効率を実現.
機械翻訳タスクの実験では、標準的なモデルと同等の精度を維持しつつ、高速な処理が可能であることを示している.
さらに、特定の入力に対して全く注目しない「Null Attention」という独自の現象が発生し、これが翻訳品質の判定に役立つ可能性も示唆されている.
- Sparse Attention with Linear Units
- [2021]
- arxiv.org
Sparse Attention with Linear Units
— AK (@_akhaliq) April 15, 2021
pdf: https://t.co/g0Qsx4Clmr
abs: https://t.co/HP7UkNJW2W pic.twitter.com/8rPmVB4LH3
FlashAttention / 2022
注意計算のボトルネックはGPUのSRAMとHBM(high bandwidth memory)間のデータのやり取りにあるとし、 これを削減するための計算方法を考案.
この工夫により、注意機構の計算で2-4倍の高速化と10-20倍のメモリ削減が可能.
- NeurIPS 2022 参加報告 後編
- 大規模言語モデル
- 計算コストの削減
- blog.recruit.co.jp
- FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness
- [2022]
- arxiv.org
Attention Manipulation / ATMAN / 2023
- AtMan: Understanding Transformer Predictions Through Memory Efficient Attention Manipulation
- [2023]
- arxiv.org
Exponential Signal Preserving Attention / E-SPA / 2023
- Deep Transformers without Shortcuts: Modifying Self-attention for Faithful Signal Propagation
- [2023]
- arxiv.org
Intention / 2023
- Exploring the Space of Key-Value-Query Models with Intention
- [2023]
- arxiv.org
PagedAttention / 2023
Efficient Memory Management for Large Language Model Serving with PagedAttention
- [2023]
- arxiv.org
vLLM: Easy, Fast, and Cheap LLM Serving with PagedAttention
大規模言語モデルの出力スピードを最大24倍に高めるライブラリ「vLLM」が登場、メモリ効率を高める新たな仕組み「PagedAttention」とは?
Attention Sinks / 2023
Efficient Streaming Language Models with Attention Sinks
- [2023]
- arxiv.org
Efficient Streaming Language Models with Attention Sinks (Paper Explained)
Griffin / 2024
- Griffin: Mixing Gated Linear Recurrences with Local Attention for Efficient Language Models
- [2024]
- arxiv.org
テクニック
Token Dropping / 2022
BERTのような大規模言語モデルの事前学習にかかる時間とコストを25%削減することを目的としており、性能を維持しながら、重要でないトークンを中間層から削除することで効率化を図る.
このアプローチは、タスクに依存しない事前学習を加速させ、将来的により長い文脈を分析するTransformerモデルへの応用が期待されている.
Token Dropping for Efficient BERT Pretraining
- [2022]
- arxiv.org
The Token-Dropping Approach Used By ML Researchers From Google and NYU Reduces BERT Pretraining Time And Cost By 25%
参考
書籍
生成 Deep Learning / オライリー
- 7.2.2 Keras で アテンション機構を作成する (内容は Self Attention)
- 7.2.4 エンコーダ - デコーダネットワークのアテンション機構 (内容は Source Target Attention / Soft Attention)
-
深層学習による自然言語処理
- 4.1 注意機構 (4.1.1 ソフト注意機構 / 4.1.2 ハード注意機構)
-
ゼロから作るDeep Learning 2
- 8章 Attention (内容は Source Target Attention / Soft Attention)
-
コンピュータービジョン最前線 Winter 2021
- 5 ニュウモン Vision and Language
- 5.3 V&L を支える基礎技術
- 5.3.4 注意機構によるモダリティ統合
- 5.3 V&L を支える基礎技術
- 5 ニュウモン Vision and Language
Web Site
Attention? Attention!
- Attention 種類まとめ
- lilianweng.github.io
最近の深層学習におけるAttention機構 - 名古屋CVPRML勉強会 ver. -
Attention再入門 is all you need
動画
Deep Learning入門:Attention(注意)
- Self Attention
- 画像データへの Attention
- 言語(系列)データへの Attention
- www.youtube.com
【速習!】Attentionから始めるTransformer超入門



