public static double ComputeOutput(double[] trainingData, double[] weight) { double z = 0.0; int i = 0; for (i = 0; i < trainingData.Length; i++) { z += trainingData[i] * weight[i]; } z += weight[i]; return SGDHlper.Output(z); }
public double[][] ComputeOutputForEachLayers(double[] trainingData) { var inputDataForLayers = new double[Weights.Length + 1][]; inputDataForLayers[0] = trainingData; var count = 0; Weights.ToList().ForEach(weights => { inputDataForLayers[count + 1] = weights.Select(x => SGDHlper.ComputeOutput(inputDataForLayers[count], x)).ToArray(); count++; }); return inputDataForLayers; }
public void Train(double[] trainingData, double[] expectedResuts) { var newWeights = new double[Weights.Length][][]; for (int i = 0; i < newWeights.Length; i++) { newWeights[i] = new double[Weights[i].Length][]; for (int j = 0; j < newWeights[i].Length; j++) { newWeights[i][j] = new double[Weights[i][j].Length]; for (int k = 0; k < newWeights[i][j].Length; k++) { newWeights[i][j][k] = Weights[i][j][k]; } } } var inputDataForLayers = new double[Weights.Length+1][]; var count = 0; inputDataForLayers[0] = trainingData; Weights.ToList().ForEach(y => { inputDataForLayers[count+1] = y.Select(x => SGDHlper.ComputeOutput(inputDataForLayers[count], x)).ToArray(); count++; }); var outputDelta = new double[expectedResuts.Length]; SGDHlper.TrainOutputLayer(LearningRate, inputDataForLayers[inputDataForLayers.Length-2], inputDataForLayers[inputDataForLayers.Length - 1], expectedResuts, newWeights[newWeights.Length - 1], outputDelta); var inNodeDelta = outputDelta; for (int i = newWeights.Length - 2; i >= 0; i--) { outputDelta = new double[newWeights[i].Length]; SGDHlper.TrainHiddenLayer(LearningRate, inputDataForLayers[i], inputDataForLayers[i+1], Weights[i + 1], newWeights[i], inNodeDelta, outputDelta); inNodeDelta = outputDelta; } Weights= newWeights; }