Crates.io | smyl |
lib.rs | smyl |
version | 0.1.4 |
source | src |
created_at | 2024-08-20 12:08:00.051551 |
updated_at | 2024-09-14 15:08:02.791092 |
description | Artificial Neuronal Network in Rust |
homepage | |
repository | https://github.com/galitan-dev/smyl |
max_upload_size | |
id | 1345182 |
size | 164,539 |
Smyl is a machine learning library written in Rust, providing a set of tools and abstractions for building and training neural networks.
Matrix
struct for efficient matrix operations, including addition,
subtraction, multiplication, and more.SignalLayer
and SynapseLayer
, which can be used to build neural
network architectures.macros
feature enabled, Smyl provides a set of macros to simplify the creation of
neural networks.idx3
feature enabled, Smyl can read and process data in the IDX3 format,
commonly used for storing images.To use Smyl, add the following to your Cargo.toml
file:
[dependencies]
smyl = "0.1.1"
Then, in your Rust code, you can start with this hello world example
extern crate rand;
extern crate smyl;
use rand::{thread_rng, Rng};
use smyl::layer::LayerGradient;
use smyl::prelude::*;
fn main() {
let mut rng = thread_rng();
let input: SignalLayer<f64, 4> = Matrix([[1.0, 0.0, 1.0, 0.5]]);
let expected: SignalLayer<f64, 4> = Matrix([[1.0, 0.0, 0.5, 0.75]]);
let learning_rate: f64 = 0.01;
let mut synapses1: SynapseLayer<f64, 4, 2, ReLU> = rng.gen();
let mut synapses2: SynapseLayer<f64, 2, 2, ReLU> = rng.gen();
let mut synapses3: SynapseLayer<f64, 2, 4, ReLU> = rng.gen();
let total_epochs = 1000_000;
let chunk_epochs = 1;
for _ in 0..total_epochs / chunk_epochs {
let mut gradient1 = LayerGradient::default();
let mut gradient2 = LayerGradient::default();
let mut gradient3 = LayerGradient::default();
for _ in 0..chunk_epochs {
let output1 = synapses1.forward(input);
let output2 = synapses2.forward(output1);
let output3 = synapses3.forward(output2);
let output_gradient3 = output3 - expected;
let output_gradient2 =
synapses3.backward(output2, output3, output_gradient3, &mut gradient3);
let output_gradient1 =
synapses2.backward(output1, output2, output_gradient2, &mut gradient2);
synapses1.backward(input, output1, output_gradient1, &mut gradient1);
dbg!(output3);
}
synapses3.apply_gradient(gradient3, learning_rate);
synapses2.apply_gradient(gradient2, learning_rate);
synapses1.apply_gradient(gradient1, learning_rate);
}
}
But you might prefer using the macro:
use smyl::prelude::*;
fn main() {
let mut ann = ann!(4, 6, 4);
let input = Matrix([[1.0, 0.0, 1.0, 0.5]]);
let expected = Matrix([[1.0, 0.0, 0.0, 0.0]]);
let batch = 100;
let learning_rate = 0.01;
for i in 0..10000 {
let output = ann.forward(input);
ann.backward(expected);
if i % batch == 0 {
ann.apply_chunk_gradient(learning_rate * batch as f64);
println!("{output:?}");
}
}
}
You can find more example usage of Smyl in the examples
directory of the repository:
For more detailed documentations, please refer to the docs.rs page.
Contributions are welcome! Please feel free to submit a pull request or open an issue for any suggestions or improvements.
This project is licensed under the GNU GPLv3 License. See the LICENSE file for details.