Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Transformerのゼミ発表資料

 Transformerのゼミ発表資料

Speech & Language Processingの10章前半をスライドにまとめました。
目次
introduction
10.1 The Transformer: A Self-Attention Network
10.1.1 Transformers: the intuition
10.2 Causual or backward-looking self attention
10.1.3 Self-attention more formally
10.1.4 Parallelizing self-attention using a single matrix X
10.1.5 Masking out the future
10.2 Multi-Head Attention
10.3 Transformer Blocks
10.4 The Residual Stream view of the Transformer Block
10.5 The input: embeddings for token and position

※学部生が作った資料なので間違いがある可能性があります。誤りを見つけた場合はコメントなどで指摘していただけると幸いです。

DaichiWarasuga

June 11, 2024
Tweet

Other Decks in Research

Transcript

  1. 目次 i. introduction ii. 10.1 The Transformer: A Self-Attention Network

    i. 10.1.1 Transformers: the intuition ii. 10.2 Causual or backward-looking self attention iii. 10.1.3 Self-attention more formally iv. 10.1.4 Parallelizing self-attention using a single matrix X v. 10.1.5 Masking out the future iii. 10.2 Multi-Head Attention iv. 10.3 Transformer Blocks v. 10.4 The Residual Stream view of the Transformer Block vi. 10.5 The input: embeddings for token and position
  2. 目次 i. introduction ii. 10.1 The Transformer: A Self-Attention Network

    i. 10.1.1 Transformers: the intuition ii. 10.2 Causual or backward-looking self attention iii. 10.1.3 Self-attention more formally iv. 10.1.4 Parallelizing self-attention using a single matrix X v. 10.1.5 Masking out the future iii. 10.2 Multi-Head Attention iv. 10.3 Transformer Blocks v. 10.4 The Residual Stream view of the Transformer Block vi. 10.5 The input: embeddings for token and position
  3. 4 Introduction transformerって何?? • 2017年にgoogleが出した論文「Attention is all you need」で提案された言語モデル •

    要約、機械翻訳、質問応答、チャットボットなどのテキ スト生成タスクに強い • 従来のモデルの欠点を克服している • 長距離依存関係の把握 • 並列処理能力 • 現在、TransformerはNLPにおいて必要不可欠な存在 となった。 • 元はテキストデータを扱うモデルだったが、画像処理 や音声処理でも使われている
  4. 6 Introduction 無視 入力と出力のみに注目 Transformerを関数と見ると… X : 入力(テキスト) Y :

    単語の確率分布 θ : モデルパラメータ X = “ Aの次は” C B E A 7章のFFNと同じ
  5. 7 Introduction Transformer block ØFeedforward layer ØMulti-Head Attention ØResidual connections

    ØLayer Norm Øinput Embedding ØPositional Encoding この図はここの Multi-Head Attentionを説明する ための図 前半
  6. 目次 i. introduction ii. 10.1 The Transformer: A Self-Attention Network

    i. 10.1.1 Transformers: the intuition ii. 10.2 Causual or backward-looking self attention iii. 10.1.3 Self-attention more formally iv. 10.1.4 Parallelizing self-attention using a single matrix X v. 10.1.5 Masking out the future iii. 10.2 Multi-Head Attention iv. 10.3 Transformer Blocks v. 10.4 The Residual Stream view of the Transformer Block vi. 10.5 The input: embeddings for token and position
  7. 10 10.1.1 Transformers: the intuition transformerアーキテクチャの直感的理解 Ø 目的 : 入力文章の文脈を表す表現(ベクトル)を獲得すること

    例)Spring Ø 意味1: 春 Ø “The flowers bloom in spring.” Ø 意味2: 泉 Ø “We found a natural spring in the forest.” Ø 意味3: 跳ねる Ø “The cat will spring at the toy.” ※静的ベクトルと呼ぶ word2vecの場合 → 同じ単語なら意味ベクトルは全て同じ 文脈によって意味が違うのに…
  8. 11 10.1 The Transformer: A Self-Attention Network transformerアーキテクチャの直感的理解 Ø 目的

    : 入力文章の文脈を表す表現(ベクトル)を獲得すること 例)Spring Ø 意味1: 春 Ø “The flowers bloom in spring.” Ø 意味2: 泉 Ø “We found a natural spring in the forest.” Ø 意味3: 跳ねる Ø “The cat will spring at the toy.” Transformerの場合 → 文脈ごとに異なる意味ベクトル “flowers bloom”だから「春」の意味 「自然」「森」だから「泉」の意味 動詞だから「跳ねる」の意味 文脈を広く見て表現を計算するアルゴリズム → Attention
  9. 12 10.1 The Transformer: A Self-Attention Network transformerアーキテクチャの直感的理解 Ø 例)

    : The animal didn’t cross the street because it was too tired. ”it”の意味を理解しようとしている “animal”に強く依存しているっぽい 以下は各入力トークン間の重要度(重み)を色で表している 離れたトークン間の関係も学習できている!
  10. 目次 i. introduction ii. 10.1 The Transformer: A Self-Attention Network

    i. 10.1.1 Transformers: the intuition ii. 10.1.2 Causual or backward-looking self attention iii. 10.1.3 Self-attention more formally iv. 10.1.4 Parallelizing self-attention using a single matrix X v. 10.1.5 Masking out the future iii. 10.2 Multi-Head Attention iv. 10.3 Transformer Blocks v. 10.4 The Residual Stream view of the Transformer Block vi. 10.5 The input: embeddings for token and position
  11. 14 10.2 Causual or backward-looking self attention (Self-Attentionにとっての)文脈とは? 因果関係 過去の情報

    親父に殴られた! 例) 原因 結果 万引きしたのがバレて 親父に殴られた! 泥酔して暴力的になった 親父に殴られた! 例) 一昨日新しい論文が出て、それは非常に革新的だった。
  12. 15 10.2 Causual or backward-looking self attention Self-Attentionのデータの流れ 過去 未来

    Self-Attention Layer 各項目の計算は 独立している 並列化できる (= GPUが活用できる) → つまりnvidiaが儲かる → 自己回帰的に生成回帰的に生成 ※現在の値が過去の値に依存して決まること 未来の情報が過去の出力に 影響を与えていない
  13. 目次 i. introduction ii. 10.1 The Transformer: A Self-Attention Network

    i. 10.1.1 Transformers: the intuition ii. 10.2 Causual or backward-looking self attention iii. 10.1.3 Self-attention more formally iv. 10.1.4 Parallelizing self-attention using a single matrix X v. 10.1.5 Masking out the future iii. 10.2 Multi-Head Attention iv. 10.3 Transformer Blocks v. 10.4 The Residual Stream view of the Transformer Block vi. 10.5 The input: embeddings for token and position
  14. 20 10.1.3 Self-attention more formally Self-Attentionの計算 過去 未来 ブロックの中で行われている計算→ 3以下の入力でそれぞれ類似度を計算

    SoftMaxで正規化(𝛼!" , 𝛼!! も同様に計算) 重みつけしてその合計を計算 実はTransformerは 類似度計算の入力にひと工夫している
  15. 21 10.1.3 Self-attention more formally TransformerでのSelf-Attentionの工夫 入力𝑥は三つの役割に分けることができる 例):The dog is

    tired. value : 最終的に使用する重み付けされる情報 query : 注目する単語 ”tired”に注目 key : 比較対象の単語 “The”, “dog”, “is”, “tired”とqueryを比較
  16. 22 10.1.3 Self-attention more formally TransformerでのSelf-Attentionの工夫 この3つの役割を捉えるために、それぞれの入力に対して重み行列を導入する。 value : query

    : key : Transformer Blockではこの𝑊", 𝑊#, 𝑊$を学習する ※”大きな値を指数関数に入力すると数値的な問題が発生し、トレー ニング中に勾配が実質的に失われる可能性があります”→ next page
  17. 目次 i. introduction ii. 10.1 The Transformer: A Self-Attention Network

    i. 10.1.1 Transformers: the intuition ii. 10.2 Causual or backward-looking self attention iii. 10.1.3 Self-attention more formally iv. 10.1.4 Parallelizing self-attention using a single matrix X v. 10.1.5 Masking out the future iii. 10.2 Multi-Head Attention iv. 10.3 Transformer Blocks v. 10.4 The Residual Stream view of the Transformer Block vi. 10.5 The input: embeddings for token and position
  18. 25 10.1.4 Parallelizing self-attention using a single matrix X 計算の並列化

    先ほどの計算を行列表現を用いて並列計算可能にしていく 𝑛はトークン数 各𝑥は1×dのベクトル
  19. 26 10.1.4 Parallelizing self-attention using a single matrix X 計算の並列化

    先ほどの計算を行列表現を用いて並列計算可能にしていく 𝑞! 𝑞" 𝑘! 𝑘"
  20. 目次 i. introduction ii. 10.1 The Transformer: A Self-Attention Network

    i. 10.1.1 Transformers: the intuition ii. 10.2 Causual or backward-looking self attention iii. 10.1.3 Self-attention more formally iv. 10.1.4 Parallelizing self-attention using a single matrix X v. 10.1.5 Masking out the future iii. 10.2 Multi-Head Attention iv. 10.3 Transformer Blocks v. 10.4 The Residual Stream view of the Transformer Block vi. 10.5 The input: embeddings for token and position
  21. 28 10.1.5 Masking out the future 並列計算時の注意点 𝑞! " 𝑘"

    𝑞# # 𝑘! 𝑞# # 𝑘$ 𝑞# # 𝑘% 𝑞" # 𝑘! 𝑞" # 𝑘$ 𝑞! # 𝑘% 𝑞! # 𝑘$ 𝑞! # 𝑘% 𝑞$ # 𝑘% 𝑞! 𝑞" 𝑞# 𝑞$ 𝑞% 𝑘! 𝑘" 𝑘# 𝑘$ 𝑘% 未来の情報を使わない → 𝑗 > 𝑖 となる項は使わない ※ ちなみに… Transformerの欠点としてメモリ消費の多さがある。入力トークン数に対して注意計算のコストが𝑂(𝑛")に比例して増 加する(QK.Tの要素数が増えるため) さらに、Q、K、Vの各行列自体も大量の要素を持つため、全体的なメモリ使用量が非常に高くなる。
  22. 29 10.1.5 Masking out the future 並列計算時の注意点 𝑞! " 𝑘"

    𝑞# # 𝑘! 𝑞# # 𝑘$ 𝑞# # 𝑘% 𝑞" # 𝑘! 𝑞" # 𝑘$ 𝑞! # 𝑘% 𝑞! # 𝑘$ 𝑞! # 𝑘% 𝑞$ # 𝑘% 使わない要素を−∞に設定し未来の知識を排除する −∞ −∞ −∞ −∞ −∞ −∞ −∞ −∞ −∞ −∞ 𝑞! " 𝑘" 𝑞# # 𝑘! 𝑞# # 𝑘$ 𝑞# # 𝑘% 𝑞" # 𝑘! 𝑞" # 𝑘$ 𝑞! # 𝑘% 𝑞! # 𝑘$ 𝑞! # 𝑘% 𝑞$ # 𝑘%
  23. 目次 i. introduction ii. 10.1 The Transformer: A Self-Attention Network

    i. 10.1.1 Transformers: the intuition ii. 10.2 Causual or backward-looking self attention iii. 10.1.3 Self-attention more formally iv. 10.1.4 Parallelizing self-attention using a single matrix X v. 10.1.5 Masking out the future iii. 10.2 Multi-Head Attention iv. 10.3 Transformer Blocks v. 10.4 The Residual Stream view of the Transformer Block vi. 10.5 The input: embeddings for token and position
  24. 31 10.2 Multihead Attention 1つの頭より2つの頭 … … … … …

    … … … … 単 語 数 𝑛 埋め込み次元数𝑑 … 𝑑/ℎ ℎ : head数 … … … … … … … … … 単 語 数 𝑛 … … … … … … … … … … … … … … … … … … … … … … … … … … … headごとに異なる重みを学習する=表現力UP
  25. 32 10.2 Multihead Attention 1つの頭より2つの頭 𝑑/ℎ … … … …

    … … … … … 単 語 数 𝑛 … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … 𝑑/ℎ 𝑑#,%,& … … … … … … … … … … … … … … … … … … … … 𝑑/ℎ 𝑑/ℎ ℎ : head数 … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … 𝑊&! … … … … … … … … … … 𝑊'! … … … … … … … … … … 𝑊(! … … … … … ℎ : head数 各headに対して異なる𝑊& , 𝑊' , 𝑊( が存在する
  26. 33 10.2 Multihead Attention 1つの頭より2つの頭 𝑑/ℎ … … … …

    … … … … … 単 語 数 𝑛 … … … … … … … … … … 𝑑/ℎ 𝑑#,%,& … … … … … … … … … … … … … … … … … … … … 𝑑/ℎ 𝑑/ℎ 簡単のため、1つのheadに注目する 𝑋'
  27. 34 … … … … … … … … …

    … … … … … … … 10.2 Multihead Attention 1つの頭より2つの頭 𝑑/ℎ … … … … … … … … … 単 語 数 𝑛 … … … … … … … … … … 𝑑/ℎ 𝑑#,%,& … … … … … … … … … … … … … … … … … … … … 𝑑/ℎ 𝑑/ℎ 簡単のため、1つのheadに注目する … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … 𝑄) 𝐾) 𝑉) 単 語 数 𝑛 𝑑#,%,& (× ℎ) 𝑋'
  28. 35 10.2 Multihead Attention 1つの頭より2つの頭 最後に各headを結合して、それに重みWoをかける。 𝑑& … … …

    … … … … … … 単 語 数 𝑛 ⨁ … … … … … … … … … ⨁ … ℎ𝑒𝑎𝑑# ℎ𝑒𝑎𝑑) … … … … … … … … … … … … … … … … = 𝐴 ∈ ℝ1×2 ℎ𝑑& 𝑑
  29. 目次 i. introduction ii. 10.1 The Transformer: A Self-Attention Network

    i. 10.1.1 Transformers: the intuition ii. 10.2 Causual or backward-looking self attention iii. 10.1.3 Self-attention more formally iv. 10.1.4 Parallelizing self-attention using a single matrix X v. 10.1.5 Masking out the future iii. 10.2 Multi-Head Attention iv. 10.3 Transformer Blocks v. 10.4 The Residual Stream view of the Transformer Block vi. 10.5 The input: embeddings for token and position
  30. 39 10.3 Transformer Blocks Layer Normalize (レイヤー正規化) → 目的:隠れ層の値を特定の範囲内に保つことで学習性能を改善する 入出力は次元𝑑)

    の単一ベクトル𝑥 平均: 分散: 単一ベクトルの平均、分散を求める 求めた平均と分散で標準化する 学習可能なパラメータ𝛾と𝛽を導入する ※ 分母はtorchの実装では、 𝜎" + 𝜀となっていた
  31. 41 10.3 Transformer Blocks Residual connection (残差接続) 𝐹(𝑥) 𝑥 𝑥

    𝐻 𝑥 = 𝐹 𝑥 + 𝑥 概要 中間層を通らずに下位層から上位層に情報を伝達する なぜうまくいく? • 残差接続なしだとdF/dxが累積し続けて勾配消失する • 残差接続があると… • 誤差が減ることなく下層にそのまま伝わる • あとNNは非線形変換を学習するもの • → よって恒等変換は苦手 • → だからモデル構造に付けてあげる(みたいな?)
  32. 43 10.3 Transformer Blocks Feedforward Layer • 全結合層で構成された2層のネットワーク • →2つの重み行列

    • 重みは各位置で同じですが、パラメータは層ごとに 異なります?? • 隠れ層の活性化関数はReLU • 隠れ層の次元数は入力の次元数よりも大きくするこ とが一般的 • 例 : d = 512, d_ff = 2048
  33. 目次 i. introduction ii. 10.1 The Transformer: A Self-Attention Network

    i. 10.1.1 Transformers: the intuition ii. 10.2 Causual or backward-looking self attention iii. 10.1.3 Self-attention more formally iv. 10.1.4 Parallelizing self-attention using a single matrix X v. 10.1.5 Masking out the future iii. 10.2 Multi-Head Attention iv. 10.3 Transformer Blocks v. 10.4 The Residual Stream view of the Transformer Block vi. 10.5 The input: embeddings for token and position
  34. 45 10.4 The Residual Stream view of the Transformer Block

    視点を変えてみる • 前章ではTransformer Blockを𝑁×𝑑の行列の入力という視点から解説した。 • この章では個々のトークンベクトルを入力としてTransformer Blockをデータが通過す る時に、何が起こっているかを解説する。 … … … … … … … … … 単 語 数 𝑛 埋め込み次元数𝑑 … … … … … … … … … 単 語 数 𝑛 埋め込み次元数𝑑 前章 この章 ←attention!! 基本的にどの処理も単一のベクトルを引数に取るからね
  35. 46 10.4 The Residual Stream view of the Transformer Block

    視点を変えてみる xiからhiが生成される過程 今までは「MHA, FFNを通す」という表 現をしていたが、この図では「xiに異な る視点を追加していく」とも見れる Residual stream : 個々のトークンの流れを表す
  36. 47 10.4 The Residual Stream view of the Transformer Block

    視点を変えてみる 埋め込み表現の1要素からみたTransformer Blockの方程式 入力として受け取るすべての情報をみる
  37. 48 10.4 The Residual Stream view of the Transformer Block

    視点を変えてみる Token A の情報がToken Bに流れ込んでいる図
  38. 49 10.4 The Residual Stream view of the Transformer Block

    Pre Norm vs Post Norm この二つの方法があるらしい (メリットもデメリットもpdfに載ってなかった)
  39. 目次 i. introduction ii. 10.1 The Transformer: A Self-Attention Network

    i. 10.1.1 Transformers: the intuition ii. 10.2 Causual or backward-looking self attention iii. 10.1.3 Self-attention more formally iv. 10.1.4 Parallelizing self-attention using a single matrix X v. 10.1.5 Masking out the future iii. 10.2 Multi-Head Attention iv. 10.3 Transformer Blocks v. 10.4 The Residual Stream view of the Transformer Block vi. 10.5 The input: embeddings for token and position
  40. 51 10.5 The input: embeddings for token and position 入力Xの作り方

    … … … … … … … … … 単 語 数 𝑛 埋め込み次元数𝑑 最初からしれっといたこいつ、どうやって作られてるの?
  41. 52 10.5 The input: embeddings for token and position 入力Xの作り方

    “thanks for all the” step1. テキストをトークン化する step2. one-hotベクトルに変換する 1 2 3 4 5 6 7 |V| step3. one-hotベクトルに埋め込み行列を乗算する
  42. 53 10.5 The input: embeddings for token and position 入力Xの作り方

    step4. one-hotベクトルを重ねて計算を効率化 このままだと問題がある
  43. 54 10.5 The input: embeddings for token and position 入力Xの作り方

    問題点 : Attention機構はトークンの位置情報を考慮してくれない! • RNNはトークンを順にモデルに入力する → 位置情報がモデルに伝わっている • Transformerはトークンを並列に入力する → ,, 伝わっていない 位置情報をベクトルに付加してあげよう
  44. 55 10.5 The input: embeddings for token and position 入力Xの作り方

    単純な解決策 : 絶対位置埋め込み … … … … … … … … … … … … … … … … 単 語 数 𝑛 埋め込み次元数𝑑 1 1 1 1 … 1 1 1 1 2 2 2 2 … 2 2 2 2 3 3 3 3 … 3 3 3 3 4 4 4 4 … 4 4 4 4 … … … … … … … … … … … n n n n … n n n n 埋め込み次元数𝑑 𝑖単語目なら𝑖をそのベクトルの全ての要素に足す これも問題がある
  45. 56 10.5 The input: embeddings for token and position 入力Xの作り方

    問題点 : 入力のサイズが大きい場合、入力の最後の方の訓練例が少なくなる テキストが長くなると後ろの方のトークンの埋め込み情報が壊れるとかもある? 解決策 : sinとcosを用いて位置を表現する 本家Transformerの実装では 範囲外なため説明は省略 位置埋め込みはさまざまな方法が提案されている (例) • 相対位置埋め込み • Rotary Positional Embeddings (RoPE)