Crates.io | rust_nn |
lib.rs | rust_nn |
version | 0.1.0 |
source | src |
created_at | 2023-12-31 06:36:13.479881 |
updated_at | 2023-12-31 06:36:13.479881 |
description | An educational Neural Network framework in Rust |
homepage | |
repository | https://github.com/Lawqup/rust-nn |
max_upload_size | |
id | 1084601 |
size | 42,416 |
Features include:
For more detailed examples, look at the examples directory.
Note: Running in release makes training drastically faster, i.e. do cargo run --release
.
Here is how you'd create a model that learns to behave like an XOR gate.
let mut net = NeuralNet::new(2, 2, Activations::Arctan);
net.add_layer(1, Activations::Sigmoid);
let inputs = Matrix2::from_array([[0, 0], [0, 1], [1, 0], [1, 1]]).into();
let targets = Matrix2::from_array([[0], [1], [1], [0]]).into();
let learning_rate = 10e-0;
let epochs = 10_000;
// Initialize the optimizer to log the error at each epoch
let optim = Optimizer::new(OptimizerMethod::Backprop, learning_rate, epochs).with_log(Some(1));
optim.train(&mut net, &inputs, &targets);
let fin = net.mean_squared_error(&inputs, &targets).unwrap();
println!("------------------");
let res = net.run_batch(&inputs).unwrap();
for (inp, out) in inputs.to_vec().into_iter().zip(res.to_vec()) {
println!("{:?} -> {}", inp.to_vec(), out[0])
}
To make the optimizer display graph the error at each epoch, you can do this:
optim.train_gui<NNGui>(&mut net, &inputs, &targets);
Here, NNGui
is a predefined struct implementing the Visualizer
trait that allows the optimizer to spawn and asynchronously update a GUI (meaning performance shouldn't be affected).