private void xorBtn_Click(object sender, EventArgs e) { NeuralNet nn = new NeuralNet(2, 2, 1); double[][] xor_examples = new double[][] { new double[] { 0.1, 0.1 }, // 0 0 new double[] { 0.1, 0.9 }, // 0 1 new double[] { 0.9, 0.1 }, // 1 0 new double[] { 0.9, 0.9 }, // 1 1 }; double[][] xor_targets = new double[][] { new double[] { 0.1 }, // 0 new double[] { 0.9 }, // 1 new double[] { 0.9 }, // 1 new double[] { 0.1 }, // 0 }; nn.train(xor_examples, xor_targets, Math.Pow(10, -20)); Debug.WriteLine("done learning"); Func <double, int> decodeOutput = x => { if (x >= 0.5) { return(1); } return(0); }; for (int j = 0; j < xor_examples.Length; j++) { double[] output = nn.run(xor_examples[j]); for (int i = 0; i < output.Length; i++) { Debug.WriteLine(i + ": " + output[i] + " => " + decodeOutput(output[i])); } } }
private void learnPctBtn_Click(object sender, EventArgs e) { NeuralNet nn = new NeuralNet(2, 6, 3); double[][] examples = new double[pts.Length][]; for (int i = 0; i < pts.Length; i++) { examples[i] = new double[2]; examples[i][0] = pts[i].p.X; examples[i][1] = pts[i].p.Y; } double[][] targets = new double[pts.Length][]; for (int i = 0; i < pts.Length; i++) { targets[i] = new double[zones.Length]; for (int j = 0; j < zones.Length; j++) { targets[i][j] = 0.1; } targets[i][pts[i].zone] = 0.9; } nn.train(examples, targets, 20);// Math.Pow(10, -1)); Debug.WriteLine("done learning"); Action <int, int, double[]> decodeOutput = (x, y, output) => { double max = 0; int pos = 0; for (int i = 0; i < output.Length; i++) { if (output[i] > max) { max = output[i]; pos = i; } } Point p = cartesianToScreenCoords(new Point(x, y)); ((Bitmap)pictureBox1.Image).SetPixel(p.X, p.Y, zones[pos].pen.Color); //Graphics g = pictureBox1.CreateGraphics(); //g.DrawEllipse(zones[pos].pen, p.X, p.Y, 3, 3); //pictureBox1.Refresh(); }; for (int i = -WIDTH / 2; i < WIDTH / 2; i += 1) { for (int j = -WIDTH / 2; j < WIDTH / 2; j += 1) { double[] output = nn.run(new double[] { j, i }); decodeOutput(j, i, output); } } pictureBox1.Refresh(); }