narma/
narma.rs

1use echo_state_network::*;
2use rand::prelude::*;
3
4const TRAIN_STEP: usize = 10000;
5const TEST_STEP: usize = 500;
6const N_X: u64 = 1000;
7const BETA: f64 = 0.1;
8
9const NARMA_ALPHA: f64 = 0.4;
10const NARMA_BETA: f64 = 0.1;
11const NARMA_GAMMA: f64 = 0.1;
12const NARMA_DELTA: f64 = 0.1;
13
14const NARMA_STEP: usize = 10;
15
16const RANDOM_SEED: u64 = 42;
17const TEST_RANDOM_SEED: u64 = 92;
18
19fn main() {
20    let (train_input, train_expected_output) =
21        narma_n_data_gen(TRAIN_STEP, RANDOM_SEED, NARMA_STEP);
22    let (test_input, test_expected_output) =
23        narma_n_data_gen(TEST_STEP, TEST_RANDOM_SEED, NARMA_STEP);
24
25    let path = format!("{}/examples/graph", env!("CARGO_MANIFEST_DIR"));
26
27    let n_u = train_input.first().unwrap().len() as u64;
28    let n_y = train_expected_output.first().unwrap().len() as u64;
29
30    let mut model = EchoStateNetwork::new(
31        n_u,
32        n_y,
33        N_X,
34        0.1,
35        1.0,
36        0.9,
37        |x| x.tanh(),
38        None,
39        None,
40        1.0,
41        |x| x.clone_owned(),
42        |x| x.clone_owned(),
43        false,
44        BETA,
45    );
46
47    model.offline_train(&train_input, &train_expected_output);
48
49    let mut estimated_output = vec![];
50    for input in test_input.iter() {
51        estimated_output.push(model.estimate(input));
52    }
53
54    let (l2_error, l1_error) = get_error_rate(
55        estimated_output.clone(),
56        test_expected_output.clone(),
57        NARMA_STEP,
58    );
59    println!("Mean Squared Error: {}", l2_error);
60    println!("Mean Absolute Error: {}", l1_error);
61
62    let y_estimated = estimated_output.iter().map(|x| x[0]).collect::<Vec<f64>>();
63    let y_expected = test_expected_output
64        .clone()
65        .into_iter()
66        .flatten()
67        .collect::<Vec<f64>>();
68
69    plotter::plot(
70        "NARMA",
71        (0..TEST_STEP).map(|v| v as f64).collect::<Vec<f64>>(),
72        vec![y_expected, y_estimated],
73        vec!["Expected".to_string(), "Estimated".to_string()],
74        Some(&path),
75    )
76    .unwrap();
77}
78
79fn narma_n_data_gen(step: usize, seed: u64, n: usize) -> (Vec<Vec<f64>>, Vec<Vec<f64>>) {
80    let mut rng = StdRng::seed_from_u64(seed);
81
82    let input_vec = (0..step)
83        .map(|_| vec![rng.gen_range(0.0..1.0)])
84        .collect::<Vec<Vec<f64>>>();
85
86    let mut output_vec = vec![vec![0.0]; step];
87
88    for i in n..step {
89        let mut state_sum = 0.0;
90        for j in 0..n {
91            state_sum += input_vec[i - j][0];
92        }
93        output_vec[i][0] = NARMA_ALPHA * output_vec[i - 1][0]
94            + NARMA_BETA * output_vec[i - 1][0] * state_sum
95            + NARMA_GAMMA * input_vec[i - n + 1][0] * input_vec[i - 1][0]
96            + NARMA_DELTA;
97    }
98
99    (input_vec, output_vec)
100}
101
102fn get_error_rate(
103    estimated_output: Vec<Vec<f64>>,
104    expected_output: Vec<Vec<f64>>,
105    ignore_bits: usize,
106) -> (f64, f64) {
107    let estimated_output = estimated_output.iter().map(|x| x[0]).collect::<Vec<f64>>();
108    let expected_output = expected_output.into_iter().flatten().collect::<Vec<f64>>();
109
110    let mse = mean_squared_error(
111        &expected_output[ignore_bits..],
112        &estimated_output[ignore_bits..],
113    );
114    let mae = mean_absolute_error(
115        &expected_output[ignore_bits..],
116        &estimated_output[ignore_bits..],
117    );
118
119    (mse, mae)
120}