Hugging Faceモデルをファインチューニングする
事前学習済みTransformerを自分のデータに適応させます
「Hugging Faceモデルをファインチューニングする」はCoddyKit上の無料Deep Learning Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDeep Learning Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Deep Learning Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
A Hub of Pretrained Models
Hugging Face hosts thousands of ready-to-use transformers for text, vision, and audio. You can fine-tune one on your own data in minutes. 🤗
Install Transformers
The transformers library gives you models, tokenizers, and training tools in one package.
pip install transformers datasetsLoad a Tokenizer
Text must become numbers first. The tokenizer that ships with each model knows exactly how to split and encode your text.
from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained('bert-base-uncased')Load a Model With a New Head
Pick a class for your task. AutoModelForSequenceClassification loads the backbone and attaches a fresh classifier head for your labels.
from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=2)Tokenize Your Dataset
Run every example through the tokenizer with padding and truncation so all inputs share the same length.
def encode(b):
return tok(b['text'], truncation=True, padding='max_length')TrainingArguments
TrainingArguments bundles your hyperparameters: learning rate, batch size, epochs, and where to save checkpoints.
from transformers import TrainingArguments
args = TrainingArguments(output_dir='out', learning_rate=2e-5, num_train_epochs=3)Keep That Rate Low
Notice the rate is just 2e-5. As with any fine-tune, a small learning rate protects the pretrained weights from being wrecked.
The Trainer
The Trainer class wraps the whole loop: forward pass, loss, backward, step, and evaluation. You skip writing it by hand.
from transformers import Trainer
trainer = Trainer(model=model, args=args, train_dataset=train_ds)Train in One Call
Once everything is wired up, kick off fine-tuning with a single train call and watch the loss fall.
trainer.train()Evaluate and Predict
After training, call evaluate for metrics on held-out data, then use predict to score brand-new examples.
trainer.evaluate()Save and Share
Persist your tuned model with save_pretrained, then reload it anywhere or push it to the Hub for others to use.
model.save_pretrained('my-classifier')Quick Check
Which Hugging Face class runs the full training loop for you so you don't write it by hand?
Recap
You tokenized data, loaded a model with a fresh head, set a low rate in TrainingArguments, and fine-tuned it with the Trainer. 🎯
よくある質問
「Hugging Faceモデルをファインチューニングする」レッスンは無料ですか?
はい。「Hugging Faceモデルをファインチューニングする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Deep Learning Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Deep Learning Academyコースには全4レッスンが含まれています。
「Hugging Faceモデルをファインチューニングする」で何を学びますか?
事前学習済みTransformerを自分のデータに適応させます ブラウザで直接実行するハンズオンコードでDeep Learning Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Deep Learning Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDeep Learning Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「Hugging Faceモデルをファインチューニングする」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDeep Learning Academyレッスンでコードを書いて実行できますか?
はい。すべてのDeep Learning Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- バックボーンを固定してヘッドを学習する
- 低い学習率でファインチューニングする
- レイヤーごとに変える学習率
- Hugging Faceモデルをファインチューニングする