Code Monkey home page Code Monkey logo

micrograd-rs's Introduction

Rust Micrograd with Compile-Time Safety

This project is a Rust-based implementation of the Micrograd engine, originally developed by Andrej Karpathy in Python. This rewrite not only transitions the dynamic capabilities of neural network operations to Rust but also enhances type safety with the use of const generics, ensuring that mismatches between the number of inputs and parameters are caught at compile time.

Features

  • Compile-Time Safety: Leveraging Rust's const generics to enforce correct dimensions for inputs and parameters, eliminating a common source of runtime errors.
  • Neural Network Fundamentals: Implements core neural network structures such as layers and multi-layer perceptrons, with operations like forward and backward propagation.
  • Custom Value and Operation Definitions: Includes a custom implementation of neural network operations, supporting basic arithmetic, power functions, and non-linear activations like tanh and ReLU.

Getting Started

Prerequisites

Ensure you have Rust installed on your machine. You can install Rust through rustup.

Installation

Clone this repository

Run the example neural network:

cargo run

Usage

This library can be used to define and train neural networks. Here is a basic example of how to use it:

// Define and train a simple MultiLayer Perceptron
let mut rng = rand::thread_rng();
let mlp = MultiLayerPerceptron::<3, 1, 1, 1>::new(&mut rng);

// Example training loop
for _ in 0..500 {
    let ys_pred = xs.iter().map(|x| mlp.forward(&x)).collect::<Vec<_>>();
    // Mean Squared Error loss calculation
    let loss = ys_pred.iter().enumerate().map(|(i, y_pred)| (y_pred[0].clone() - ys[i].clone()).pow(2.)).sum::<Value>();

    // L2 regularization
    loss = loss + mlp.weights().map(|w| w.clone().pow(2.)).sum::<Value>() * Value::new(0.01, "lambda");

    println!("Loss: {:.4}, Predictions: {:?}", loss.value(), ys_pred.iter().map(|y| y[0].value()).collect::<Vec<_>>()) * Value::new(1. / ys_pred.len() as f64, "n");

    loss.backward();
    mlp.nudge(0.01);
}

Acknowledgments

  • Original Micrograd implementation by Andrej Karpathy: micrograd
  • This Rust implementation was inspired by the simplicity and educational value of the original Python code.

Contributing

Contributions are welcome! Please feel free to submit pull requests or open issues to discuss potential improvements or fixes.

License

This project is licensed under the MIT License - see the LICENSE file for details.

micrograd-rs's People

Contributors

romanschejbal avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.