liveBook · Manning

Few-shot learning

As explained in Data Analysis with LLMs

Few-shot learning is an approach in which a model infers a task from a small number of examples specifying inputs and desired outputs.

Few-shot learning helps a language model infer a task from a small set of examples. Each example pairs an input with its desired output, giving the model a pattern to follow rather than relying only on a task description. section 1.3.1

Make the output easy to use

Suppose a model must detect cars in images. Instead of accepting a response such as “Yes, there is a car,” specify an output of “1” for a car and “0” otherwise. Consistent labels make later counting and processing easier. section 1.3.1

A practical few-shot prompt combines worked examples, instructions, and a new item. For sentiment classification, show a review with its reference label, then repeat the task for the review the model must classify. A Python helper can build this prompt by formatting each sample with its text and sentiment label, then appending the unlabeled review. section 9.5

[Sample Review]
Is the sentiment positive or negative?
Answer ("pos"/"neg"):[Sample Solution]
[Review to Classify]
Is the sentiment positive or negative?
Answer ("pos"/"neg"):

Use the prompt listing to see the compact structure: sample review, task instruction, sample answer, and a new review awaiting classification. section 9.5

A reliable few-shot setup typically follows these practices:

  • Choose examples that represent correctly solved versions of the task. section 9.11
  • Repeat the required output format so generated answers can be evaluated or processed consistently. section 1.3.1
  • Experiment with prompt variants and evaluate them on samples, since prompt design can affect performance and cost. section 9.11 section 9.5

How Data Analysis with LLMs covers it

How deep it goes
Explains example selection, prompt structure, Python construction, and evaluation, without discussing model internals.
Examples
Uses car detection with binary labels and movie-review sentiment classification with positive and negative labels.
What you can do after
Apply it by constructing example-based prompts with explicit instructions, labels, and output formats.
How it connects
Connects prompting, zero-shot learning, prompt engineering, classification, and fine-tuning.

Also explained in

Compare how 18 books explain few-shot learning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Analysis with LLMs. The section links point to the parts of the book it draws on.