1 unstable release
0.1.0 | Aug 22, 2019 |
---|
#70 in #genetic-algorithm
25KB
522 lines
Maver
Neural network library for Rust programming language.
Maver implements multilayer perceptron neural network and two learning algorithms: backpropagation and genetic algorithm.
For more documentations look at documentation.
lib.rs
:
Implementation of multilayer perceptron,backpropagation algorithm and some kind of genetic algorithm. Using genetic algorithm:
extern crate maver;
use maver::*;
struct XOR;
impl Environment for XOR {
fn test(&self, org: &mut Organism) {
let mut distance: f64;
let nn = &mut org.nn;
let out = nn.forward_prop(&[0.0, 0.0]);
distance = (0.0 - out[0]).powi(2);
let out = nn.forward_prop(&[0.0, 1.0]);
distance += (1.0 - out[0]).powi(2);
let out = nn.forward_prop(&[1.0, 0.0]);
distance += (1.0 - out[0]).powi(2);
let out = nn.forward_prop(&[1.0, 1.0]);
distance += (0.0 - out[0]).powi(2);
let fitness = 16.0 / (1.0 + distance);
org.fitness = fitness;
}
}
fn main() {
let mut n = Genetic::new(100, &[2, 1], Activation::Tanh, Activation::Tanh);
let p = LearnParams::default();
let mut champion = None;
let mut i = 0;
while champion.is_none() {
n.evaluate(&mut XOR);
if n.get_champion().fitness > 15.9 {
champion = Some(n.get_champion().clone());
}
println!(
"Iteration: {:?},best fitness: {}",
i,
n.get_champion().fitness
);
n.evolve(&p);
i += 1;
}
println!("{:#?}", champion.as_ref().unwrap());
println!(
"{:?}",
champion.as_mut().unwrap().nn.forward_prop(&[0.0, 0.0])
);
println!(
"{:?}",
champion.as_mut().unwrap().nn.forward_prop(&[1.0, 0.0])
);
println!(
"{:?}",
champion.as_mut().unwrap().nn.forward_prop(&[1.0, 1.0])
);
println!(
"{:?}",
champion.as_mut().unwrap().nn.forward_prop(&[0.0, 1.0])
);
}
Using backpropagation algorithm:
let mut nn = NeuralNetwork!(vec![2,2,1],Activation::Tanh,Activation::Tanh);
let xor_sets = vec![
(vec![0., 0.], vec![0.0]),
(vec![0., 1.], vec![1.0]),
(vec![1., 0.], vec![1.0]),
(vec![1., 1.], vec![0.0]),
];
nn.train(1000,false,xor_sets,Some(0.01));
println!("{:?}",nn.forward_prop(&[0.0,0.0]));
println!("{:?}",nn.forward_prop(&[0.0,1.0]));
Dependencies
~2.5–3.5MB
~72K SLoC