xbe.at

LLMs and generative AI / LLM architecture and theory

Many-Shot vs Few-Shot In-Context Learning with Python

9 min read, 14 sections

Contents
  1. Introduction to Many-Shot vs Few-Shot In-Context Learning
  2. Many-Shot In-Context Learning
  3. Few-Shot In-Context Learning
  4. Comparing Many-Shot and Few-Shot Performance
  5. Advantages of Many-Shot Learning
  6. Advantages of Few-Shot Learning
  7. Implementing Many-Shot Learning for Named Entity Recognition
  8. Implementing Few-Shot Learning for Named Entity Recognition
  9. Real-Life Example: Sentiment Analysis for Product Reviews
  10. Real-Life Example: Named Entity Recognition in News Headlines
  11. Choosing Between Many-Shot and Few-Shot Learning
  12. Hybrid Approaches: Combining Many-Shot and Few-Shot Learning
  13. Future Directions in In-Context Learning
  14. Additional Resources

Introduction to Many-Shot vs Few-Shot In-Context Learning

In-context learning (ICL) is a crucial aspect of Large Language Models (LLMs).

import numpy as np
import matplotlib.pyplot as plt

# Simulating performance of many-shot vs few-shot learning
shots = np.arange(1, 101)
many_shot = 1 - np.exp(-0.05 * shots)
few_shot = 1 - np.exp(-0.2 * shots)

plt.plot(shots, many_shot, label='Many-shot')
plt.plot(shots, few_shot, label='Few-shot')
plt.xlabel('Number of examples')
plt.ylabel('Performance')
plt.legend()
plt.title('Many-shot vs Few-shot Learning Curves')
plt.show()

Many-Shot In-Context Learning

Many-shot ICL involves providing the model with numerous examples to learn from. This approach typically leads to better performance but requires more data and computational resources.

def many_shot_sentiment_analysis(text, examples):
    prompt = "Classify the sentiment of the following text as positive or negative:\n\n"
    for example in examples:
        prompt += f"Text: {example['text']}\nSentiment: {example['sentiment']}\n\n"
    prompt += f"Text: {text}\nSentiment:"
    
    # In a real scenario, you would send this prompt to an LLM API
    # For demonstration, we'll use a simple rule-based classifier
    return "positive" if "good" in text.lower() or "great" in text.lower() else "negative"

# Example usage
examples = [
    {"text": "I love this product!", "sentiment": "positive"},
    {"text": "The service was terrible.", "sentiment": "negative"},
    {"text": "It's an amazing experience.", "sentiment": "positive"},
    # ... many more examples ...
]

result = many_shot_sentiment_analysis("The food was great!", examples)
print(f"Sentiment: {result}")

Few-Shot In-Context Learning

Few-shot ICL uses a limited number of examples, typically 2-5, to guide the model's understanding. This approach is more efficient in terms of prompt length and processing time but may sacrifice some accuracy.

def few_shot_sentiment_analysis(text, examples):
    prompt = "Classify the sentiment of the following text as positive or negative:\n\n"
    for example in examples[:3]:  # Using only 3 examples
        prompt += f"Text: {example['text']}\nSentiment: {example['sentiment']}\n\n"
    prompt += f"Text: {text}\nSentiment:"
    
    # In a real scenario, you would send this prompt to an LLM API
    # For demonstration, we'll use a simple rule-based classifier
    return "positive" if "good" in text.lower() or "great" in text.lower() else "negative"

# Example usage
examples = [
    {"text": "I love this product!", "sentiment": "positive"},
    {"text": "The service was terrible.", "sentiment": "negative"},
    {"text": "It's an average experience.", "sentiment": "neutral"},
]

result = few_shot_sentiment_analysis("The food was great!", examples)
print(f"Sentiment: {result}")

Comparing Many-Shot and Few-Shot Performance

Let's compare the performance of many-shot and few-shot approaches using a simple text classification task.

import random

def generate_data(n):
    words = ["good", "great", "excellent", "amazing", "bad", "terrible", "awful", "poor"]
    data = []
    for _ in range(n):
        sentiment = random.choice(["positive", "negative"])
        text = f"This product is {random.choice(words)}."
        data.append({"text": text, "sentiment": sentiment})
    return data

def evaluate_model(model, test_data):
    correct = 0
    for item in test_data:
        prediction = model(item["text"], train_data)
        if prediction == item["sentiment"]:
            correct += 1
    return correct / len(test_data)

# Generate data
train_data = generate_data(100)
test_data = generate_data(50)

# Evaluate models
many_shot_accuracy = evaluate_model(many_shot_sentiment_analysis, test_data)
few_shot_accuracy = evaluate_model(few_shot_sentiment_analysis, test_data)

print(f"Many-shot accuracy: {many_shot_accuracy:.2f}")
print(f"Few-shot accuracy: {few_shot_accuracy:.2f}")

Advantages of Many-Shot Learning

Many-shot learning often leads to better performance due to the wealth of examples provided. It allows the model to learn from a diverse set of scenarios, potentially improving generalization.

import numpy as np
import matplotlib.pyplot as plt

def performance_curve(n_examples, learning_rate):
    return 1 - np.exp(-learning_rate * n_examples)

n_examples = np.arange(1, 101)
many_shot = performance_curve(n_examples, 0.05)
few_shot = performance_curve(n_examples, 0.2)

plt.plot(n_examples, many_shot, label='Many-shot')
plt.plot(n_examples, few_shot, label='Few-shot')
plt.xlabel('Number of examples')
plt.ylabel('Performance')
plt.legend()
plt.title('Learning Curves: Many-shot vs Few-shot')
plt.show()

Advantages of Few-Shot Learning

Few-shot learning is more efficient in terms of prompt length and processing time. It's particularly useful when working with limited data or when quick adaptation to new tasks is required.

import time

def measure_processing_time(func, text, examples):
    start_time = time.time()
    result = func(text, examples)
    end_time = time.time()
    return end_time - start_time

text = "The product exceeded my expectations."
many_shot_time = measure_processing_time(many_shot_sentiment_analysis, text, examples)
few_shot_time = measure_processing_time(few_shot_sentiment_analysis, text, examples[:3])

print(f"Many-shot processing time: {many_shot_time:.6f} seconds")
print(f"Few-shot processing time: {few_shot_time:.6f} seconds")

Implementing Many-Shot Learning for Named Entity Recognition

Let's implement a many-shot learning approach for Named Entity Recognition (NER) using a simple rule-based system.

def many_shot_ner(text, examples):
    prompt = "Identify named entities (Person, Organization, Location) in the following text:\n\n"
    for example in examples:
        prompt += f"Text: {example['text']}\nEntities: {example['entities']}\n\n"
    prompt += f"Text: {text}\nEntities:"
    
    # In a real scenario, you would send this prompt to an LLM API
    # For demonstration, we'll use a simple rule-based system
    entities = []
    for word in text.split():
        if word[0].isupper():
            if word in ["Inc.", "Corp.", "LLC"]:
                entities.append((word, "Organization"))
            elif word in ["Street", "Avenue", "Road"]:
                entities.append((word, "Location"))
            else:
                entities.append((word, "Person"))
    return entities

# Example usage
examples = [
    {"text": "John works at Google in New York.", 
     "entities": [("John", "Person"), ("Google", "Organization"), ("New York", "Location")]},
    {"text": "Apple Inc. is headquartered in Cupertino.", 
     "entities": [("Apple Inc.", "Organization"), ("Cupertino", "Location")]},
    # ... many more examples ...
]

result = many_shot_ner("Microsoft Corp. was founded by Bill Gates in Redmond.", examples)
print(f"Entities: {result}")

Implementing Few-Shot Learning for Named Entity Recognition

Now, let's implement a few-shot learning approach for the same NER task.

def few_shot_ner(text, examples):
    prompt = "Identify named entities (Person, Organization, Location) in the following text:\n\n"
    for example in examples[:2]:  # Using only 2 examples
        prompt += f"Text: {example['text']}\nEntities: {example['entities']}\n\n"
    prompt += f"Text: {text}\nEntities:"
    
    # In a real scenario, you would send this prompt to an LLM API
    # For demonstration, we'll use a simple rule-based system
    entities = []
    for word in text.split():
        if word[0].isupper():
            if word in ["Inc.", "Corp.", "LLC"]:
                entities.append((word, "Organization"))
            elif word in ["Street", "Avenue", "Road"]:
                entities.append((word, "Location"))
            else:
                entities.append((word, "Person"))
    return entities

# Example usage
examples = [
    {"text": "John works at Google in New York.", 
     "entities": [("John", "Person"), ("Google", "Organization"), ("New York", "Location")]},
    {"text": "Apple Inc. is headquartered in Cupertino.", 
     "entities": [("Apple Inc.", "Organization"), ("Cupertino", "Location")]},
]

result = few_shot_ner("Microsoft Corp. was founded by Bill Gates in Redmond.", examples)
print(f"Entities: {result}")

Real-Life Example: Sentiment Analysis for Product Reviews

Let's apply many-shot and few-shot learning to analyze sentiment in product reviews.

import random

def generate_reviews(n):
    positive_words = ["great", "excellent", "amazing", "wonderful", "fantastic"]
    negative_words = ["terrible", "awful", "disappointing", "poor", "bad"]
    reviews = []
    for _ in range(n):
        sentiment = random.choice(["positive", "negative"])
        words = positive_words if sentiment == "positive" else negative_words
        review = f"This product is {random.choice(words)}. I {'would' if sentiment == 'positive' else 'would not'} recommend it."
        reviews.append({"text": review, "sentiment": sentiment})
    return reviews

# Generate example reviews
examples = generate_reviews(20)

# Test reviews
test_reviews = [
    "This gadget is fantastic! It works perfectly and saves me so much time.",
    "I'm very disappointed with this purchase. The quality is poor and it broke after a week."
]

for review in test_reviews:
    many_shot_result = many_shot_sentiment_analysis(review, examples)
    few_shot_result = few_shot_sentiment_analysis(review, examples[:3])
    print(f"Review: {review}")
    print(f"Many-shot sentiment: {many_shot_result}")
    print(f"Few-shot sentiment: {few_shot_result}\n")

Real-Life Example: Named Entity Recognition in News Headlines

Let's apply many-shot and few-shot learning to perform Named Entity Recognition on news headlines.

# Example headlines
headlines = [
    "Apple unveils new iPhone at annual event in California",
    "NASA successfully launches Mars rover from Cape Canaveral",
    "President Biden meets with German Chancellor in Washington D.C."
]

# Example data for training
examples = [
    {"text": "Amazon opens new office in Seattle", 
     "entities": [("Amazon", "Organization"), ("Seattle", "Location")]},
    {"text": "Elon Musk announces Tesla's expansion to Europe", 
     "entities": [("Elon Musk", "Person"), ("Tesla", "Organization"), ("Europe", "Location")]},
]

for headline in headlines:
    many_shot_entities = many_shot_ner(headline, examples)
    few_shot_entities = few_shot_ner(headline, examples)
    print(f"Headline: {headline}")
    print(f"Many-shot NER: {many_shot_entities}")
    print(f"Few-shot NER: {few_shot_entities}\n")

Choosing Between Many-Shot and Few-Shot Learning

The choice between many-shot and few-shot learning depends on various factors such as available data, computational resources, and the specific task at hand.

import matplotlib.pyplot as plt

factors = ['Data Availability', 'Computational Resources', 'Task Complexity', 'Adaptation Speed']
many_shot_scores = [0.9, 0.8, 0.9, 0.6]
few_shot_scores = [0.6, 0.9, 0.7, 0.9]

x = range(len(factors))
width = 0.35

fig, ax = plt.subplots(figsize=(10, 6))
ax.bar([i - width/2 for i in x], many_shot_scores, width, label='Many-shot')
ax.bar([i + width/2 for i in x], few_shot_scores, width, label='Few-shot')

ax.set_ylabel('Suitability Score')
ax.set_title('Many-shot vs Few-shot: Suitability for Different Factors')
ax.set_xticks(x)
ax.set_xticklabels(factors)
ax.legend()

plt.tight_layout()
plt.show()

Hybrid Approaches: Combining Many-Shot and Few-Shot Learning

In some cases, a hybrid approach combining elements of both many-shot and few-shot learning can be beneficial.

def hybrid_sentiment_analysis(text, many_shot_examples, few_shot_examples):
    # Use many-shot learning for initial classification
    many_shot_result = many_shot_sentiment_analysis(text, many_shot_examples)
    
    # Use few-shot learning for confidence check
    few_shot_result = few_shot_sentiment_analysis(text, few_shot_examples)
    
    # If results agree, return with high confidence
    if many_shot_result == few_shot_result:
        return many_shot_result, "High"
    else:
        # If results disagree, return many-shot result with low confidence
        return many_shot_result, "Low"

# Example usage
many_shot_examples = generate_reviews(50)
few_shot_examples = generate_reviews(5)

test_text = "This product exceeds expectations in every way. Highly recommended!"
result, confidence = hybrid_sentiment_analysis(test_text, many_shot_examples, few_shot_examples)

print(f"Text: {test_text}")
print(f"Sentiment: {result}")
print(f"Confidence: {confidence}")

Future Directions in In-Context Learning

As LLMs continue to evolve, we can expect advancements in both many-shot and few-shot learning techniques, potentially leading to more efficient and accurate models.

import numpy as np
import matplotlib.pyplot as plt

def future_performance(x, current_rate, improvement_rate):
    return 1 - np.exp(-(current_rate + improvement_rate * x))

x = np.linspace(0, 5, 100)
many_shot = future_performance(x, 0.05, 0.01)
few_shot = future_performance(x, 0.2, 0.05)

plt.figure(figsize=(10, 6))
plt.plot(x, many_shot, label='Many-shot')
plt.plot(x, few_shot, label='Few-shot')
plt.xlabel('Time (years)')
plt.ylabel('Performance')
plt.title('Projected Improvements in Many-shot and Few-shot Learning')
plt.legend()
plt.grid(True)
plt.show()

Additional Resources

For those interested in delving deeper into in-context learning in Large Language Models, here are some valuable resources:

  1. "In-context Learning and Induction Heads" by Burns et al. (2022) ArXiv: https://arxiv.org/abs/2209.11895
  2. "What Can Transformers Learn In-Context? A Case Study of Simple Function Classes" by Garg et al. (2022) ArXiv: https://arxiv.org/abs/2208.01066
  3. "Few-Shot Learning with Multilingual Language Models" by Nooralahzadeh et al. (2020) ArXiv: https://arxiv.org/abs/2112.10668
  4. "Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?" by Min et al. (2022) ArXiv: https://arxiv.org/abs/2202.12837

These papers provide in-depth analyses of various aspects of in-context learning, including its mechanisms, capabilities, and applications across different domains and languages. They offer valuable insights for researchers and practitioners working with LLMs and in-context learning techniques.

More in LLM architecture and theory

  1. mPLUG-Owl3 AI Vision and Understanding10 min

    mPLUG-Owl3 represents a significant advancement in AI's ability to comprehend visual information. This model extends beyond simple image recognition, aiming...

  2. LLM Initialization Techniques in Python4 min

    LLM initialization refers to the process of setting initial values for the model's parameters before training. Proper initialization is crucial for efficient...

  3. Positional Encodings in Transformer LLMs15 min

    Positional encodings form the backbone of modern transformer architectures, enabling models to understand sequential information. They inject...

  4. Large Language Models for Information Retrieval with Python5 min

    Large Language Models (LLMs) have revolutionized the field of natural language processing, enabling powerful text generation and understanding capabilities....

  5. Rigorous Unit Testing for LLM Outputs with DeepEval7 min

    DeepEval is a Python framework designed specifically for rigorous testing of Large Language Model outputs. It extends traditional unit testing paradigms to...

  6. Knowledge Graphs as Powerful Evaluation Tools for LLM11 min

    Knowledge graphs are powerful tools for organizing and representing complex information. When combined with Large Language Models (LLMs), they can...