public static void drawNeurons(List <List <Neuron> > layers) { UInt16 x = (Visualization.nrnSize * 2), y = x; List <List <Byte> > adjustedLayers = new List <List <Byte> >(); IntPtr dc = Visualization.GetDC(IntPtr.Zero); using (Pen pen = new Pen(Color.Black)) using (Pen pen0 = new Pen(Color.White)) using (Graphics g = Graphics.FromHdc(dc)) { g.FillRectangle(pen0.Brush, 0, 0, Visualization.width, Visualization.height); foreach (List <Neuron> layer in layers) { if (layer.Count <= Visualization.maxLayerLengthToDraw) { adjustedLayers.Add(layer.Select(x0 => x0.activation).ToList()); continue; } UInt16 split = (UInt16)(layer.Count / Visualization.maxLayerLengthToDraw), j = 0, ctr = 0; Byte[] newNeurons = new Byte[Visualization.maxLayerLengthToDraw]; Byte i = 0; UInt32 sum = 0; while (i < Visualization.maxLayerLengthToDraw) { while (j < split) { sum += layer[ctr].activation; ++j; ++ctr; } newNeurons[i] = (Byte)(sum / j); sum = 0; j = 0; ++i; } adjustedLayers.Add(newNeurons.ToList()); } foreach (List <Byte> layer in adjustedLayers) { foreach (Byte b in layer) { g.DrawEllipse(pen, new RectangleF(x, y, Visualization.nrnSize, Visualization.nrnSize)); g.DrawString(b.ToString(), Visualization.font, pen.Brush, x, y + (Visualization.nrnSize / 4)); y += (Visualization.nrnSize * 2); } x += (Visualization.nrnSize * 2); y = (Visualization.nrnSize * 2); } } Visualization.ReleaseDC(IntPtr.Zero, dc); }
/// <summary> /// Make a prediction /// </summary> /// <param name="input"></param> /// <param name="desired"></param> /// <param name="learn"></param> /// <returns></returns> public List <Byte> makePrediction(IEnumerable <Byte> input, Byte[] desired, Boolean learn, Boolean visualize = false, Action onFail = null) { //Clear all potential old neuron values foreach (List <Neuron> layer in this.layers.Skip(1)) { foreach (Neuron nrn in layer) { nrn.activation = 0; } } //Restructure input List <Byte> _input; if (input.GetType() == typeof(List <Byte>)) { _input = (List <Byte>)input; } else { _input = new List <Byte>(input); } //Fill input layers UInt16 i = 0; while (i < this.layerData[0]) /* possible data loss if input.Length>layerData[0] */ { this.layers[0][i].activation = _input[i]; ++i; } i = 0; Single sum = 0; while (i < (this.layerCount - 1)) { foreach (Neuron neuron in this.layers[i + 1]) { foreach (Neuron neuron0 in this.layers[i]) { // Console.WriteLine("Activation:"+neuron0.activation.ToString()+",bias:"+neuron0.bias.ToString()+",weight:"+this.weights[neuron0][neuron].ToString()); sum += (neuron0.activation + neuron0.bias) * (this.weights[neuron0][neuron]) * 0.5F; } neuron.activation = sum.sigmoid(); // Console.WriteLine("Sum:"+sum.ToString()+",Sigmoided:"+sum.sigmoid().ToString()); sum = 0; } ++i; } if (visualize) { Visualization.drawNeurons(this.layers); } if (learn) { this.backPropogate(desired, onFail); } return(this.layers[this.layerCount - 1].Select(x => x.activation).ToList()); }