0Pricing
NLP Academy · Lezione

Tokenizzare per i modelli Transformer

Subword, padding e maschere di attention

Tokenizzare per i modelli Transformer è una lezione NLP Academy gratuita su CoddyKit. Questa è la lezione 2 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.

Tokens Come First

Before a transformer can learn anything, your text must become numbers. That conversion job belongs to the tokenizer.

Match the Model

Always load the tokenizer that was trained with your model. A mismatched vocabulary produces garbage ids the model never saw.

from transformers import AutoTokenizer
tok = AutoTokenizer.from_pretrained("bert-base-uncased")

Subword Pieces

Modern tokenizers split rare words into smaller chunks called subwords, so even unknown text stays representable.

print(tok.tokenize("tokenization"))

Why Subwords Win

Subwords keep the vocabulary small while still handling typos and new words. The model rarely meets a true unknown token.

From Tokens to Ids

Each subword maps to an integer id. Calling the tokenizer on text returns those ids ready for the model.

enc = tok("Fine-tuning is fun")
print(enc["input_ids"])

Special Tokens

Tokenizers add markers like CLS and SEP so the model knows where a sequence starts and ends. These are the special tokens.

Padding to Equal Length

Batches need same-length rows, so shorter texts get filler tokens. This step is called padding.

tok(texts, padding=True)

Truncation for Long Text

Models cap input length, so very long text is cut to fit. Enabling truncation keeps every example within the limit.

tok(texts, truncation=True, max_length=128)

The Attention Mask

An attention mask marks real tokens as 1 and padding as 0, so the model ignores the filler positions.

print(enc["attention_mask"])

Return Tensors

Ask the tokenizer for framework tensors directly so the output drops straight into training without extra conversion.

tok("hello", return_tensors="pt")

Decode Back to Text

To read predictions, run ids through decode and the tokenizer rebuilds the original text, special tokens stripped.

print(tok.decode(enc["input_ids"]))

Quick Check

What is the job of the attention mask during batching?

Recap

Tokenizers turn text into subword ids, add special tokens, then handle padding, truncation, and the attention mask for clean batches. ✅

Domande Frequenti

La lezione «Tokenizzare per i modelli Transformer» è gratuita?

Sì — il testo completo di «Tokenizzare per i modelli Transformer» è 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 «Tokenizzare per i modelli Transformer»?

Subword, padding e maschere di attention 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 2 di 4.

Quanto tempo richiede la lezione «Tokenizzare per i modelli Transformer»?

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

  1. Panoramica della libreria Transformers
  2. Tokenizzare per i modelli Transformer
  3. Fine-tuning con l'API Trainer
  4. Valutare e salvare il modello
← Torna a NLP Academy