Beispiel #1
0
        private static float performEpoch(UnifiedCameraNeuralNetwork ann,
                                          int numOfSamples, float[] samples, int numOfTests, float[] tests,
                                          DrawingGroup drawingGroup)
        {
            int failedTests = 0;

            using (DrawingContext dc = drawingGroup.Open())
            {
                // Draw a transparent background to set the render size
                float RenderWidth  = 640f;
                float RenderHeight = 480f;
                dc.DrawRectangle(Brushes.Black, null, new System.Windows.Rect(0.0, 0.0, RenderWidth, RenderHeight));
                drawingGroup.ClipGeometry = new RectangleGeometry(new System.Windows.Rect(0.0, 0.0, RenderWidth, RenderHeight));

                for (int i = 0; i < numOfSamples; i++)
                {
                    float   x        = samples[i];
                    float[] input    = new float[1];
                    float[] expected = new float[1];
                    input[0]    = (float)x;
                    expected[0] = normalizeFunc(x); // Normalize
                    ann.train(input, expected);

                    if (i % 10000 == 9999)
                    {
                        Console.WriteLine((i + 1) + " samples trained..");
                    }
                }

                if (ann.Stochastic)
                {
                    ann.forceWeightUpdates();
                }

                for (int i = 0; i < numOfTests; i++)
                {
                    float   x     = tests[i];
                    float[] input = new float[1];
                    input[0] = (float)x;
                    float result     = approximationFunc(x);
                    float prediction = ann.feedForward(input)[0, 0];

                    if (float.IsNaN(prediction))
                    {
                        throw new InvalidOperationException("ANN returned a NaN output.");
                    }

                    prediction = denormalizeFunc(prediction); // Denormalize

                    // Draw
                    int   brushSize   = 2;
                    float xPlot       = (x / (maxRange - minRange)) * RenderWidth;
                    float funcPlot    = RenderHeight - normalizeFunc(x) * RenderHeight;
                    float predictPlot = RenderHeight - normalize(prediction) * RenderHeight;
                    dc.DrawEllipse(groundTruthBrush, null, new System.Windows.Point(xPlot, funcPlot), brushSize, brushSize);
                    dc.DrawEllipse(predictionBrush, null, new System.Windows.Point(xPlot, predictPlot), brushSize, brushSize);

                    float diff = Math.Abs(result - prediction);

                    if (diff > 0.0001)
                    {
                        failedTests++;
                    }
                }
            }

            Console.WriteLine("Total failed tests: " + failedTests);
            float successRate = 1.0f - (float)((float)failedTests / (float)numOfTests);

            Console.WriteLine("Success rate: " + successRate);

            return(successRate);
        }