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 Hopfield and Deep Belief Network demos with visualization #46

Merged
merged 1 commit into from
Nov 13, 2024

Conversation

leonvanbokhorst
Copy link
Owner

@leonvanbokhorst leonvanbokhorst commented Nov 13, 2024

Initial implementation of Hopfield Network for pattern memory:

  • Basic pattern storage and retrieval
  • Noise-resistant pattern completion
  • Modern attention-like mechanism comparison
  • Visualization capabilities

TODO:

  • Add Deep Belief Network comparison
  • Implement MNIST dataset demonstration
  • Enhance visualization for larger patterns

Technical details:

  • Hebbian learning implementation
  • Asynchronous neuron updates
  • Binary threshold neurons (-1/1)
  • Energy minimization dynamics

Summary by Sourcery

Add demonstrations for Hopfield Network and Deep Belief Network (DBN) to showcase pattern recognition and memory capabilities. The Hopfield Network demo illustrates pattern storage, corruption, and recovery, while the DBN demo includes training on the MNIST dataset with comprehensive visualizations.

New Features:

  • Introduce a Hopfield Network demonstration for pattern recognition and memory, showcasing pattern storage, corruption, and recovery capabilities.
  • Implement a Deep Belief Network (DBN) demonstration on the MNIST dataset, including visualization of input reconstructions, weight matrices, and activation patterns.

Initial implementation of Hopfield Network for pattern memory:
- Basic pattern storage and retrieval
- Noise-resistant pattern completion
- Modern attention-like mechanism comparison
- Visualization capabilities

TODO:
- Add Deep Belief Network comparison
- Implement MNIST dataset demonstration
- Enhance visualization for larger patterns

Technical details:
- Hebbian learning implementation
- Asynchronous neuron updates
- Binary threshold neurons (-1/1)
- Energy minimization dynamics
Copy link
Contributor

sourcery-ai bot commented Nov 13, 2024

Reviewer's Guide by Sourcery

This PR introduces a proof-of-concept implementation of pattern recognition using Hopfield Networks and Deep Belief Networks (DBNs). The implementation focuses on demonstrating associative memory, pattern completion, and hierarchical feature learning through three main Python modules. The code includes comprehensive visualization capabilities and modern attention-like mechanisms.

Class diagram for Hopfield Network and Deep Belief Network

classDiagram
    class HopfieldNetwork {
        +int size
        +double[][] weights
        +train(List~double[]~ patterns)
        +update(double[] state, int max_iterations)
        +update_modern(double[] state, double beta)
    }
    class RBM {
        +int n_visible
        +int n_hidden
        +double learning_rate
        +double[][] weights
        +double[] visible_bias
        +double[] hidden_bias
        +sigmoid(double[] x)
        +sample_hidden(double[] visible)
        +sample_visible(double[] hidden)
    }
    class EnhancedDBN {
        +List~RBM~ rbm_layers
        +List~int~ layer_sizes
        +double learning_rate
        +pretrain(double[][] data, int epochs, int batch_size, boolean visualize)
        +_train_batch(RBM rbm, double[][] batch)
        +_visualize_training(RBM rbm, int layer_idx, int epoch, double[][] sample_batch)
        +save_visualization(String viz_type, int layer_idx, int epoch, String file_suffix)
    }
    class DBN {
        +List~RBM~ rbm_layers
        +pretrain(double[][] data, int epochs, int batch_size)
    }
    EnhancedDBN --> RBM
    DBN --> RBM
    RBM --> HopfieldNetwork
    HopfieldNetwork --> RBM
    note for HopfieldNetwork "Implements pattern storage and retrieval using associative memory"
    note for EnhancedDBN "Includes visualization and analysis capabilities"
    note for DBN "Implements a stack of RBMs for hierarchical feature learning"
Loading

File-Level Changes

Change Details Files
Implementation of a Hopfield Network for pattern memory and recognition
  • Implemented binary threshold neurons with -1/1 states
  • Added asynchronous neuron updates for pattern convergence
  • Created pattern storage using Hebbian learning
  • Added modern attention-like mechanism comparison
  • Implemented visualization for pattern recovery process
pocs/poc_hopfield_memory.py
Implementation of Deep Belief Network (DBN) for MNIST pattern recognition
  • Created RBM layers with contrastive divergence training
  • Implemented layer-wise pretraining mechanism
  • Added comprehensive visualization tools for network analysis
  • Implemented progress tracking and metrics collection
  • Added organized output management with timestamped directories
pocs/dbn_mnist_demo.py
Base implementation of Deep Belief Network core components
  • Implemented RBM with visible and hidden layers
  • Added mini-batch training support
  • Created greedy layer-wise pretraining mechanism
  • Added dimensionality reduction capabilities
pocs/deep_belief_network.py

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 feat(poc): add Hopfield Network pattern recognition demo @sourcery-ai Nov 13, 2024
@sourcery-ai sourcery-ai bot changed the title @sourcery-ai Implement Hopfield and Deep Belief Network demos with visualization Nov 13, 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:

  • Consider consolidating the two Deep Belief Network implementations (dbn_mnist_demo.py and deep_belief_network.py) into a single reusable module while preserving the visualization capabilities from dbn_mnist_demo.py
Here's what I looked at during the review
  • 🟡 General issues: 1 issue 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.

pocs/dbn_mnist_demo.py Show resolved Hide resolved
pocs/dbn_mnist_demo.py Show resolved Hide resolved
pocs/deep_belief_network.py Show resolved Hide resolved
@leonvanbokhorst leonvanbokhorst merged commit e02f7c8 into main Nov 13, 2024
1 check failed
@leonvanbokhorst leonvanbokhorst deleted the hopfield-dbn branch November 13, 2024 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant