30个版本 (6个稳定版本)

1.4.0 2022年9月12日
1.3.0 2022年8月30日
1.2.0 2022年7月25日
1.1.0 2021年12月25日
0.1.10 2020年11月23日

#213 in 机器学习

Download history 1/week @ 2024-03-10 197/week @ 2024-03-31

每月下载量:92

GPL-3.0 许可证

125KB
3K SLoC

neat-gru-rust

CICD Crates.io Downloads License

文档

Crates.io 文档

示例

XOR

目前这是唯一的运行示例。您可以通过以下方式运行它:

cargo run --example example

如何使用

In Cargo.toml

[dependencies]
neat-gru = 1.3.0"

创建一个实现Game trait的结构体

use neat_gru::game::Game;
use neat_gru::neural_network::NeuralNetwork;
use neat_gru::train::{Train, HistoricTopology};
struct Player {
    pub net: NeuralNetwork<f64>,
}

impl Player {
    pub fn new(net: NeuralNetwork<f64>) -> Player {
        Player {
            net,
        }
    }
}

struct Simulation {
    players: Vec<Player>,
}

impl Simulation {
    pub fn new() -> Simulation {
        Simulation {
            players: Vec::new(),
        }
    }
}

impl Game<f64> for Simulation {
    // Loss function
    fn run_generation(&mut self) -> Vec<f64> {
        let inputs = get_inputs();
        self.players.iter().map(|p| {
            let output = p.net.compute(inputs);
            let scores = compute_score(output, target);
            scores
        }).collect()
    }

    // Reset networks
    fn reset_players(&mut self, nets: Vec<NeuralNetwork<f64>>) {
        self.players.clear();
        self.players = nets
            .into_iter()
            .map(Player::new)
            .collect();
    }

    // Called at the end of training
    fn post_training(&mut self, history: &[HistoricTopology<f64>]) {
        // Iter on best topologies and upload the best one
    }
}

异步运行_generation(必须在像Tokio这样的异步运行时中运行)


#[async_trait]
impl GameAsync<f64> for Simulation {
    // Loss function
    async fn run_generation(&mut self) -> Vec<f64> {
        let inputs = get_inputs().await;
        self.players.iter().map(|p| {
            let output = p.net.compute(inputs);
            let scores = compute_score(output, target);
            scores
        }).collect()
    }
}

启动训练

fn run_sim() {
    let mut sim = Simulation::new();

    let mut runner = Train::new(&mut sim);
    runner
        .inputs(input_count)
        .outputs(output_count as i32)
        .iterations(nb_generations as i32)
        .max_layers((hidden_layers + 2) as i32)
        .max_per_layers(hidden_layers as i32)
        .max_species(max_species as i32)
        .max_individuals(max_individuals as i32)
        .delta_threshold(2.) // Delta parameter from NEAT paper
        .formula(0.8, 0.8, 0.3) // c1, c2 and c3 from NEAT paper
        .access_train_object(Box::new(|train| {
            let species_count = train.species_count();
            println!("Species count: {}", species_count);
        })) // Callback called after `reset_players` that gives you access to the train object during training
        .start(); // .start_async().await for async version
}

依赖项

~4.5–5.5MB
~115K SLoC