dAImond 💎

Deep Learning framework for Ruby, inspired by PyTorch. Written from scratch with love for the Ruby community.

Ruby License: MIT

Why Ruby? IDK, i just love this lang. dAImond brings back the happiness to ML.

Features

  • 🔥 Automatic Differentiation - Full autograd engine with computational graphs
  • 🧠 Neural Networks - Linear layers, activations (ReLU, Sigmoid, Softmax, Tanh)
  • 📊 Optimizers - SGD with momentum, learning rate scheduling
  • 🎯 Loss Functions - MSE, CrossEntropy
  • 💾 Model Serialization - Save/load trained models with Marshal
  • 📈 Data Loaders - Batch processing, shuffling, MNIST support
  • Fast Backend - Numo::NArray for vectorized operations (C-speed)
  • 🎨 Beautiful API - Idiomatic Ruby DSL, chainable methods

Installation

Add this line to your Gemfile:

gem 'daimond'

Or install manually:

gem install daimond

Dependencies: Ruby 2.7+, numo-narray

Quick Start

require 'daimond'

# Define your model
class NeuralNet < Daimond::NN::Module
  attr_reader :fc1, :fc2
  
  def initialize
    super()
    @fc1 = Daimond::NN::Linear.new(784, 128)
    @fc2 = Daimond::NN::Linear.new(128, 10)
    @parameters = @fc1.parameters + @fc2.parameters
  end
  
  def forward(x)
    x = @fc1.forward(x).relu
    @fc2.forward(x).softmax
  end
end

# Training loop
model = NeuralNet.new
optimizer = Daimond::Optim::SGD.new(model.parameters, lr: 0.1, momentum: 0.9)
criterion = Daimond::Loss::CrossEntropyLoss.new

# Forward → Backward → Update
loss = criterion.call(model.forward(input), target)
optimizer.zero_grad
loss.backward!
optimizer.step

MNIST Example (97% Accuracy!)

Train a classifier on 60,000 handwritten digits:

ruby examples/mnist.rb

Results:

Epoch 1/5: Loss = 0.2898, Accuracy = 91.35%
Epoch 2/5: Loss = 0.1638, Accuracy = 95.31%
Epoch 3/5: Loss = 0.1389, Accuracy = 96.2%
Epoch 4/5: Loss = 0.1195, Accuracy = 96.68%
Epoch 5/5: Loss = 0.1083, Accuracy = 97.12%

Save your model:

model.save('models/mnist_model.bin')

Load and predict:

model = NeuralNet.new
model.load('models/mnist_model.bin')
prediction = model.forward(test_image)

Performance

While pure Ruby is slower than PyTorch/CUDA, dAImond achieves reasonable speeds for prototyping and small-to-medium datasets: MNIST (60k images): ~2-3 minutes per epoch on modern CPU Perfect for education, research, and production models < 1M parameters

Roadmap

  • [x] Core autograd engine
  • [x] Linear layers & activations
  • [x] MNIST 97% accuracy
  • [x] Model serialization
  • [ ] Convolutional layers (Conv2D)
  • [ ] Batch Normalization & Dropout
  • [ ] Adam/RMSprop optimizers
  • [ ] GPU support (OpenCL/CUDA via FFI)
  • [ ] ONNX export/import

Contributing

I'll be happy to see any contributors! Please read CONTRIBUTING.md for details.

License

MIT License - see LICENSE file.