public BackPropogationNetwork(int numInputs, int numOutputs, int numHidden, int numHiddenLayers = 1) : base(numHiddenLayers + 2) { Layers[0] = new Layer(0, numInputs, Neuron.ActivationType.RAMP); Layers[2 + numHiddenLayers - 1] = new Layer(2 + numHiddenLayers - 1, numOutputs, Neuron.ActivationType.SIGMOID); for (int i = 0; i < numHiddenLayers; i++) { Layers[i + 1] = new Layer(i + 1, numHidden, Neuron.ActivationType.SIGMOID); } for (int i = 0; i < NumLayers - 1; i++) { Layers[i].ConnectAllTo(Layers[i + 1]); } InputIndex = 0; OutputIndex = NumLayers - 1; }
public List <List <List <double> > > BackPropogate(DataSet datain, double learninnRate, double momentum = 0) { if (PrevDW == null) { PrevDW = new List <List <List <double> > >(); deltaArr = new List <List <double> >(); for (int i = 0; i < NumLayers; i++) { deltaArr.Add(new List <double>()); PrevDW.Add(new List <List <double> >()); for (int n = 0; n < Layers[i].NumNeurons; n++) { deltaArr[i].Add(0); PrevDW[i].Add(new List <double>()); IEnumerable <Connection> outConn = Layers[i].Neurons[n].Connections.Where(r => r.toNeuron == Layers[i].Neurons[n]); int d = 0; foreach (Connection c in outConn) { PrevDW[i][n].Add(0); d++; } } } } ApplyInput(datain.Inputs); CalculateOutput(); Layer currentLayer = Layers[OutputIndex]; while (currentLayer != Layers[InputIndex]) { Parallel.ForEach(currentLayer.Neurons, new Action <Neuron>((n) => { double error = 0; if (currentLayer == Layers[OutputIndex]) { error = datain.Outputs[n.Index] - n.Value; } else { foreach (Connection c in n.Connections.Where(r => r.fromNeuron == n)) { error += c.Weight * deltaArr[c.toNeuron.SelfLayer.Index][c.toNeuron.Index]; } } error = error * n.Value * (1 - n.Value); deltaArr[currentLayer.Index][n.Index] = error; })); currentLayer = Layers[currentLayer.Index - 1]; } currentLayer = Layers[OutputIndex]; while (currentLayer != Layers[InputIndex]) { for (int i = 0; i < currentLayer.NumNeurons; i++) { Neuron n = currentLayer.Neurons[i]; foreach (Connection c in n.Connections.Where(r => r.toNeuron == n)) { double dw = (deltaArr[c.toNeuron.SelfLayer.Index][c.toNeuron.Index] * learninnRate * c.fromNeuron.Value) + (momentum * PrevDW[currentLayer.Index][i][n.Connections.IndexOf(c)]); c.Weight += dw; PrevDW[currentLayer.Index][i][n.Connections.IndexOf(c)] = dw; } n.Bias += deltaArr[currentLayer.Index][i] * learninnRate; } currentLayer = Layers[currentLayer.Index - 1]; } return(PrevDW); }
public NeuralNetwork(int numLayers) { Layers = new Layer[numLayers]; NumLayers = numLayers; }
void Form1_Updated(Layer l, EventArgs e) { MessageBox.Show("Hello"); }
public Neuron(int index, Layer initLayer, Neuron.ActivationType actType = ActivationType.SIGMOID, double bias = 0) { Index = index; Connections = new List<Connection>(); SelfLayer = initLayer; ActType = actType; Bias = bias; }
public NeuralNetwork(NetworkData network) { Layers = new Layer[network.Layers.Count]; NumLayers = network.Layers.Count; foreach (LayerData ld in network.Layers) { Layers[network.Layers.IndexOf(ld)] = new Layer(network.Layers.IndexOf(ld), ld.NumNeuron, ld.ActType, ld.Bias); } foreach (ConnectionData cd in network.Connections) { Layers[cd.From.Layer].Neurons[cd.From.Node].AddConnection(Layers[cd.To.Layer].Neurons[cd.To.Node], true, cd.Weight); } InputIndex = network.InputLayerId; OutputIndex = network.OutputLayerId; }
public void ConnectAllTo(Layer nextlayer, double defaultWeight = 0) { //Connects all neurons in current layer to the neurons in the next foreach (Neuron n1 in Neurons) { foreach (Neuron n2 in nextlayer.Neurons) { n1.AddConnection(n2, true, RandomProvider.random.NextDouble() - 0.5); } } }