()
| 12 | // the XOR function. |
| 13 | |
| 14 | fn 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 |
nothing calls this directly
no test coverage detected