๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #334

Few-Shot Learning

Few-shot learning means adapting a model to a new task or new classes from just a handful of examples โ€” sometimes as few as one or two per class โ€” a dramatic departure from the thousands or millions of examples standard supervised training assumes.

Two Distinct Ways Few-Shot Learning Happens

ApproachMechanismWeight Updates?
In-context learning (LLMs)Provide a few examples directly in the prompt; the model infers the task purely from that contextNone โ€” the model's weights never change
Classical few-shot learningA specialized architecture/training procedure (like prototypical networks) explicitly designed for learning from few examplesTypically yes, but a small, specialized update

Prototypical Networks โ€” A Classical Approach

\[ \mathbf{c}_k = \frac{1}{|S_k|}\sum_{(\mathbf{x}_i,y_i)\in S_k} f_\theta(\mathbf{x}_i) \]

For each class \(k\), compute a "prototype" \(\mathbf{c}_k\) โ€” simply the average embedding of that class's few available examples, using a shared, pretrained embedding function \(f_\theta\). A new query example is then classified by finding which prototype it's closest to (via a distance metric, similar to Vector Norms) โ€” no gradient-based fine-tuning is needed at all for the new classes themselves.

In-Context Learning โ€” How Modern LLMs Do This

prompt = """
Classify the sentiment as positive or negative.

Review: "Amazing product, exceeded expectations!"
Sentiment: positive

Review: "Terrible quality, broke after one day."
Sentiment: negative

Review: "Works great, very happy with this purchase."
Sentiment:"""
# No weight updates happen at all -- the model infers the task purely from
# the two examples shown in the prompt (this is exactly "2-shot" in-context learning)

This is a genuinely remarkable emergent capability from What Is an LLM? โ€” a sufficiently large pretrained model can infer a task's pattern purely from a few in-context demonstrations, with zero gradient updates, simply because next-token prediction at massive scale implicitly teaches this kind of pattern-completion behavior.

Code โ€” A Simple Prototypical Network

import torch

def classify_few_shot(query_embedding, support_embeddings, support_labels, num_classes):
    prototypes = torch.stack([
        support_embeddings[support_labels == k].mean(dim=0) for k in range(num_classes)
    ])
    distances = torch.cdist(query_embedding.unsqueeze(0), prototypes)
    return distances.argmin(dim=1)   # classify by nearest prototype -- no gradient updates needed

Common Mistakes

  • Confusing "few-shot" with "fine-tuning on a small dataset" โ€” few-shot learning specifically means adapting with only a handful of examples, often without any gradient updates at all (in-context learning), a meaningfully different setting from standard fine-tuning on, say, hundreds of examples.
  • Assuming in-context learning permanently changes a model โ€” it doesn't; the model reverts to its original behavior the moment the prompt's examples are removed, since no weights were ever updated.

Interview Relevance

Q: "How does an LLM perform 'few-shot learning' without any gradient updates?" Through in-context learning โ€” the model infers the task's pattern purely from a few example demonstrations placed directly in the prompt, using its pretrained next-token prediction capability to complete the pattern for a new query. No weights change; the model's parameters are entirely unaffected, and its adapted "behavior" exists only within that specific conversation/prompt context.

Practice Question

Why might prototypical networks struggle if the embedding function \(f_\theta\) wasn't already well-trained on data related to the new few-shot classes?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Few-Shot Learning โ€“ FAQs

Quick answers about learning Few-Shot Learning in Deep Learning.

This free note from CodingNow 2.0 explains Few-Shot Learning in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Few-Shot Learning, is 100% free with no signup required.
With focused practice, most students grasp Few-Shot Learning in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now