[][src]Crate maver

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]));

Structs

Genetic
L1
L2
LearnParams
NeuralNetwork
Organism
Perceptron

Enums

Activation
Regularization

Traits

Environment

A trait that is implemented by user to allow test of the Environment.

RegularizationFn

Functions

err_der
err_fn