Key terms for this tutorial
Here are the words that matter most in this tutorial. Come back here if a term feels fuzzy.
- inference
- Using a trained model to make predictions on new inputs.
- language model
- A model that predicts the next token (or fills in missing tokens) in a sequence of text.
- model
- A function with learnable parameters that maps inputs to outputs.
- overfitting
- When a model memorizes training data and fails on new data.
- token
- A unit of text (word, sub-word, or character) that a language model processes.
- tokenizer
- A tool that splits text into tokens and maps them to integer IDs.
- transformer
- An architecture that uses attention to process sequences in parallel instead of one step at a time.
- BERT
- A Transformer model pre-trained to predict masked words and understand sentence relationships.
Tokenisers and encoders
Hugging Face tokenisers map text to ids and back. They handle padding, truncation, and special tokens consistently across datasets.
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
enc = tokenizer('Hello world', return_tensors='pt')
print('input ids:', enc['input_ids'])
print('tokens:', tokenizer.convert_ids_to_tokens(enc['input_ids'][0]))
input ids: tensor([[ 101, 7592, 2088, 102]]) tokens: ['[CLS]', 'hello', 'world', '[SEP]']
Line-by-line: tokenize.py
Read this slowly. Each line does one small, concrete thing.
from transformers import AutoTokenizer— Import tools from thetransformerspackage.tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')— Load a pre-trained tokenizer and model from the Hugging Face Hub.print('input ids:', enc['input_ids'])— Print a result so we can see what the model learned.print('tokens:', tokenizer.convert_ids_to_tokens(enc['input_ids'][0]))— Print a result so we can see what the model learned.
Pop-in: Pre-trained models are cheat sheets
You do not have to teach the model English from scratch. You download a model that already knows English, then fine-tune it for your exact task.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Modern NLP with Hugging Face'. Explain the section 'Tokenisers and encoders' in plain English. Include simple analogies, vocabulary definitions, and a short example. Also walk through the code example in this section step by step.
Fine-tune BERT for classification
Fine-tuning attaches a new classifier head on top of the [CLS] token and updates the whole model or just the head. For small datasets, only updating the head reduces overfitting.
from transformers import AutoModelForSequenceClassification
model = AutoModelForSequenceClassification.from_pretrained(
'bert-base-uncased', num_labels=2
)
print('num labels:', model.classifier.out_features)
num labels: 2
Line-by-line: bert_finetune.py
Read this slowly. Each line does one small, concrete thing.
from transformers import AutoModelForSequenceClassification— Import tools from thetransformerspackage.model = AutoModelForSequenceClassification.from_pretrained(— Load a pre-trained tokenizer and model from the Hugging Face Hub.print('num labels:', model.classifier.out_features)— Print a result so we can see what the model learned.
Pop-in: Pre-trained models are cheat sheets
You do not have to teach the model English from scratch. You download a model that already knows English, then fine-tune it for your exact task.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Modern NLP with Hugging Face'. Explain the section 'Fine-tune BERT for classification' in plain English. Include simple analogies, vocabulary definitions, and a short example. Also walk through the code example in this section step by step.
Generate with a causal model
Causal language models predict the next token autoregressively. Use generate with sampling strategies like temperature and top-k to control creativity.
from transformers import AutoModelForCausalLM, AutoTokenizer
tok = AutoTokenizer.from_pretrained('gpt2')
model = AutoModelForCausalLM.from_pretrained('gpt2')
inputs = tok('Once upon a time', return_tensors='pt')
outputs = model.generate(
**inputs, max_new_tokens=10, do_sample=True, temperature=0.8, top_k=50
)
print(tok.decode(outputs[0]))
Once upon a time, there was a little girl
Line-by-line: generate.py
Read this slowly. Each line does one small, concrete thing.
from transformers import AutoModelForCausalLM, AutoTokenizer— Import tools from thetransformerspackage.tok = AutoTokenizer.from_pretrained('gpt2')— Load a pre-trained tokenizer and model from the Hugging Face Hub.model = AutoModelForCausalLM.from_pretrained('gpt2')— Load a pre-trained tokenizer and model from the Hugging Face Hub.outputs = model.generate(— Generate new tokens from the model, one at a time.print(tok.decode(outputs[0]))— Print a result so we can see what the model learned.
Pop-in: Pre-trained models are cheat sheets
You do not have to teach the model English from scratch. You download a model that already knows English, then fine-tune it for your exact task.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Modern NLP with Hugging Face'. Explain the section 'Generate with a causal model' in plain English. Include simple analogies, vocabulary definitions, and a short example. Also walk through the code example in this section step by step.
Check yourself
Tokenizer vs model
Why do you need a tokenizer with a transformer?
Transformers operate on integer token ids. The tokenizer converts text into ids and special tokens, and decodes ids back to text.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Modern NLP with Hugging Face'. The practice question 'Tokenizer vs model' asks: Why do you need a tokenizer with a transformer? The answer is: Transformers operate on integer token ids. The tokenizer converts text into ids and special tokens, and decodes ids back to text.. Explain the concept behind this in simple terms.
Stuck or curious? Explain this to me on ChatGPT
Prompt: I am a beginner AI engineer working through the CodeHS tutorial 'AI Engineer: Modern NLP with Hugging Face'. Explain the section 'Check yourself' in plain English. Include simple analogies, vocabulary definitions, and a short example.
Three questions
1. What is BERT's [CLS] token used for?
Stuck or curious? Explain this question to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Modern NLP with Hugging Face'. The quiz asks: What is BERT's [CLS] token used for? The correct answer is: A pooled sentence representation for classification. Explain why that answer is right and why the other choices are wrong, using a simple example.
2. What does do_sample=True do during generation?
Stuck or curious? Explain this question to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Modern NLP with Hugging Face'. The quiz asks: What does do_sample=True do during generation? The correct answer is: Samples the next token from the probability distribution instead of greedily. Explain why that answer is right and why the other choices are wrong, using a simple example.
3. Why might you freeze BERT layers?
Stuck or curious? Explain this question to me on ChatGPT
Prompt: I am a beginner AI engineer studying 'AI Engineer: Modern NLP with Hugging Face'. The quiz asks: Why might you freeze BERT layers? The correct answer is: To avoid overfitting when you have little data. Explain why that answer is right and why the other choices are wrong, using a simple example.
Keep learning
Next: train a small LLM from scratch.
Part of the CodeHS tutorial library.