diff --git a/DeepLearning/Program.cs b/DeepLearning/Program.cs index 5e40965..a253c0b 100644 --- a/DeepLearning/Program.cs +++ b/DeepLearning/Program.cs @@ -27,14 +27,14 @@ static void Main(string[] args) // XOR output, cooresponding to the input. double[][] outputs = new double[][] { - new double[] { 1, 0 }, - new double[] { 0, 1 }, - new double[] { 0, 1 }, - new double[] { 0, 1 } + new double[] { 0 }, + new double[] { 1 }, + new double[] { 1 }, + new double[] { 0 } }; - // Setup the deep belief network and initialize with random weights. - DeepBeliefNetwork network = new DeepBeliefNetwork(2, 2); + // Setup the deep belief network (2 inputs, 3 hidden, 1 output) and initialize with random weights. + DeepBeliefNetwork network = new DeepBeliefNetwork(2, 3, 1); new GaussianWeights(network, 0.1).Randomize(); network.UpdateVisibleWeights(); @@ -57,12 +57,12 @@ static void Main(string[] args) // Learning data for the specified layer. double[][][] layerData; - // Unsupervised learning on each layer. - for (int layerIndex = 0; layerIndex < network.Machines.Count; layerIndex++) + // Unsupervised learning on each hidden layer, except for the output. + for (int layerIndex = 0; layerIndex < network.Machines.Count - 1; layerIndex++) { teacher.LayerIndex = layerIndex; layerData = teacher.GetLayerInput(batches); - for (int i = 0; i < 500; i++) + for (int i = 0; i < 5000; i++) { double error = teacher.RunEpoch(layerData) / inputs.Length; if (i % 10 == 0) @@ -94,7 +94,9 @@ static void Main(string[] args) for (int i = 0; i < inputs.Length; i++) { double[] outputValues = network.Compute(inputs[i]); - if (FormatOutputResult(outputValues) == FormatOutputResult(outputs[i])) + double outputResult = outputValues.First() >= 0.5 ? 1 : 0; + + if (outputResult == outputs[i].First()) { correct++; } @@ -104,43 +106,5 @@ static void Main(string[] args) Console.Write("Press any key to quit .."); Console.ReadKey(); } - - #region Utility Methods - - /// - /// Converts a numeric output label (0, 1, 2, 3, etc) to its cooresponding array of doubles, where all values are 0 except for the index matching the label (ie., if the label is 2, the output is [0, 0, 1, 0, 0, ...]). - /// - /// double - /// double[] - private static double[] FormatOutputVector(double label) - { - double[] output = new double[10]; - - for (int i = 0; i < output.Length; i++) - { - if (i == label) - { - output[i] = 1; - } - else - { - output[i] = 0; - } - } - - return output; - } - - /// - /// Finds the largest output value in an array and returns its index. This allows for sequential classification from the outputs of a neural network (ie., if output at index 2 is the largest, the classification is class "3" (zero-based)). - /// - /// double[] - /// double - private static double FormatOutputResult(double[] output) - { - return output.ToList().IndexOf(output.Max()); - } - - #endregion } }