Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MAML for adaptive regression with documentation and dependencies #34

Merged
merged 7 commits into from
Nov 8, 2024

Conversation

leonvanbokhorst
Copy link
Owner

@leonvanbokhorst leonvanbokhorst commented Nov 8, 2024

Added documentation for the model_agnostic_meta_learning.py proof of concept,
highlighting its key features including:

  • Meta-learning architecture with skip connections
  • Task generation and adaptation capabilities
  • Visualization and analysis tools
  • Rapid context adaptation functionality

Summary by Sourcery

Introduce a Model-Agnostic Meta-Learning (MAML) implementation for rapid adaptation in narrative modeling, complete with documentation and necessary dependencies.

New Features:

  • Implement a Model-Agnostic Meta-Learning (MAML) approach for rapid adaptation of narrative models to new contexts, featuring a meta-learning architecture with skip connections and adaptive learning rate scheduling.

Build:

  • Update requirements.txt to include wandb and matplotlib for experiment tracking and visualization.

Documentation:

  • Add detailed documentation for the model_agnostic_meta_learning.py proof of concept, highlighting its meta-learning architecture, task generation, and visualization tools.

…s designed to learn how to quickly adapt to new tasks with minimal training data. Here's what it aims to achieve:

Meta-Learning Purpose:
Learn a good initialization point for neural networks
Enable fast adaptation to new, unseen tasks
Require minimal fine-tuning data for new tasks
How it Works:
Trains on a collection of related but different tasks
Uses a nested optimization loop:
Inner loop: Simulates adaptation to specific tasks
Outer loop: Updates the meta-model to find better initial parameters
Practical Applications:
Few-shot learning problems
Quick adaptation to new environments
Transfer learning with minimal data
Personalization of models with limited user data
Example Use Cases:
Regression tasks with different functions
Classification with few examples per class
Robotics with different environments
Personalized recommendations with limited user interaction
…k generation

This commit introduces a complete MAML implementation with the following features:
- Bi-level optimization for meta-learning
- Synthetic task generation with controlled complexity
- Gradient clipping and skip connections for stability
- Learning rate scheduling and early stopping
- Comprehensive type hints and documentation

Technical details:
- Multi-component non-linear task generation
- Adaptive noise scaling for robustness
- Higher-order gradients for meta-optimization
- Task-specific fast weight adaptation
- Efficient batch processing with DataLoader

Breaking changes: None
Related issues: None
Added documentation for the model_agnostic_meta_learning.py proof of concept,
highlighting its key features including:
- Meta-learning architecture with skip connections
- Task generation and adaptation capabilities
- Visualization and analysis tools
- Rapid context adaptation functionality
Copy link
Contributor

sourcery-ai bot commented Nov 8, 2024

Reviewer's Guide by Sourcery

This PR implements a Model-Agnostic Meta-Learning (MAML) system with a focus on rapid adaptation of narrative models. The implementation features a neural network architecture with skip connections, comprehensive visualization tools, and robust training components. The code is well-structured with detailed documentation and includes both the core MAML implementation and supporting utilities for task generation and analysis.

Sequence diagram for meta-training step

sequenceDiagram
    participant MetaModelGenerator
    participant TaskBatch
    participant Device
    participant FastWeights
    participant MetaOptimizer
    participant Scheduler

    MetaModelGenerator->>TaskBatch: Get task batch
    loop for each task in task_batch
        MetaModelGenerator->>FastWeights: Clone parameters
        loop Inner loop steps
            FastWeights->>MetaModelGenerator: forward_with_fast_weights(support_x)
            MetaModelGenerator->>FastWeights: Compute gradients
            FastWeights->>FastWeights: Update fast weights
        end
        FastWeights->>MetaModelGenerator: forward_with_fast_weights(query_x)
        MetaModelGenerator->>MetaOptimizer: Accumulate meta-gradients
    end
    MetaOptimizer->>MetaModelGenerator: Apply meta-update
    MetaModelGenerator->>Scheduler: Step with avg_loss
Loading

Class diagram for MetaModelGenerator

classDiagram
    class MetaModelGenerator {
        +int input_size
        +List~int~ hidden_sizes
        +int output_size
        +float inner_lr
        +float meta_lr
        +Linear input_layer
        +ModuleList hidden_layers
        +Linear output_layer
        +SGD meta_optimizer
        +ReduceLROnPlateau scheduler
        +forward(torch.Tensor) torch.Tensor
        +meta_train_step(List~Tuple~torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor~~, torch.device) Tuple~float, float~
        +forward_with_fast_weights(torch.Tensor, Dict~str, torch.Tensor~) torch.Tensor
        +compute_metrics(torch.Tensor, torch.Tensor) Dict~str, float~
        +visualize_adaptation(torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, Dict~str, torch.Tensor~, str) void
    }
Loading

File-Level Changes

Change Details Files
Implemented core MAML architecture with skip connections and adaptive learning
  • Created MetaModelGenerator class with skip connections and multi-layer architecture
  • Implemented bi-level optimization with inner/outer training loops
  • Added adaptive learning rate scheduling and gradient clipping
  • Implemented forward propagation with fast weights for quick adaptation
src/maml_model_agnostic_meta_learning.py
Added comprehensive task generation and evaluation system
  • Created synthetic task generation with controlled complexity
  • Implemented support/query set splitting for evaluation
  • Added task difficulty analysis functionality
  • Created task-specific DataLoader implementation
src/maml_model_agnostic_meta_learning.py
Implemented visualization and analysis tools
  • Added feature importance visualization
  • Created adaptation progress tracking plots
  • Implemented error distribution analysis
  • Added comprehensive metrics computation
src/maml_model_agnostic_meta_learning.py
src/maml.md
Added project documentation and dependencies
  • Added detailed MAML documentation with implementation details
  • Updated project dependencies to include visualization libraries
  • Added README section describing MAML implementation
README.md
requirements.txt
src/maml.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@leonvanbokhorst leonvanbokhorst changed the title Model-Agnostic-Meta-Learning @sourcery-ai Nov 8, 2024
@sourcery-ai sourcery-ai bot changed the title @sourcery-ai Implement MAML for adaptive regression with documentation and dependencies Nov 8, 2024
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @leonvanbokhorst - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The extracted visualization helper methods need cleanup - there are duplicate methods with TODO comments that should be properly refactored into a single well-named utility function.
Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

…k generation

This commit adds the implementation of model-agnostic meta-learning (MAML) with synthetic task generation. The code changes include the addition of a sequence diagram that illustrates the process of generating meta-models, task batches, and performing fast weight updates. This implementation allows the system to quickly adapt to new narrative contexts with minimal data, making it valuable for modeling emerging story dynamics.
@leonvanbokhorst leonvanbokhorst self-assigned this Nov 8, 2024
@leonvanbokhorst leonvanbokhorst added documentation Improvements or additions to documentation enhancement New feature or request labels Nov 8, 2024
@leonvanbokhorst leonvanbokhorst merged commit 971b4fa into main Nov 8, 2024
1 check passed
@leonvanbokhorst leonvanbokhorst deleted the Model-Agnostic-Meta-Learning branch November 8, 2024 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant