bike_feedback/
feedback.rs

1// Copyright (C) 2024 Hallvard Høyland Lavik
2
3use neurons::{activation, feedback, network, objective, optimizer, plot, random, tensor};
4
5use std::{
6    fs::File,
7    io::{BufRead, BufReader},
8};
9
10fn data(path: &str) -> (Vec<tensor::Tensor>, Vec<tensor::Tensor>) {
11    let reader = BufReader::new(File::open(&path).unwrap());
12
13    let mut x: Vec<tensor::Tensor> = Vec::new();
14    let mut y: Vec<tensor::Tensor> = Vec::new();
15
16    for line in reader.lines().skip(1) {
17        let line = line.unwrap();
18        let record: Vec<&str> = line.split(',').collect();
19
20        let mut data: Vec<f32> = Vec::new();
21        for i in 2..14 {
22            data.push(record.get(i).unwrap().parse::<f32>().unwrap());
23        }
24        x.push(tensor::Tensor::single(data));
25
26        y.push(tensor::Tensor::single(vec![record
27            .get(16)
28            .unwrap()
29            .parse::<f32>()
30            .unwrap()]));
31    }
32
33    let mut generator = random::Generator::create(12345);
34    let mut indices: Vec<usize> = (0..x.len()).collect();
35    generator.shuffle(&mut indices);
36
37    let x: Vec<tensor::Tensor> = indices.iter().map(|i| x[*i].clone()).collect();
38    let y: Vec<tensor::Tensor> = indices.iter().map(|i| y[*i].clone()).collect();
39
40    (x, y)
41}
42
43fn main() {
44    // Load the ftir dataset
45    let (x, y) = data("./examples/datasets/bike/hour.csv");
46
47    let split = (x.len() as f32 * 0.8) as usize;
48    let x = x.split_at(split);
49    let y = y.split_at(split);
50
51    let x_train: Vec<&tensor::Tensor> = x.0.iter().collect();
52    let y_train: Vec<&tensor::Tensor> = y.0.iter().collect();
53    let x_test: Vec<&tensor::Tensor> = x.1.iter().collect();
54    let y_test: Vec<&tensor::Tensor> = y.1.iter().collect();
55
56    // Create the network
57    let mut network = network::Network::new(tensor::Shape::Single(12));
58
59    network.dense(24, activation::Activation::ReLU, false, None);
60    network.feedback(
61        vec![
62            feedback::Layer::Dense(24, activation::Activation::ReLU, false, None),
63            feedback::Layer::Dense(24, activation::Activation::ReLU, false, None),
64        ],
65        3,
66        true,
67        true,
68        feedback::Accumulation::Mean,
69    );
70
71    network.dense(1, activation::Activation::Linear, false, None);
72    network.set_objective(objective::Objective::RMSE, None);
73
74    network.set_optimizer(optimizer::Adam::create(0.01, 0.9, 0.999, 1e-4, None));
75
76    println!("{}", network);
77
78    // Train the network
79
80    let (train_loss, val_loss, val_acc) = network.learn(
81        &x_train,
82        &y_train,
83        Some((&x_test, &y_test, 25)),
84        64,
85        600,
86        Some(100),
87    );
88    plot::loss(
89        &train_loss,
90        &val_loss,
91        &val_acc,
92        &"FEEDBACK : BIKE",
93        &"./output/bike/feedback.png",
94    );
95
96    // Use the network
97    let prediction = network.predict(x_test.get(0).unwrap());
98    println!(
99        "Prediction. Target: {}. Output: {}.",
100        y_test[0].data, prediction.data
101    );
102}