https://github.com/nithinbekal/micrograd
A tiny autograd engine in ruby
https://github.com/nithinbekal/micrograd
machine-learning neural-networks ruby
Last synced: 4 months ago
JSON representation
A tiny autograd engine in ruby
- Host: GitHub
- URL: https://github.com/nithinbekal/micrograd
- Owner: nithinbekal
- License: mit
- Created: 2024-01-17T16:44:17.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-01-24T03:12:08.000Z (over 2 years ago)
- Last Synced: 2025-12-10T17:46:17.305Z (7 months ago)
- Topics: machine-learning, neural-networks, ruby
- Language: Ruby
- Homepage:
- Size: 32.2 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Micrograd
A tiny autograd engine. This is a Ruby implementation of [karpathy/micrograd](https://github.com/karpathy/micrograd). I created this while working through Andrej Karpathy's [Neural Networks: Zero To Hero](https://karpathy.ai/zero-to-hero.html) course.
## Installation
```
gem install micrograd
```
## Usage
Here are some of the operatinos available on `Value`.
```ruby
include Micrograd
a = Value.new(2.0)
b = Value.new(-3.0)
c = Value.new(10.0)
e = a * b
d = e + c
f = Value.new(-2.0)
l = d * f
# Walk through all the values and calculate gradients for them.
l.start_backward
```
Example of training a multi level perceptron (`MLP`):
```ruby
mlp = MLP.new(input_size: 3, layer_sizes: [4, 4, 1])
# These are the training inputs
inputs = [
[2.0, 3.0, -1.0],
[3.0, -1.0, 0.5],
[0.5, 1.0, 1.0],
[1.0, 1.0, -1.0],
]
# These are the outputs for each of the inputs above.
desired_outputs = [1.0, -1.0, -1.0, 1.0]
# Training loop
100.times do |n|
# forward pass
mlp_outputs = inputs.map { mlp.call(_1).first }
loss = desired_outputs.zip(mlp_outputs).sum { (_1 - _2) ** 2 }
# backward pass
mlp.parameters.each { _1.grad = 0.0 }
loss.start_backward
# update the params
mlp.parameters.each { _1.data -= _1.grad * 0.1 }
end
```
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/nithinbekal/micrograd. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/nithinbekal/micrograd/blob/main/CODE_OF_CONDUCT.md).
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the Micrograd project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nithinbekal/micrograd/blob/main/CODE_OF_CONDUCT.md).