liveBook · Manning

Few-shot learning

As explained in Learn Generative AI with PyTorch

A prompting approach that provides exactly one example to condition a large language model on the task.

Few-shot prompting guides a large language model by placing multiple task examples in the prompt before the new input. The examples clarify the expected pattern and can improve the relevance or accuracy of the response. section 16.2.3

Distinguish it from nearby prompting approaches:

  • Zero-shot prompting supplies an instruction without examples. section 16.2.3
  • One-shot prompting supplies one example, while few-shot prompting supplies multiple examples. section 16.2.3
  • Separators such as // or -> can divide each input from its expected output, provided the format stays consistent. section 16.2.3

Classifying sentiment

Provide labeled examples such as “The movie is awesome! // Positive” and “It is so bad! // Negative,” then add “How horrible the movie is! //”. The model can infer the pattern and return “Negative.” section 16.2.3

prompt = """
The movie is awesome! // Positive
It is so bad! // Negative
Wow, the movie was incredible! // Positive
How horrible the movie is! //
"""
res=llm.invoke(prompt)
print(res)

Run a compact sentiment-classification prompt with three labeled examples and an unlabeled review. section 16.2.3

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains prompting distinctions, formatting, and a sentiment example, but does not descend into model internals.
Examples
Demonstrates sentiment labels, movie reviews, consistent separators, and a runnable LLM invocation.
What it assumes
Assumes access to a large language model while teaching zero-, one-, and few-shot prompting from their basic definitions.
What you can do after
Apply it via a structured prompt to classify new text according to demonstrated examples.
How it connects
Connects few-shot prompting with one-shot prompting, zero-shot prompting, sentiment analysis, and large language models.

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 Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.