liveBook · Manning

Zero-shot learning

As explained in Generative AI in Action

Zero-shot learning is the ability to perform a task or recognize classes not directly represented in task-specific training examples by relying on broader learned associations or descriptions.

Zero-shot learning lets a model perform a task without seeing task-specific examples during training. It relies on broader learned associations, such as semantic similarity, word embeddings, or shared representations, to generalize to a new task. section 2.5 section 4.4

Use zero-shot learning when the task can be expressed clearly without supplying examples:

  • Translate text into another language even when the model has not seen examples of that specific translation task. section 2.5 section 6.4.2
  • Classify images using natural-language class names, even when the model was not fine-tuned for those classes. section 4.4
  • Prefer few-shot learning when examples are available and task criteria or intent are difficult to convey through instructions alone. section 6.4.2
  • Avoid confusing inference-time conditioning with weight updates: few-shot and many-shot examples affect the current call transiently and may be cached to reduce repeated cost. section 6.4.2

Prompt-based translation

A zero-shot completion prompt can request translation directly: “Translate the following to Spanish: I have a small dog called Champ.” An Azure OpenAI completion call sends this instruction without supplying translation examples. section 6.4.2

Listing 6.3 Prompt engineering zero-shot example
import os
import openai

openai.api_type = "azure"
openai.api_base = os.getenv("AOAI_ENDPOINT")
openai.api_version = "2022-12-01"
openai.api_key = os.getenv("AOAI_KEY")

prompt_startphrase = "Translate the following to Spanish: 
                      I have a small dog called Champ."

response = openai.Completion.create(
  engine="gpt-35-turbo",
  prompt=prompt_startphrase,
  temperature=0.8,
  max_tokens=100,
  stop=None)

responsetext = response["choices"][0]["text"]

print("Prompt:" + prompt_startphrase + "\nResponse:" + responsetext)

Inspect the Python completion example for the prompt, Azure OpenAI configuration, model engine, temperature, and token limit. section 6.4.2

Table 2.1 Types of LLMs
LLM Type
Description
Base LLM
These are the original models, pretrained on a massive corpus of text data, and they can generate text based on the patterns they learned during this training. Some also call these raw language models or even refer to them as foundational models; they can be used out of the box to generate text. They learn powerful and general representations but lack specific expertise in a particular task. GPT-3’s DaVinci model is an example of a base LLM.
Instruction-based LLM
This involves using a base LLM and providing explicit instructions in prompt input. In many examples we saw in the previous chapter, we instructed the model to follow instructions, such as “Translate the following text to French:” or “Summarize this article:” Sometimes, these models are also called instruction-tuned LLMs.
Fine-tuned LLM
Fine-tuning involves taking the base LLM and training it further on a task it might perform poorly at, often in a specific domain. An example would be training the model on medical literature if we want it to understand medical topics or training it on customer service interactions if we want it to respond to customer inquiries for a specific industry. Fine- tuning can help make the model more accurate or helpful to particular tasks or domains, but it requires additional data and training time.

Compare zero-shot learning with base, instruction-based, and fine-tuned language-model approaches. section 2.5

How Generative AI in Action covers it

How deep it goes
Explains generalization mechanisms, contrasts few-shot and transfer learning, and demonstrates an API call without mathematical derivation or model internals.
Examples
Uses English-to-German translation, Spanish translation through Azure OpenAI, and CLIP image labeling from natural-language class names.
What it assumes
Assumes familiarity with language models and prompting while defining zero-shot, few-shot, many-shot, and transfer learning distinctions.
What you can do after
Apply it via an instruction prompt or CLIP-style text-image comparison when suitable task examples or fine-tuning resources are unavailable.
How it connects
Connects zero-shot learning to few-shot learning, transfer learning, prompt engineering, semantic similarity, word embeddings, and contrastive learning.

Also explained in

Compare how 10 books explain zero-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.