トランスフォーマーの理解

Pythonで学ぶ LLM 入門

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

トランスフォーマーとは?

  • ディープラーニングアーキテクチャ
  • テキストの処理・理解・生成
  • ほとんどのLLMで使用
  • 長いテキスト系列を並列処理

3種類のトランスフォーマーアーキテクチャの図:エンコーダーのみ、デコーダーのみ、エンコーダー・デコーダー

Pythonで学ぶ LLM 入門

トランスフォーマーのアーキテクチャ

3種類のトランスフォーマーアーキテクチャの図:エンコーダーのみ、デコーダーのみ、エンコーダー・デコーダー

  • Hugging Faceモデルカードでアーキテクチャの詳細を確認
Pythonで学ぶ LLM 入門

エンコーダーのみ

エンコーダーのみのアーキテクチャの図

  • 入力テキストの理解
  • 逐次出力なし
  • 主なタスク:
    • テキスト分類
    • 感情分析
    • 抽出的質問応答(抽出またはラベル付け)
  • BERTモデル
  • 例: "distilbert-base-uncased-distilled-squad"
Pythonで学ぶ LLM 入門

エンコーダーのみ

エンコーダーのみのアーキテクチャの図

llm = pipeline(model="bert-base-uncased")
print(llm.model)
BertForMaskedLM(
  (bert): ...
    )
    (encoder): BertEncoder(
      ...
print(llm.model.config)
BertConfig {
...
  "architectures": [
    "BertForMaskedLM"
...
Pythonで学ぶ LLM 入門

エンコーダーのみ

エンコーダーのみのアーキテクチャの図

print(llm.model.config.is_decoder)
False
  • 代替手段: llm.model.config.is_encoder_decoder
Pythonで学ぶ LLM 入門

デコーダーのみ

デコーダーのみのアーキテクチャの図

  • 出力に焦点を当てる
  • 主なタスク:
    • テキスト生成
    • 生成的質問応答(文または段落)
  • GPTモデル
  • 例: "gpt-5.4-mini"
Pythonで学ぶ LLM 入門

デコーダーのみ

デコーダーのみのアーキテクチャの図

llm = pipeline(model="gpt2")
print(llm.model.config)
GPT2Config {
...
  "architectures": [
    "GPT2LMHeadModel"
  ],
...
  "task_specific_params": {
    "text-generation": {
...
print(llm.model.config.is_decoder)
False
Pythonで学ぶ LLM 入門

エンコーダー・デコーダー

エンコーダー・デコーダーのアーキテクチャの図

  • 入力と出力を理解・処理する
  • 主なタスク:
    • 翻訳
    • 要約
  • T5、BARTモデル
Pythonで学ぶ LLM 入門

エンコーダー・デコーダー

エンコーダー・デコーダーのアーキテクチャの図

llm = pipeline(model="Helsinki-NLP/opus-mt-es-en")
print(llm.model)
MarianMTModel(
...
    (encoder): MarianEncoder(
...
    (decoder): MarianDecoder(
...
Pythonで学ぶ LLM 入門

エンコーダー・デコーダー

エンコーダー・デコーダーのアーキテクチャの図

print(llm.model.config)
MarianConfig {
...
  "decoder_attention_heads": 8,
...
  "encoder_attention_heads": 8,
...
  "is_encoder_decoder": true,
...
print(llm.model.config.is_encoder_decoder)
True
Pythonで学ぶ LLM 入門

では、練習しましょう!

Pythonで学ぶ LLM 入門

Preparing Video For Download...