コード例 #1
0
        private static void AlphabetRecognition()
        {
            double[][] testInput  = AlphabetData.Samples.Select(s => s.Input).ToArray();
            double[][] testOutput = AlphabetData.Samples.Select(s => new double[] { s.Output }).ToArray();

            BackPropNeuralNetwork network = new BackPropNeuralNetwork(15, 20, 1);

            network.Initialize();
            BackPropNeuralNetworkTrainer trainer = new BackPropNeuralNetworkTrainer(100000, 0.3, 0.1, maxAllowedError);

            if (trainer.TrainNetwork(network, testInput, testOutput))
            {
                string fileName = "AlphabetRecognition_" + DateTime.Now.ToString("yyMMdd-HHmmss") + ".nnetwork";
                using (Stream stream = File.Create(fileName))
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(stream, network);
                }

                BackPropNeuralNetwork newNetwork;
                using (Stream stream = File.OpenRead(fileName))
                {
                    BinaryFormatter formetter = new BinaryFormatter();
                    newNetwork = (BackPropNeuralNetwork)formetter.Deserialize(stream);
                }
            }
        }
コード例 #2
0
        public bool TrainNetwork(BackPropNeuralNetwork network, double[][] inputValues, double[][] targetValues)
        {
            // TODO: check array lenghts agains network params

            double[] currentResult;
            double   iterationError;

            double[]  currentInput;
            double[]  targetOutput;
            double[]  sampleErrors = new double[inputValues.Length];
            Stopwatch watch        = new Stopwatch();

            for (int iteration = 0; iteration < maxIterations; iteration++)
            {
                if (iteration % 20 == 0)
                {
                    watch.Restart();
                    // TODO: broadcast progress
                    Console.Write("Training iteration " + iteration + "...");
                    //Console.WriteLine("Errors: "+string.Join("  ", sampleErrors));
                }

                bool allClear = true;

                for (int sampleIndex = 0; sampleIndex < inputValues.Length; sampleIndex++)
                {
                    currentInput = inputValues[sampleIndex];
                    targetOutput = targetValues[sampleIndex];

                    currentResult             = network.Run(currentInput);
                    iterationError            = CalculateError(currentResult, targetOutput);
                    sampleErrors[sampleIndex] = iterationError;
                    if (iterationError >= this.maxAllowedError)
                    {
                        allClear = false;
                        network.UpdateWeights(targetOutput, this.learningRate, this.momentum);
                    }
                }

                if (iteration % 20 == 0)
                {
                    watch.Stop();
                    Console.WriteLine("\b\b\b took " + watch.Elapsed.ToString());
                }

                if (allClear)
                {
                    // TODO: success!
                    Console.WriteLine("Training complete at iteration " + iteration);
                    return(true);
                }
            }

            return(false);
        }