AutoModelとTokenizer

Hugging Faceを使いこなす

Jacob H. Marquez

Lead Data Engineer

パイプライン:早くて簡単

from transformers import pipeline  

my_pipeline = pipeline(
    "text-classification",
    model="distilbert-base-uncased-finetuned-sst-2-english"))

print(my_pipeline("Wi-Fi is slower than a snail today!"))
[{'label': 'NEGATIVE', 'score': 0.99}]
Hugging Faceを使いこなす

AutoClass:柔軟で強力

$$

  • AutoClass: モデルとトークナイザーを柔軟に読み込める
  • モデルの動作と出力をより細かく制御
  • 高度なタスクに向いている

$$

  • パイプライン = 速い、AutoClass = 柔軟

3つのスライダーバーとトグル、そしてそれらの1つを調整する手。 より多くの制御を表現。

Hugging Faceを使いこなす

AutoModels

  • AutoModelクラスを選び、モデルを直接ダウンロードする

$$

from transformers import AutoModelForSequenceClassification

# Download a pre-trained text classification model model = AutoModelForSequenceClassification.from_pretrained( "distilbert-base-uncased-finetuned-sst-2-english" )
Hugging Faceを使いこなす

AutoTokenizers

  • テキスト入力データを準備する
  • モデルに対応している特定のトークナイザーを使う

$$

from transformers import AutoTokenizer


# Retrieve the tokenizer paired with the model tokenizer = AutoTokenizer.from_pretrained( "distilbert-base-uncased-finetuned-sst-2-english" )
Hugging Faceを使いこなす

AutoTokenizerを使用したテキストのトークン化

  • トークナイザーは入力をクリーニングし、テキストを分割してトークン化する

$$

tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")

# Tokenize input text tokens = tokenizer.tokenize("AI: Helping robots think and humans overthink:)") print(tokens)
['ai', ':', 'helping', 'robots', 'think', 'and', 
 'humans', 'over', '##thi', '##nk', ':', ')']
Hugging Faceを使いこなす

モデルが変わるとトークナイザーも変わる

  • 今回利用したモデル(distilbert-base-uncased):

    ['ai', ':', 'helping', 'robots', 'think', 'and', 'humans', 'over', '##thi',
    '##nk', ':', ')']
    
  • BERT-Base-Casedのトークナイザー:

    ['AI', ':', 'Help', '##ing', 'robots', 'think', 'and', 'humans', 'over',
    '##thin', '##k', ':', ')']
    
Hugging Faceを使いこなす

AutoClassを使ったパイプラインの構築

from transformers import AutoModelForSequenceClassification,
AutoTokenizer, pipeline

# Download the model and tokenizer my_model = AutoModelForSequenceClassification.from_pretrained( "distilbert-base-uncased-finetuned-sst-2-english") my_tokenizer = AutoTokenizer.from_pretrained( "distilbert-base-uncased-finetuned-sst-2-english")
# Create the custom pipeline my_pipeline = pipeline( task="sentiment-analysis", model=my_model, tokenizer=my_tokenizer)
Hugging Faceを使いこなす

AutoModelsとAutoTokenizersの使用例

$$

  • 🔧 より細かい制御とカスタマイズに使用

  • 📝 テキスト前処理: 特定の用途向けにクリーニングしてトークン化する

  • 🏆 しきい値処理: 分類タスクで特定のカテゴリを優先する
  • 🚀 複雑なワークフロー: 複数ステップの処理と統合を制御

$$ より多くの制御とカスタマイズ

Hugging Faceを使いこなす

練習しましょう!

Hugging Faceを使いこなす

Preparing Video For Download...