MCPcopy Create free account
hub / github.com/daniel-e/rustml / main

Function main

examples/neuralnetwork.rs:14–63  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12// the XOR function.
13
14fn main() {
15 // create the dataset
16 let seed = [1, 2, 3, 4];
17 let n = 50;
18 let x = mixture_builder()
19 .add(n, normal_builder(seed).add(0.0, 0.2).add(0.0, 0.2))
20 .add(n, normal_builder(seed).add(1.0, 0.2).add(1.0, 0.2))
21 .add(n, normal_builder(seed).add(0.0, 0.2).add(1.0, 0.2))
22 .add(n, normal_builder(seed).add(1.0, 0.2).add(0.0, 0.2))
23 .as_matrix()
24 .rm_column(0).unwrap();
25
26 // create the labels
27 let labels = Matrix::from_iter(
28 repeat(0.0).take(2 * n).chain(repeat(1.0).take(2 * n)), 1
29 ).unwrap();
30
31 let n = NeuralNetwork::new()
32 .add_layer(2) // input layer with two units
33 .add_layer(3) // hidden layer with two units
34 .add_layer(1) // output layer
35 .optimize(&x, &labels, empty_opts().alpha(20.0).iter(500));
36
37 // create the values for the contour plot
38 let mut p = vec![];
39 for y in (-1.0).linspace(2.0, 100) {
40 for x in (-1.0).linspace(2.0, 100) {
41 p.push(*n.predict(&[x, y]).first().unwrap());
42 }
43 }
44
45 builder()
46 // contour plot
47 .add_vector("P = $$", &p)
48 .add("tx = ty = linspace(-1, 2, 100)")
49 .add("contour(tx, ty, reshape(P, 100, 100))")
50 .add("hold on")
51 // examples
52 .add_matrix("X = $$", &x)
53 .add_vector_iter("y = $$", labels.values())
54 .add("plot(X(y == 0, 1), X(y == 0, 2), 'd', 'markersize', 6, 'markerfacecolor', 'yellow', 'color', 'black')")
55 .add("plot(X(y == 1, 1), X(y == 1, 2), 'o', 'markersize', 6, 'markerfacecolor', 'blue', 'color', 'black')")
56 .add("axis([-1, 2, -1, 2])")
57 .add("grid on")
58 .add("print -dpng -r100 '/tmp/nn.png'")
59 .run("/tmp/nn.m")
60 .unwrap();
61
62 Window::new().show_image(&RgbImage::from_file("/tmp/nn.png").unwrap()).wait_key();
63}
64

Callers

nothing calls this directly

Calls 15

mixture_builderFunction · 0.85
normal_builderFunction · 0.85
empty_optsFunction · 0.85
from_fileFunction · 0.85
rm_columnMethod · 0.80
as_matrixMethod · 0.80
optimizeMethod · 0.80
add_layerMethod · 0.80
alphaMethod · 0.80
linspaceMethod · 0.80
predictMethod · 0.80
runMethod · 0.80

Tested by

no test coverage detected