()
| 115 | } |
| 116 | |
| 117 | pub fn plot_nn() { |
| 118 | |
| 119 | let seed = [1, 2, 3, 4]; |
| 120 | let n = 50; |
| 121 | let x = mixture_builder() |
| 122 | .add(n, normal_builder(seed).add(0.0, 0.2).add(0.0, 0.2)) |
| 123 | .add(n, normal_builder(seed).add(1.0, 0.2).add(1.0, 0.2)) |
| 124 | .add(n, normal_builder(seed).add(0.0, 0.2).add(1.0, 0.2)) |
| 125 | .add(n, normal_builder(seed).add(1.0, 0.2).add(0.0, 0.2)) |
| 126 | .as_matrix() |
| 127 | .rm_column(0).unwrap(); |
| 128 | |
| 129 | // create the labels |
| 130 | let labels = Matrix::from_iter( |
| 131 | repeat(0.0).take(2 * n).chain(repeat(1.0).take(2 * n)), 1 |
| 132 | ).unwrap(); |
| 133 | |
| 134 | let n = NeuralNetwork::new() |
| 135 | .add_layer(2) // input layer with two units |
| 136 | .add_layer(3) // hidden layer with two units |
| 137 | .add_layer(1) // output layer |
| 138 | .optimize(&x, &labels, empty_opts().alpha(20.0).iter(500)); |
| 139 | |
| 140 | // create the values for the contour plot |
| 141 | let mut p = vec![]; |
| 142 | for y in (-1.0).linspace(2.0, 100) { |
| 143 | for x in (-1.0).linspace(2.0, 100) { |
| 144 | p.push(*n.predict(&[x, y]).first().unwrap()); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | builder() |
| 149 | // contour plot |
| 150 | .add_vector("P = $$", &p) |
| 151 | .add("tx = ty = linspace(-1, 2, 100)") |
| 152 | .add("contour(tx, ty, reshape(P, 100, 100))") |
| 153 | .add("hold on") |
| 154 | // examples |
| 155 | .add_matrix("X = $$", &x) |
| 156 | .add_vector_iter("y = $$", labels.values()) |
| 157 | .add("plot(X(y == 0, 1), X(y == 0, 2), 'd', 'markersize', 6, 'markerfacecolor', 'yellow', 'color', 'black')") |
| 158 | .add("plot(X(y == 1, 1), X(y == 1, 2), 'o', 'markersize', 6, 'markerfacecolor', 'blue', 'color', 'black')") |
| 159 | .add("axis([-1, 2, -1, 2])") |
| 160 | .add("axis('nolabel')") |
| 161 | .add("grid on") |
| 162 | .add("print -dpng -r50 '/tmp/nn.png'") |
| 163 | .run("/tmp/nn.m") |
| 164 | .unwrap(); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | pub fn main() { |
no test coverage detected