Expand description
§Only Brain
A very simple Neural Network library built in Rust with the objective to allow the user to create, manipulate and train a neural network directly. The user has direct access to weights and biases of the network, allowing them to implement their own training and manipulation methods.
§Example
The input and output widths are part of the network’s type, so a wrongly sized
input is a compile error. Hidden layers are given at construction time, and weights
and biases are plain arrays, slices or Vecs.
use only_brain::NeuralNetwork;
// A 2 -> 2 -> 1 network.
let mut nn = NeuralNetwork::<2, 1>::new(&[2]);
// One row per neuron, one weight per neuron of the previous layer.
nn.set_layer_weights(1, &[[0.1, 0.2],
[0.3, 0.4]]);
nn.set_layer_biases(1, &[0.1, 0.2]);
nn.set_layer_weights(2, &[[0.9, 0.8]]);
nn.set_layer_biases(2, &[0.1]);
let output = nn.feed_forward(&[0.5, 0.2]);
println!("{:?}", output);§Training it your way
The library leaves learning to you. Two views of the same network make that easy:
- Per layer:
NeuralNetwork::layer_weights,NeuralNetwork::set_layer_biasesand friends, for methods that reason about layers, such as backpropagation. - Flat:
NeuralNetwork::parameters,NeuralNetwork::set_parametersandNeuralNetwork::from_parameters, for methods that search a list of numbers, such as genetic algorithms. A genome ofNeuralNetwork::parameter_count_forgenes is a network.
use only_brain::{ActivationFunction, NeuralNetwork};
/// Scores a genome, as a genetic algorithm's fitness function would.
fn fitness(genome: &[f64]) -> f64 {
let mut nn = NeuralNetwork::<2, 1>::from_parameters(&[3], genome);
nn.set_activation_function(ActivationFunction::Tanh);
nn.set_output_activation(ActivationFunction::Sigmoid);
let [output] = nn.feed_forward(&[1.0, 0.0]);
-(1.0 - output).powi(2)
}
let genome = vec![0.1; NeuralNetwork::<2, 1>::parameter_count_for(&[3])];
println!("fitness: {}", fitness(&genome));Each layer has its own ActivationFunction, and dump_model and
load_model save and load networks in a versioned format that keeps reading
files written by earlier versions.
Macros§
- bvector
- Macro to construct a
BVectorwith compile-time dimension inferred from the number of elements provided.
Structs§
- BVector
- This module provides a vector type
BVectorthat is optimized for compile-time dimension and uses SIMD operations for performance. - Neural
Network - Neural Network
- Perceptron
- Perceptron
Enums§
- Activation
Function - The function a layer applies to each neuron’s weighted sum.
- Model
Error - Something that went wrong while saving or loading a model.
Constants§
- MODEL_
FORMAT_ VERSION - The model format version
dump_modelandwrite_modelwrite.load_modelandread_modelread this version and every earlier one.
Functions§
- binary_
step - dump_
model - Writes a model to
path, in the newest format version. - get_
activation_ function - Returns the plain function behind an
ActivationFunction. - identity
- load_
model - Reads a model from
path, checking that its shape matchesINandOUT. - read_
model - Reads one model from any reader, checking that its shape matches
INandOUT. - relu
- sigmoid
- tanh
- write_
model - Writes a model to any writer, in the newest format version.