📖

Transformer とは?

Transformerは、自己注意機構を基盤としたニューラルネットワークアーキテクチャであり、現在の大規模言語モデルを支えています。その仕組みと重要性について学びましょう。

Transformerは、シーケンスデータ(最も有名なのは言語)を処理するために設計されたニューラルネットワークの一種で、入力内のすべての要素を同時に他のすべての要素と比較します。古い回帰型ネットワークのように厳密に左から右へ読み進めるのではなく、自己注意(self-attention)と呼ばれる仕組みを用いて、単語、トークン、位置が互いにどれほど重要であるかを、距離に関係なく学習します。この並列設計により、Transformerは最新のハードウェア上での学習が高速になり、長距離依存関係の捕捉が飛躍的に向上したため、現在ではほぼすべての最先端の大規模言語モデルを支える基盤となっています。

Transformerの仕組み

Transformerの中核には自己注意演算があります。すべての入力トークンは、クエリ(query)キー(key)バリュー(value)と呼ばれる3つのベクトルに射影されます。1つのトークンを理解するために、モデルは自身のクエリを他のすべてのトークンのキーと比較し、「それぞれにどれだけの注意を向けるべきか」を示す注意スコアの集合を生成します。これらのスコアは重みに正規化され、バリューベクトルの重み付き和がそのトークンの新しい表現となります。マルチヘッド注意はこのような比較を並行して複数回実行し、文法、相互参照、感情など、さまざまな種類の関係を同時に追跡できるようにします。

これらの注意ブロックを積み重ね、それぞれに小さなフィードフォワードネットワークと残差接続を付加することで、モデル全体が構成されます。注意機構自体は順序に依存しないため、入力には位置エンコーディングが追加され、ネットワークがトークンの順序を把握できるようにします。学習時には、デコーダー専用のTransformerがシーケンス内の次のトークンを予測します。十分なデータとパラメータがあれば、このシンプルな目的関数だけでも、GPTのようなシステムに見られる推論、翻訳、コード生成の能力が生まれます。

なぜ重要なのか

Transformer以前には、回帰型ニューラルネットワーク(RNN)やLSTMがテキストを1トークンずつ処理していたため、速度が遅く、長い文脈への対応に苦戦していました。Transformerの並列注意機構により、研究者はウェブ規模のコーパスで学習した数十億パラメータのモデルへのスケーリングを可能にし、现代のLLMの能力を解き放ちました。以来、同じアーキテクチャは画像(Vision Transformer)、音声、タンパク質、強化学習にも適用され、現在の深層学習の支配的なパラダイムとなっています。

主要な種類

  • エンコーダー専用Transformer — BERTなど、分類、検索ランキング、埋め込みなどの理解タスクに最適化されています。
  • デコーダー専用Transformer — GPTやLlamaなど、テキストを1トークンずつ生成することに最適化されています。
  • エンコーダー・デコーダーTransformer — オリジナルの「Attention Is All You Need」モデルやT5など、翻訳や系列変換タスクに使用されます。
  • Vision Transformer(ViT) — 単語の代わりに画像のパッチに対して自己注意を適用します。
  • Mixture-of-Experts(MoE)Transformer — 各トークンを「専門家」サブネットワークのサブセットにルーティングし、計算コストを比例的に増やさず容量を増やします。

2017年以来、TransformerはAI研究とプロダクトエンジニアリングの両方を変革し、HyperStoreカタログのほとんどのアプリ — チャットボット、コードアシスタント、画像生成AI、推論エージェント — はその何らかのバリアント上に構築されています。基礎設計については原著論文「Attention Is All You Need」を、ステップ・バイ・ステップの解説についてはIllustrated Transformerガイドをご覧ください。

Frequently Asked Questions

Who invented the Transformer architecture?
A team at Google Brain led by Vaswani et al. introduced the Transformer in the 2017 paper "Attention Is All You Need." Its eight authors — including Noam Shazeer, Jakob Uszkoreit, Llion Jones, and Aidan Gomez — showed that self-attention alone could match or beat recurrent and convolutional models on translation tasks while training far faster on GPUs.
What is the difference between a Transformer and an LLM?
A Transformer is the underlying neural network architecture; an LLM (large language model) is a specific application of it, trained on massive text datasets to generate and reason about language. In other words, every modern LLM is built from Transformer blocks, but not every Transformer is an LLM — vision and audio models use the same architecture too.
Why did Transformers replace RNNs and LSTMs?
Transformers process entire sequences in parallel rather than one token at a time, making them far more efficient to train on modern hardware. Their self-attention also captures relationships across long distances in a sequence — something RNNs and LSTMs struggled with due to vanishing gradients. The result is faster training, larger models, and noticeably better performance on language tasks.
What are the main limitations of Transformers?
Self-attention scales quadratically with sequence length, so very long contexts (tens of thousands of tokens) become expensive in both memory and compute. Transformers also require large amounts of training data, are opaque in how they reach decisions, and can hallucinate confident but incorrect outputs. Active research on sparse attention, state-space models, and retrieval augmentation aims to address these trade-offs.