Fine-tuning con l'API Trainer
Addestrare un classificatore sui propri dati
Fine-tuning con l'API Trainer è una lezione NLP Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento NLP Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso NLP Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Why Fine-Tune
A pretrained model already knows language; fine-tuning nudges it to excel at your specific task using your labeled data.
Start From a Checkpoint
Load a model with a classification head sized to your labels. The base weights are reused, only the head starts fresh.
from transformers import AutoModelForSequenceClassification
m = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=2)Meet the Trainer
The Trainer API handles the whole training loop for you: batching, gradients, evaluation, and saving, all in one class.
Prepare Your Dataset
Tokenize your text and keep the labels. A Hugging Face Dataset object plugs straight into the Trainer.
ds = ds.map(lambda x: tok(x["text"], truncation=True), batched=True)Set Training Arguments
TrainingArguments collects every knob in one place: learning rate, batch size, epochs, and where to save checkpoints.
from transformers import TrainingArguments
args = TrainingArguments("out", num_train_epochs=3)Build the Trainer
Pass the model, args, and your datasets into the Trainer. Now everything it needs to learn lives in one object.
from transformers import Trainer
trainer = Trainer(model=m, args=args, train_dataset=train, eval_dataset=val)Launch Training
A single call runs the full loop. The Trainer steps through epochs and updates the weights as it learns.
trainer.train()Learning Rate Matters
Fine-tuning uses a small learning rate, often around 2e-5, so you adjust the pretrained weights gently instead of wrecking them.
Just a Few Epochs
Transformers usually need only two to four passes over the data. Too many epochs and the model starts to overfit.
Track Metrics
Give the Trainer a compute_metrics function and it reports accuracy or F1 each evaluation, so you watch progress live.
trainer = Trainer(..., compute_metrics=metric_fn)Use a Data Collator
A data collator pads each batch dynamically to its longest example, saving memory versus padding everything to one length.
Quick Check
Why does fine-tuning use a small learning rate?
Recap
Load a model with a fresh head, tokenize your data, set TrainingArguments, then let the Trainer run a few low-rate epochs. ✅
Domande Frequenti
La lezione «Fine-tuning con l'API Trainer» è gratuita?
Sì — il testo completo di «Fine-tuning con l'API Trainer» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso NLP Academy, passa a CoddyKit PRO. Il corso NLP Academy include 4 lezioni in totale.
Cosa imparerò in «Fine-tuning con l'API Trainer»?
Addestrare un classificatore sui propri dati Eserciti NLP Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare NLP Academy?
Non è richiesta alcuna esperienza precedente. NLP Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Fine-tuning con l'API Trainer»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione NLP Academy?
Sì. Ogni lezione NLP Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Panoramica della libreria Transformers
- Tokenizzare per i modelli Transformer
- Fine-tuning con l'API Trainer
- Valutare e salvare il modello