public void TestGaussianReset()
        {
            var strategy = ResetWeightMutationStrategy <double> .CreateGaussianResetStrategy(
                new SelectAllStrategy(),
                1.0, RandomDefaults.CreateRandomSource(0));

            int iters = 100000;

            double[] weightArr = new double[iters];
            for (int i = 0; i < iters; i++)
            {
                weightArr[i] = 123.0;
            }

            strategy.Invoke(weightArr);

            // Construct a histogram on the array of weights.
            HistogramData hist = NumericsUtils.BuildHistogramData(weightArr, 8);

            // We expect min and max to be close to be about -4.5 and +4.5 respectively
            // (but they could be higher in magnitude, with no bound).
            Assert.IsTrue(hist.Max >= 3.8);
            Assert.IsTrue(hist.Min <= -3.8);

            TestMean(weightArr);
            TestStandardDeviation(weightArr);
        }
示例#2
0
        public void UniformReset()
        {
            double weightScale = 5.0;
            var    strategy    = ResetWeightMutationStrategy <double> .CreateUniformResetStrategy(
                new SelectAllStrategy(),
                weightScale);

            IRandomSource rng = RandomDefaults.CreateRandomSource(0);

            int iters = 10_000;

            double[] weightArr = new double[iters];
            for (int i = 0; i < iters; i++)
            {
                weightArr[i] = 123.0;
            }

            strategy.Invoke(weightArr, rng);

            // Construct a histogram on the array of weights.
            HistogramData hist = NumericsUtils.BuildHistogramData(weightArr, 8);

            // We expect samples to be approximately evenly distributed over the histogram buckets.
            for (int i = 0; i < hist.FrequencyArray.Length; i++)
            {
                Assert.True(hist.FrequencyArray[i] > (iters / 8) * 0.8);
            }

            // We expect min and max to be close to -weightScale and +weightScale respectively.
            double delta = weightScale - hist.Max;

            Assert.True(delta >= 0.0 && delta < 0.1);

            delta = weightScale + hist.Min;
            Assert.True(delta >= 0.0 && delta < 0.1);

            // Mean should be near to zero.
            TestMean(weightArr);
        }