circle/
circle.rs

1use tiny_ml::prelude::*;
2
3fn main() {
4    // this network is completely overkill, but it does the job
5    let mut net: NeuralNetwork<2, 1> = NeuralNetwork::new()
6        .add_layer(3, ActivationFunction::ReLU)
7        .add_layer(3, ActivationFunction::ReLU)
8        // this layer reduced everything to one input!
9        .add_layer(1, ActivationFunction::Linear);
10
11    let mut inputs = vec![];
12    let mut output = vec![];
13    for x in 0..=100 {
14        for y in 0..=100 {
15            inputs.push([x as f32, y as f32]);
16            // we want this to be a classifier, so we ask it for a result greater zero or smaller zero
17            output.push(if (x as f32).abs() + (y as f32).abs() < 30.0 {
18                [1.0]
19            } else {
20                [-1.0]
21            })
22        }
23    }
24
25    let data = DataSet {
26        inputs,
27        outputs: output,
28    };
29
30    let trainer = BasicTrainer::new(data);
31    for _ in 0..50 {
32        trainer.train(&mut net, 10);
33        println!("{}", trainer.get_total_error(&net))
34    }
35}