Exemplo n.º 1
0
 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);
 }
Exemplo n.º 2
0
        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;


        }
Exemplo n.º 3
0
        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;

        }