liveBook · Manning

Few-shot learning

As explained in Generative AI in Action

Few-shot learning is the adaptation of a model to a task by providing a small number of task-specific examples at use time.

Few-shot learning adapts a model to a task by giving it a small number of examples and then asking it to handle a new input. For example, a sentiment classifier can use a few tweets paired with sentiment labels to infer how to label another tweet. section 2.5

In few-shot prompting, the examples are supplied in the prompt at inference time. They condition the response without updating model weights; the adaptation is transient rather than retraining. Zero-shot prompting supplies no task examples, while many-shot prompting supplies more, potentially tens to hundreds. section 6.3 section 6.4.2

Prompt for sentiment classification

Give the model a task and a few input-output pairs: “Label each tweet as positive or negative. Tweet: ‘The service was great.’ Sentiment: positive. Tweet: ‘My order arrived late.’ Sentiment: negative. Tweet: ‘The support team was helpful.’ Sentiment:” The model then predicts the label for the final tweet. This is an illustrative prompt, not a validated classifier. section 2.5 section 6.4.2

To make demonstrations useful:

  • Choose high-quality examples that show both inputs and desired outputs. Keep their format consistent and relevant to the task. section 6.4.2 section 6.4.4
  • Cover a variety of inputs and appropriate labels. Research findings emphasize the label space and distribution as well as individual mappings; this is not a reason to deliberately use incorrect labels. section 6.4.4
  • Test different example orders and prompts. Ordering can affect output, and an order that works well for one model may work poorly for another. section 6.8 section 6.9

Few-shot examples often improve performance over zero-shot prompts, but responses can still be inconsistent or prompts overfitted. Repeatedly supplying the same examples can add cost; consider saving or caching them where appropriate. section 6.4.2 section 6.9

How Generative AI in Action covers it

How deep it goes
Explains inference-time conditioning and demonstration design, while noting that the reason in-context learning works is not fully known.
Examples
Illustrates sentiment labeling, Celsius-to-Fahrenheit conversion, and defining invented words from demonstrations.
What you can do after
Apply few-shot prompting by supplying input-output demonstrations and testing their selection and order.
How it connects
Connects few-shot learning with in-context learning, prompt engineering, zero-shot learning, and many-shot learning.

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