public NeuralNetwork(int nbInputs, int nbOuputs, List <int> innerLayerDescr) { _inputs = new NeuralLayer(nbInputs); _outputs = new NeuralLayer(nbOuputs); for (int idxLayer = 0; idxLayer < innerLayerDescr.Count; idxLayer++) { _innerLayers.Add(new NeuralLayer(innerLayerDescr[idxLayer])); } NeuralLayer prevLayer = _outputs; for (int idxLayer = innerLayerDescr.Count - 1; idxLayer >= 0; idxLayer--) { NeuralLayer postLayer = idxLayer > 0 ? _innerLayers[idxLayer - 1] : _inputs; for (int idxNeuron = 0; idxNeuron < innerLayerDescr[idxLayer]; idxNeuron++) { _innerLayers[idxLayer].Neurons.Add(new Neuron(postLayer, prevLayer)); } _innerLayers[idxLayer].SetBias(new NeuronBias(prevLayer)); prevLayer = _innerLayers[idxLayer]; } for (int idxInput = 0; idxInput < nbInputs; idxInput++) { _inputs.Neurons.Add(new NeuronInput(prevLayer, "")); } _inputs.SetBias(new NeuronBias(prevLayer)); for (int idxOutput = 0; idxOutput < nbOuputs; idxOutput++) { _outputs.Neurons.Add(new NeuronOutput(_innerLayers.Last(), "")); } }
void AnnVisualizerForm_Paint(object sender, PaintEventArgs e) { Pen pen = new Pen(Color.FromArgb(255, 0, 0, 0)); int idxNeuron = 0; Label curDest = null; int idxLayer = 0; int nextLayerIdx = _ann._outputs.Neurons.Count; foreach (var nn in _ann._outputs.Neurons) { var nextOrigin = findWeightLabelOrig(idxNeuron++); int idxWeight = 0; Value max = nn.Weights.Max(); foreach (var nextNn in _ann._innerLayers[0].Neurons) { curDest = findWeightLabelDest(nextLayerIdx++); double strength = Math.Abs(nn.Weights[idxWeight++].X / max.X); pen.Color = Color.FromArgb(255, (byte)(strength * 255.0), 0, (byte)((1.0 - strength) * 255.0)); e.Graphics.DrawLine(pen, nextOrigin.Location.X, nextOrigin.Location.Y, curDest.Location.X, curDest.Location.Y + 10); } } foreach (var layer in _ann._innerLayers) { int idxCurLayerNn = idxNeuron + layer.Neurons.Count; foreach (var nn in layer.Neurons) { var nextOrigin = findWeightLabelOrig(idxNeuron++); NeuralLayer nextLayer = null; if (_ann._innerLayers.Count > idxLayer + 1) { nextLayer = _ann._innerLayers[idxLayer + 1]; } else { nextLayer = _ann._inputs; } nextLayerIdx = idxCurLayerNn; if (nn.Weights.Count > 0) { Value max = nn.Weights.Max(); int idxWeight = 0; foreach (var nextNn in nextLayer.Neurons) { curDest = findWeightLabelDest(nextLayerIdx++); double strength = Math.Abs(nn.Weights[idxWeight++].X / max.X); pen.Color = Color.FromArgb(255, (byte)(strength * 255.0), 0, (byte)((1.0 - strength) * 255.0)); e.Graphics.DrawLine(pen, nextOrigin.Location.X, nextOrigin.Location.Y, curDest.Location.X, curDest.Location.Y + 10); } } } idxLayer++; } }
public AnnVisualizerForm(NeuralNetwork ann) { _ann = ann; InitializeComponent(); ResizeEnd += AnnVisualizerForm_Resize; Paint += AnnVisualizerForm_Paint; var tlayer = new NeuralLayer(1); Neuron input = new NeuronInput(tlayer, "X"); tlayer.Neurons.Add(input); _transferFunc = new Neuron(tlayer, new NeuralLayer(0), "transfer"); _transferFunc.Weights[0].X = 1.0; Recompute(); }
public Neuron(NeuralLayer children, NeuralLayer parentLayer, string label = "hidden") { _parents = parentLayer; _children = children; _label = label; int size = children.Size; while (size-- > 0) { Weights.Add(new Value()); } if (children.Size > 0) // Bias neuron { Weights.Add(new Value()); } size = parentLayer.Size; while (size-- > 0) { Deltas.Add(new Value()); } }
public NeuronBias(NeuralLayer nextLayer) : base(nextLayer, "bias") { Value.X = 1.0; }
public NeuronInput(NeuralLayer firstInnerLayer, string label) : base(new NeuralLayer(0), firstInnerLayer, label) { }
public NeuronOutput(NeuralLayer outputLayer, string label) : base(outputLayer, new NeuralLayer(0), label) { }
public NeuralNetwork(int nbInputs, int nbOuputs, List<int> innerLayerDescr) { _inputs = new NeuralLayer(nbInputs); _outputs = new NeuralLayer(nbOuputs); for (int idxLayer = 0; idxLayer < innerLayerDescr.Count; idxLayer++) _innerLayers.Add(new NeuralLayer(innerLayerDescr[idxLayer])); NeuralLayer prevLayer = _outputs; for (int idxLayer = innerLayerDescr.Count - 1; idxLayer >= 0; idxLayer--) { NeuralLayer postLayer = idxLayer > 0 ? _innerLayers[idxLayer - 1] : _inputs; for (int idxNeuron = 0; idxNeuron < innerLayerDescr[idxLayer]; idxNeuron++) _innerLayers[idxLayer].Neurons.Add(new Neuron(postLayer, prevLayer)); _innerLayers[idxLayer].SetBias(new NeuronBias(prevLayer)); prevLayer = _innerLayers[idxLayer]; } for (int idxInput = 0; idxInput < nbInputs; idxInput++) _inputs.Neurons.Add(new NeuronInput(prevLayer, "")); _inputs.SetBias(new NeuronBias(prevLayer)); for (int idxOutput = 0; idxOutput < nbOuputs; idxOutput++) _outputs.Neurons.Add(new NeuronOutput(_innerLayers.Last(), "")); }
public Neuron(NeuralLayer children, NeuralLayer parentLayer, string label = "hidden") { _parents = parentLayer; _children = children; _label = label; int size = children.Size; while (size--> 0) Weights.Add(new Value()); if (children.Size > 0) // Bias neuron Weights.Add(new Value()); size = parentLayer.Size; while (size--> 0) Deltas.Add(new Value()); }