double sumDOW(Warstwa nextLayer) { double sum = 0.0; // Sum our contributions of the errors at the nodes we feed. for (int n = 0; n < nextLayer.neurony.Length - 1; ++n) { sum += wagi[n] * nextLayer.neurony[n].gradient; } return(sum); }
public void backProp(double[] target) { error = 0; //liczenie bleu na warstwie wyjsciowej for (int i = 0; i < warstwy[warstwy.Length - 1].neurony.Length - 1; i++) { double delta = target[i] - warstwy[warstwy.Length - 1].neurony[i].output; error += delta * delta; } error /= warstwy[warstwy.Length - 1].neurony.Length - 1; error = Math.Sqrt(error); //liczenie gradienta warstwy wyjsciowej for (int n = 0; n < warstwy[warstwy.Length - 1].neurony.Length - 1; ++n) { warstwy[warstwy.Length - 1].neurony[n].calcOutputGradients(target[n]); } //liczenie gradientu poprzednich warstw for (int layerNum = warstwy.Length - 2; layerNum > 0; --layerNum) { Warstwa hiddenLayer = warstwy[layerNum]; Warstwa nextLayer = warstwy[layerNum + 1]; for (int n = 0; n < hiddenLayer.neurony.Length; ++n) { hiddenLayer.neurony[n].calcHiddenGradients(nextLayer); } } //updata wag na karzdym neuronie for (int layerNum = warstwy.Length - 1; layerNum > 0; --layerNum) { Warstwa layer = warstwy[layerNum]; Warstwa prevLayer = warstwy[layerNum - 1]; for (int n = 0; n < layer.neurony.Length - 1; ++n) { layer.neurony[n].updateInputWeights(prevLayer, eta, alpha); } } // Console.Write(error); //Console.WriteLine("/n"); }
double alpha; // szybkosc/moment uczenia public Siec(uint[] topologia) { eta = 0.15; alpha = 0.5; error = 0; warstwy = new Warstwa[topologia.Length]; for (int i = 0; i < topologia.Length; i++) { if (i < topologia.Length - 1) { warstwy[i] = new Warstwa(topologia[i] + 1, topologia[i + 1] + 1); } else { warstwy[i] = new Warstwa(topologia[i] + 1, 0); } } }
public void updateInputWeights(Warstwa prevLayer, double eta, double alpha) { // The weights to be updated are in the Connection container // in the neurons in the preceding layer for (int n = 0; n < prevLayer.neurony.Length; ++n) { Neuron neuron = prevLayer.neurony[n]; double oldDeltaWeight = neuron.delta[index]; double newDeltaWeight = // Individual input, magnified by the gradient and train rate: eta * neuron.output * gradient // Also add momentum = a fraction of the previous delta weight; + alpha * oldDeltaWeight; neuron.delta[index] = newDeltaWeight; neuron.wagi[index] += newDeltaWeight; } }
public void calcHiddenGradients(Warstwa nextLayer) { double dow = sumDOW(nextLayer); gradient = dow * transferFunctionDerivative(output); }