public void AddNeuron(int index, Neuron n) { this.neurons[index] = n; }
protected Neuron(Neuron neuron) { this.activationFunction = neuron.activationFunction; this.learningAlpha = neuron.learningAlpha; }
//public double Bias { get; set; } public Connection(Neuron from, Neuron to) { toNeuron = to; fromNeuron = from; Weight = RandomProvider.random.NextDouble() * 0.5; }
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 void AddConnection(Neuron neuron, bool to, double weight = 0/*, double bias = 0*/) { Connection c; if (to) c = new Connection(this, neuron, weight); else c = new Connection(neuron, this, weight); this.Connections.Add(c); neuron.Connections.Add(c); }
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 Layer(int index, int numNeurons, Neuron.ActivationType actTye = Neuron.ActivationType.SIGMOID, IEnumerable<double> initBias = null) { Index = index; NumNeurons = numNeurons; ActType = actTye; Neurons = new Neuron[NumNeurons]; if (initBias != null) { for (int i = 0; i < NumNeurons; i++) { Neurons[i] = new Neuron(i, this, ActType, initBias.ElementAt(i)); } } else { for (int i = 0; i < NumNeurons; i++) { Neurons[i] = new Neuron(i, this, ActType); } } }
public Connection(Neuron from, Neuron to, double weight) { toNeuron = to; fromNeuron = from; Weight = weight; //Bias = bias; }