public double[] FeedForward(double[] x_inp) { Debug.Assert(x[0].Length == x_inp.Length + 1); x[0][0] = bias; for (int i = 0; i < x_inp.Length; i++) { x[0][i + 1] = x_inp[i]; } Debug.Assert(w.Length + 1 == x.Length); for (int lay = 0; lay < w.Length - 1; lay++) { x[lay + 1][0] = bias; for (int neu = 0; neu < w[lay].Length; neu++) { s[lay][neu] = MatrixArithmetics.Sum(MatrixArithmetics.PairMult(x[lay], w[lay][neu])); x[lay + 1][neu + 1] = F(s[lay][neu]); } } // the last layer has no bias in outputs int last = w.Length - 1; { for (int neu = 0; neu < w[last].Length; neu++) { s[last][neu] = MatrixArithmetics.Sum(MatrixArithmetics.PairMult(x[last], w[last][neu])); x[last + 1][neu] = F(s[last][neu]); } } return(x[x.Length - 1]); }
public static double CalculateError(double[] etalonOut, double[] sampleOut) { double[] D = MatrixArithmetics.Minus(sampleOut, etalonOut); return(MatrixArithmetics.Sum(MatrixArithmetics.PairMult(D, D))); }
public void Train(double[][] xCookedSamples, double[][] yCookedSamples) { int outputs = w[w.Length - 1].Length; //Debug.Assert(outputs == chars.Length); int P = xCookedSamples.GetLength(0); double netError; int epoch = 0; // ready to start do { double localEta = const_Eta; netError = 0; // passing all samples for (int p = 0; p < P; p++) { double[] y = FeedForward(xCookedSamples[p]); double[] D = MatrixArithmetics.Minus(y, yCookedSamples[p]); netError += MatrixArithmetics.Sum(MatrixArithmetics.PairMult(D, D)); // do the last layer int last = w.Length - 1; for (int j = 0; j < outputs; j++) { d[last][j] = D[j] * dF(s[last][j]); } // do inner layers for (int lay = last - 1; lay >= 0; lay--) { for (int j = 0; j < d[lay].Length; j++) { double sum_dw = 0; for (int k = 0; k < d[lay + 1].Length; k++) { sum_dw += d[lay + 1][k] * w[lay + 1][k][j]; } // d = dF(s) * Sum(d * w) d[lay][j] = dF(s[lay][j]) * sum_dw; } } for (int lay = 0; lay < w.Length; lay++) { for (int neu = 0; neu < w[lay].Length; neu++) { for (int i = 0; i < w[lay][neu].Length; i++) { w[lay][neu][i] -= localEta * d[lay][neu] * x[lay][i]; } } } } epoch++; //if (backgroundWorkerTrain.CancellationPending) // goto stopTrain; //if (epoch % 5 == 0) //{ // backgroundWorkerTrain.ReportProgress(epoch); // reportEpoc = epoch; // reportError = netError; //} }while (Math.Abs(netError) > eps && epoch < maxEpoch); //stopTrain: //reportEpoc = epoch; //reportError = netError; //Debug.Print("Trainig finished with Error: {0}, at epoch #{1}", netError, epoch); }