Ejemplo n.º 1
0
        public void ThresholdLayer_Forward()
        {
            var layer = new ThresholdLayer();

            layer.Setup(bottom, top);
            layer.Forward(bottom, top);

            Assert.Equal(bottom.Count, top.Count);

            using (var topCpu = top.OnCpu())
                using (var bottomCpu = bottom.OnCpu())
                {
                    int count = bottom.Count;
                    for (int i = 0; i < count; i++)
                    {
                        Assert.True(topCpu.DataAt(i) >= 0.0d);
                        Assert.True(topCpu.DataAt(i) <= 1.0d);

                        if (topCpu.DataAt(i) == 0.0d)
                        {
                            Assert.True(bottomCpu.DataAt(i) <= layer.Parameters.Threshold);
                        }
                        else if (topCpu.DataAt(i) == 1.0d)
                        {
                            Assert.True(bottomCpu.DataAt(i) > layer.Parameters.Threshold);
                        }
                        else
                        {
                            Assert.True(false);
                        }
                    }
                    ;
                }
        }
Ejemplo n.º 2
0
        public void PowerLayer_Forward(double power, double scale, double shift)
        {
            var config = new PowerLayerConfiguration(power, scale, shift);
            var layer  = new PowerLayer(config);

            layer.Setup(bottom, top);
            layer.Forward(bottom, top);

            using (var topCpu = top.OnCpu())
                using (var bottomCpu = bottom.OnCpu())
                {
                    double minPrecision = 1e-5f;

                    // Now, check values
                    int count = bottomCpu.Data.Count;
                    for (int i = 0; i < count; i++)
                    {
                        var expectedValue = Math.Pow(shift + scale * bottomCpu.DataAt(i), power);
                        if (power == 0 || power == 1 || power == 2)
                        {
                            Assert.False(double.IsNaN(topCpu.DataAt(i)));
                        }

                        if (double.IsNaN(expectedValue))
                        {
                            Assert.True(double.IsNaN(topCpu.DataAt(i)));
                        }
                        else
                        {
                            double precision = Math.Max(Math.Abs(expectedValue * 1e-4f), minPrecision);
                            Assert.True(MathHelpers.Equality(expectedValue, topCpu.DataAt(i), precision));
                        }
                    }
                }
        }
Ejemplo n.º 3
0
        public void DropoutLayer_ForwardTrainPhase(double ratio)
        {
            Context.Instance.Phase = PhaseType.Train;

            var config = new DropoutLayerConfiguration(ratio);
            var layer  = new DropoutLayer(config);

            layer.Setup(bottom, top);
            layer.Forward(bottom, top);

            Assert.Equal(bottom.Count, top.Count);

            using (var topCpu = top.OnCpu())
                using (var bottomCpu = bottom.OnCpu())
                {
                    double scale = 1f / (1f - layer.Parameters.Ratio);

                    int count = bottom.Count;
                    int kept  = 0;
                    for (int i = 0; i < count; i++)
                    {
                        if (!MathHelpers.Equality(topCpu.DataAt(i), 0))
                        {
                            kept++;
                            Assert.True(MathHelpers.Equality(topCpu.DataAt(i), bottomCpu.DataAt(i) * scale));
                        }
                    }
                    ;

                    double stdError = Math.Sqrt(ratio * (1 - ratio) / count);
                    double empiricalDropoutRatio = 1.0d - ((double)kept / count);

                    Assert.True(MathHelpers.Equality(ratio, empiricalDropoutRatio, 1.96 * stdError));
                }
        }
Ejemplo n.º 4
0
        public void Blob_Initialization()
        {
            var blob = new Tensor();
            Assert.Equal(0, blob.Num);
            Assert.Equal(0, blob.Count);
            Assert.Equal(0, blob.Channels);
            Assert.Equal(0, blob.Height);
            Assert.Equal(0, blob.Width);

            Assert.Throws<InvalidOperationException>(() => blob.OnCpu());
            // Assert.Throws<InvalidOperationException>(() => blob.Data);
            // Assert.Throws<InvalidOperationException>(() => blob.Diff);

            var preshapedBlob = new Tensor(2, 3, 4, 5);
            Assert.Equal(2, preshapedBlob.Num);
            Assert.Equal(3, preshapedBlob.Channels);
            Assert.Equal(4, preshapedBlob.Height);
            Assert.Equal(5, preshapedBlob.Width);
            Assert.Equal(120, preshapedBlob.Count);

            using (var preshapedBlobCpu = preshapedBlob.OnCpu())
            {
                Assert.NotNull(preshapedBlobCpu.Data);
                Assert.NotNull(preshapedBlobCpu.Diff);
                Assert.Equal(preshapedBlob.Count, preshapedBlobCpu.Data.Count);
                Assert.Equal(preshapedBlob.Count, preshapedBlobCpu.Diff.Count);
            }
        }
Ejemplo n.º 5
0
        public void AveragePoolingLayer_Forward()
        {
            var bottom = new Tensor(1, 1, 3, 3);

            var filler = new ConstantFiller(2.0d);

            filler.Fill(bottom);

            var layer = new AveragePoolingLayer(3, 1, 1);

            layer.Setup(bottom, top);

            Assert.Equal(1, top.Num);
            Assert.Equal(1, top.Channels);
            Assert.Equal(3, top.Height);
            Assert.Equal(3, top.Width);

            layer.Forward(bottom, top);

            using (var topCpu = top.OnCpu())
            {
                var topData = topCpu.Data;
                AssertInRange(8.0d / 9, topData[0]);
                AssertInRange(4.0d / 3, topData[1]);
                AssertInRange(8.0d / 9, topData[2]);
                AssertInRange(4.0d / 3, topData[3]);
                AssertInRange(2.0d, topData[4]);
                AssertInRange(4.0d / 3, topData[5]);
                AssertInRange(8.0d / 9, topData[6]);
                AssertInRange(4.0d / 3, topData[7]);
                AssertInRange(8.0d / 9, topData[8]);
            }
        }
Ejemplo n.º 6
0
        public void Blob_Initialization()
        {
            var blob = new Tensor();

            Assert.Equal(0, blob.Num);
            Assert.Equal(0, blob.Count);
            Assert.Equal(0, blob.Channels);
            Assert.Equal(0, blob.Height);
            Assert.Equal(0, blob.Width);

            Assert.Throws <InvalidOperationException>(() => blob.OnCpu());
            // Assert.Throws<InvalidOperationException>(() => blob.Data);
            // Assert.Throws<InvalidOperationException>(() => blob.Diff);

            var preshapedBlob = new Tensor(2, 3, 4, 5);

            Assert.Equal(2, preshapedBlob.Num);
            Assert.Equal(3, preshapedBlob.Channels);
            Assert.Equal(4, preshapedBlob.Height);
            Assert.Equal(5, preshapedBlob.Width);
            Assert.Equal(120, preshapedBlob.Count);

            using (var preshapedBlobCpu = preshapedBlob.OnCpu())
            {
                Assert.NotNull(preshapedBlobCpu.Data);
                Assert.NotNull(preshapedBlobCpu.Diff);
                Assert.Equal(preshapedBlob.Count, preshapedBlobCpu.Data.Count);
                Assert.Equal(preshapedBlob.Count, preshapedBlobCpu.Diff.Count);
            }
        }
Ejemplo n.º 7
0
        public void Filler_GaussianDense(double meanParam, double stdParam)
        {
            var blob   = new Tensor(2, 3, 4, 5);
            var config = new GaussianFillerConfiguration(meanParam, stdParam);
            var filler = new GaussianFiller(config);

            filler.Fill(blob);

            double mean = 0;
            double var  = 0;

            int count = blob.Count;

            using (var blobCpu = blob.OnCpu())
            {
                for (int i = 0; i < count; i++)
                {
                    mean += blobCpu.DataAt(i);
                    var  += (blobCpu.DataAt(i) - config.Mean) * (blobCpu.DataAt(i) - config.Mean);
                }

                mean /= count;
                var  /= count;

                Assert.True(mean >= config.Mean - config.Std * 5);
                Assert.True(mean <= config.Mean + config.Std * 5);

                double targetVar = config.Std * config.Std;
                Assert.True(var >= (targetVar / 5.0d));
                Assert.True(var <= (targetVar * 5.0d));
            }
        }
Ejemplo n.º 8
0
        public void Filler_GaussianDense(double meanParam, double stdParam)
        {
            var blob = new Tensor(2, 3, 4, 5);
            var config = new GaussianFillerConfiguration(meanParam, stdParam);
            var filler = new GaussianFiller(config);
            filler.Fill(blob);

            double mean = 0;
            double var = 0;

            int count = blob.Count;

            using (var blobCpu = blob.OnCpu())
            {
                for (int i = 0; i < count; i++)
                {
                    mean += blobCpu.DataAt(i);
                    var += (blobCpu.DataAt(i) - config.Mean) * (blobCpu.DataAt(i) - config.Mean);
                }

                mean /= count;
                var /= count;

                Assert.True(mean >= config.Mean - config.Std * 5);
                Assert.True(mean <= config.Mean + config.Std * 5);

                double targetVar = config.Std * config.Std;
                Assert.True(var >= (targetVar / 5.0d));
                Assert.True(var <= (targetVar * 5.0d));
            }
        }
Ejemplo n.º 9
0
        public void Filler_PositiveUnitball()
        {
            var blob   = new Tensor(2, 3, 4, 5);
            var filler = new PositiveUnitballFiller();

            filler.Fill(blob);

            int num   = blob.Num;
            int count = blob.Count;
            int dim   = count / num;

            using (var blobCpu = blob.OnCpu())
            {
                var data = blobCpu.Data;
                for (int i = 0; i < count; i++)
                {
                    Assert.True(data[i] >= 0.0d);
                    Assert.True(data[i] <= 1.0d);
                }

                for (int i = 0; i < num; i++)
                {
                    double sum = 0;
                    for (int j = 0; j < dim; j++)
                    {
                        sum += blobCpu.DataAt(i * dim + j);
                    }

                    Assert.True(sum >= 0.999f);
                    Assert.True(sum <= 1.001f);
                }
            }
        }
Ejemplo n.º 10
0
        public void TanhLayer_Forward()
        {
            var layer = new TanhLayer();

            layer.Setup(bottom, top);
            layer.Forward(bottom, top);

            Assert.Equal(bottom.Count, top.Count);

            using (var topCpu = top.OnCpu())
                using (var bottomCpu = bottom.OnCpu())
                {
                    int count = bottom.Count;

                    for (int i = 0; i < bottom.Num; i++)
                    {
                        for (int j = 0; j < bottom.Channels; j++)
                        {
                            for (int k = 0; k < bottom.Height; k++)
                            {
                                for (int l = 0; l < bottom.Width; l++)
                                {
                                    var v = (Math.Exp(2 * bottomCpu.DataAt(i, j, k, l)) - 1) / (Math.Exp(2 * bottomCpu.DataAt(i, j, k, l)) + 1);
                                    Assert.True(MathHelpers.Equality(topCpu.DataAt(i, j, k, l), v, 1e-4f));
                                }
                            }
                        }
                    }
                }
        }
Ejemplo n.º 11
0
        public void ReluLayer_Forward()
        {
            var layer = new ReluLayer();

            layer.Setup(bottom, top);
            layer.Forward(bottom, top);

            Assert.Equal(bottom.Count, top.Count);

            using (var topCpu = top.OnCpu())
                using (var bottomCpu = bottom.OnCpu())
                {
                    int count = bottom.Count;
                    for (int i = 0; i < count; i++)
                    {
                        Assert.True(topCpu.DataAt(i) >= 0.0d);
                        Assert.True(topCpu.DataAt(i) == 0.0d || topCpu.DataAt(i) == bottomCpu.DataAt(i));
                    }
                    ;
                }
        }
        public void StocasticPoolingLayer_Forward()
        {
            Context.Instance.Phase = PhaseType.Train;

            var layer = new StocasticPoolingLayer(3, 2);

            layer.Setup(bottom, top);
            layer.Forward(bottom, top);

            int num      = top.Num;
            int channels = top.Channels;
            int height   = top.Height;
            int width    = top.Width;

            using (var topCpu = top.OnCpu())
                using (var bottomCpu = bottom.OnCpu())
                {
                    var topData    = topCpu.Data;
                    var bottomData = bottomCpu.Data;

                    for (int n = 0; n < num; n++)
                    {
                        for (int c = 0; c < channels; c++)
                        {
                            for (int ph = 0; ph < height; ph++)
                            {
                                for (int pw = 0; pw < width; pw++)
                                {
                                    double pooled = topData[topCpu.Offset(n, c, ph, pw)];

                                    int hstart = ph * 2;
                                    int hend   = Math.Min(hstart + 3, bottom.Height);
                                    int wstart = pw * 2;
                                    int wend   = Math.Min(wstart + 3, bottom.Width);

                                    bool hasEqual = false;
                                    for (int h = hstart; h < hend; ++h)
                                    {
                                        for (int w = wstart; w < wend; ++w)
                                        {
                                            hasEqual |= (pooled == bottomData[bottomCpu.Offset(n, c, h, w)]);
                                        }
                                    }
                                    Assert.True(hasEqual);
                                }
                            }
                        }
                    }
                }
        }
Ejemplo n.º 13
0
        public void Filler_Constant()
        {
            var blob = new Tensor(2, 3, 4, 5);

            var config = new ConstantFillerConfiguration(10.0d);
            var filler = new ConstantFiller(config);
            filler.Fill(blob);

            using (var blobCpu = blob.OnCpu())
            {
                int count = blobCpu.Count;
                var data = blobCpu.Data;
                for (int i = 0; i < count; i++)
                    Assert.Equal(data[i], 10.0d);
            }
        }
        public SoftmaxLossLayerTests()
        {
            var filler = new GaussianFiller();

            filler.Fill(bottom);

            Random rnd = new Random(1000);

            using (var labelsCpu = labels.OnCpu())
            {
                var labelData = labelsCpu.Data;
                for (int i = 0; i < labelData.Count; i++)
                {
                    labelData[i] = rnd.Next(5);
                }
            }
        }
Ejemplo n.º 15
0
        public void Blob_Reshape()
        {
            var blob = new Tensor();
            blob.Reshape(2, 3, 4, 5);
            Assert.Equal(2, blob.Num);
            Assert.Equal(3, blob.Channels);
            Assert.Equal(4, blob.Height);
            Assert.Equal(5, blob.Width);
            Assert.Equal(120, blob.Count);

            using (var blobCpu = blob.OnCpu())
            {
                Assert.NotNull(blobCpu.Data);
                Assert.NotNull(blobCpu.Diff);
                Assert.Equal(blobCpu.Count, blobCpu.Data.Count);
                Assert.Equal(blobCpu.Count, blobCpu.Diff.Count);
            }
        }
Ejemplo n.º 16
0
        public void Filler_GaussianSparse(double meanParam, double stdParam)
        {
            var blob   = new Tensor(2, 3, 4, 5);
            var config = new GaussianFillerConfiguration(meanParam, stdParam)
            {
                IsSparse = true
            };

            var filler = new GaussianFiller(config);

            filler.Fill(blob);

            double mean = 0;
            double var  = 0;

            using (var blobCpu = blob.OnCpu())
            {
                int count  = blob.Count;
                int zeroes = 0;

                for (int i = 0; i < count; i++)
                {
                    if (blobCpu.DataAt(i) == 0.0d)
                    {
                        zeroes++;
                    }
                    else
                    {
                        mean += blobCpu.DataAt(i);
                        var  += (blobCpu.DataAt(i) - config.Mean) * (blobCpu.DataAt(i) - config.Mean);
                    }
                }

                mean /= (count - zeroes);
                var  /= (count - zeroes);

                Assert.True(mean >= config.Mean - config.Std * 5);
                Assert.True(mean <= config.Mean + config.Std * 5);

                double targetVar = config.Std * config.Std;
                Assert.True(var >= (targetVar / 5.0d));
                Assert.True(var <= (targetVar * 5.0d));
            }
        }
Ejemplo n.º 17
0
        public void SoftmaxLayer_Forward()
        {
            var layer = new SoftmaxLayer();

            layer.Setup(bottom, top);
            layer.Forward(bottom, top);

            Assert.Equal(bottom.Count, top.Count);

            using (var topCpu = top.OnCpu())
                using (var bottomCpu = bottom.OnCpu())
                {
                    int count = bottom.Count;

                    int num      = bottom.Num;
                    int channels = bottom.Channels;
                    for (int i = 0; i < num; i++)
                    {
                        double sum = 0;
                        for (int j = 0; j < channels; j++)
                        {
                            sum += topCpu.DataAt(i, j, 0, 0);
                        }

                        Assert.True(sum >= 0.999);
                        Assert.True(sum <= 1.001);
                    }

                    for (int i = 0; i < num; i++)
                    {
                        double scale = 0;
                        for (int j = 0; j < channels; j++)
                        {
                            scale += Math.Exp(bottomCpu.DataAt(i, j, 0, 0));
                        }

                        for (int j = 0; j < channels; j++)
                        {
                            Assert.True(topCpu.DataAt(i, j, 0, 0) + 1e-4f >= Math.Exp(bottomCpu.DataAt(i, j, 0, 0)) / scale);
                            Assert.True(topCpu.DataAt(i, j, 0, 0) - 1e-4f <= Math.Exp(bottomCpu.DataAt(i, j, 0, 0)) / scale);
                        }
                    }
                }
        }
Ejemplo n.º 18
0
        public void Filler_Uniform(double min, double max)
        {
            var blob   = new Tensor(2, 3, 4, 5);
            var config = new UniformFillerConfiguration(min, max);
            var filler = new UniformFiller(config);

            filler.Fill(blob);

            using (var blobCpu = blob.OnCpu())
            {
                int count = blob.Count;
                var data  = blobCpu.Data;
                for (int i = 0; i < count; i++)
                {
                    Assert.True(data[i] >= min);
                    Assert.True(data[i] <= max);
                }
            }
        }
Ejemplo n.º 19
0
        public void Filler_Constant()
        {
            var blob = new Tensor(2, 3, 4, 5);

            var config = new ConstantFillerConfiguration(10.0d);
            var filler = new ConstantFiller(config);

            filler.Fill(blob);

            using (var blobCpu = blob.OnCpu())
            {
                int count = blobCpu.Count;
                var data  = blobCpu.Data;
                for (int i = 0; i < count; i++)
                {
                    Assert.Equal(data[i], 10.0d);
                }
            }
        }
Ejemplo n.º 20
0
        public void Blob_Reshape()
        {
            var blob = new Tensor();

            blob.Reshape(2, 3, 4, 5);
            Assert.Equal(2, blob.Num);
            Assert.Equal(3, blob.Channels);
            Assert.Equal(4, blob.Height);
            Assert.Equal(5, blob.Width);
            Assert.Equal(120, blob.Count);

            using (var blobCpu = blob.OnCpu())
            {
                Assert.NotNull(blobCpu.Data);
                Assert.NotNull(blobCpu.Diff);
                Assert.Equal(blobCpu.Count, blobCpu.Data.Count);
                Assert.Equal(blobCpu.Count, blobCpu.Diff.Count);
            }
        }
Ejemplo n.º 21
0
        public void Filler_Xavier()
        {
            var blob   = new Tensor(2, 3, 4, 5);
            var filler = new XavierFiller();

            filler.Fill(blob);

            int    fanIn = blob.Count / blob.Num;
            double scale = Math.Sqrt(3 / fanIn);


            using (var blobCpu = blob.OnCpu())
            {
                int count = blob.Count;
                var data  = blobCpu.Data;
                for (int i = 0; i < count; i++)
                {
                    Assert.True(data[i] >= -scale);
                    Assert.True(data[i] <= scale);
                }
            }
        }
Ejemplo n.º 22
0
        public void InnerProductLayer_Forward()
        {
            var weightsFiller = new UniformFillerConfiguration(0, 1);
            var biasFiller    = new UniformFillerConfiguration(1, 2);

            var config = new InnerProductLayerConfiguration(10, true, weightsFiller, biasFiller);

            var layer = new InnerProductLayer(config);

            layer.Setup(bottom, top);
            layer.Forward(bottom, top);

            using (var topCpu = top.OnCpu())
            {
                int count = top.Count;

                for (int i = 0; i < count; i++)
                {
                    Assert.True(topCpu.DataAt(i) >= 1f);
                }
            }
        }
Ejemplo n.º 23
0
        public void SigmoidLayer_Forward()
        {
            var layer = new SigmoidLayer();

            layer.Setup(bottom, top);
            layer.Forward(bottom, top);

            Assert.Equal(bottom.Count, top.Count);

            using (var topCpu = top.OnCpu())
                using (var bottomCpu = bottom.OnCpu())
                {
                    int count = bottom.Count;
                    for (int i = 0; i < count; i++)
                    {
                        Assert.True(MathHelpers.Equality(topCpu.DataAt(i), 1.0d / (1.0d + Math.Exp(-bottomCpu.DataAt(i)))));

                        // check that we squashed the value between 0 and 1
                        Assert.True(topCpu.DataAt(i) >= 0.0d);
                        Assert.True(topCpu.DataAt(i) <= 1.0d);
                    }
                    ;
                }
        }
Ejemplo n.º 24
0
        public void Filler_GaussianSparse(double meanParam, double stdParam)
        {
            var blob = new Tensor(2, 3, 4, 5);
            var config = new GaussianFillerConfiguration(meanParam, stdParam) { IsSparse = true };

            var filler = new GaussianFiller(config);
            filler.Fill(blob);

            double mean = 0;
            double var = 0;

            using (var blobCpu = blob.OnCpu())
            {
                int count = blob.Count;
                int zeroes = 0;

                for (int i = 0; i < count; i++)
                {
                    if (blobCpu.DataAt(i) == 0.0d)
                    {
                        zeroes++;
                    }
                    else
                    {
                        mean += blobCpu.DataAt(i);
                        var += (blobCpu.DataAt(i) - config.Mean) * (blobCpu.DataAt(i) - config.Mean);
                    }
                }

                mean /= (count - zeroes);
                var /= (count - zeroes);

                Assert.True(mean >= config.Mean - config.Std * 5);
                Assert.True(mean <= config.Mean + config.Std * 5);

                double targetVar = config.Std * config.Std;
                Assert.True(var >= (targetVar / 5.0d));
                Assert.True(var <= (targetVar * 5.0d));
            }
        }
Ejemplo n.º 25
0
        public void MaxPoolingLayer_ForwardRectangularWithSquareKernel(int topLayer)
        {
            Contract.Requires(topLayer > 0);

            const int num = 2;
            const int channels = 2;
            var bottom = new Tensor(num, channels, 3, 5);

            var topList = new Tensor[topLayer];
            for (int i = 0; i < topLayer; i++)
                topList[i] = new Tensor();

            // Input: 2x 2 channels of:
            //     [1 2 5 2 3]
            //     [9 4 1 4 8]
            //     [1 2 5 2 3]

            using (var bottomCpu = bottom.OnCpu())
            {
                var bottomData = bottomCpu.Data;
                for (int i = 0; i < 15 * num * channels; i += 15)
                {
                    bottomData[i + 0] = 1;
                    bottomData[i + 1] = 2;
                    bottomData[i + 2] = 5;
                    bottomData[i + 3] = 2;
                    bottomData[i + 4] = 3;
                    bottomData[i + 5] = 9;
                    bottomData[i + 6] = 4;
                    bottomData[i + 7] = 1;
                    bottomData[i + 8] = 4;
                    bottomData[i + 9] = 8;
                    bottomData[i + 10] = 1;
                    bottomData[i + 11] = 2;
                    bottomData[i + 12] = 5;
                    bottomData[i + 13] = 2;
                    bottomData[i + 14] = 3;
                }
            }

            var layer = new MaxPoolingLayer(2, 1, 0);
            layer.Setup(new TensorCollection { bottom }, topList);

            foreach ( var top in topList)
            {
                Assert.Equal(num, top.Num);
                Assert.Equal(channels, top.Channels);
                Assert.Equal(2, top.Height);
                Assert.Equal(4, top.Width);
            }

            layer.Forward(new TensorCollection { bottom }, topList);

            // Expected output: 2x 2 channels of:
            //     [9 5 5 8]
            //     [9 5 5 8]
            for (int i = 0; i < 8 * num * channels; i += 8)
            {
                using (var topCpu = topList[0].OnCpu())
                {
                    var topData = topCpu.Data;
                    Assert.Equal(9, topData[i + 0]);
                    Assert.Equal(5, topData[i + 1]);
                    Assert.Equal(5, topData[i + 2]);
                    Assert.Equal(8, topData[i + 3]);
                    Assert.Equal(9, topData[i + 4]);
                    Assert.Equal(5, topData[i + 5]);
                    Assert.Equal(5, topData[i + 6]);
                    Assert.Equal(8, topData[i + 7]);
                }
            }

            if ( topList.Length > 1 )
            {
                // Expected mask output: 2x 2 channels of:
                //     [5  2  2 9]
                //     [5 12 12 9]
                for (int i = 0; i < 8 * num * channels; i += 8)
                {
                    using (var topCpu = topList[1].OnCpu())
                    {
                        var topData = topCpu.Data;
                        Assert.Equal(5, topData[i + 0]);
                        Assert.Equal(2, topData[i + 1]);
                        Assert.Equal(2, topData[i + 2]);
                        Assert.Equal(9, topData[i + 3]);
                        Assert.Equal(5, topData[i + 4]);
                        Assert.Equal(12, topData[i + 5]);
                        Assert.Equal(12, topData[i + 6]);
                        Assert.Equal(9, topData[i + 7]);
                    }
                }
            }
        }
Ejemplo n.º 26
0
        public void MaxPoolingLayer_ForwardRectangularWithSquareKernel(int topLayer)
        {
            Contract.Requires(topLayer > 0);

            const int num      = 2;
            const int channels = 2;
            var       bottom   = new Tensor(num, channels, 3, 5);

            var topList = new Tensor[topLayer];

            for (int i = 0; i < topLayer; i++)
            {
                topList[i] = new Tensor();
            }

            // Input: 2x 2 channels of:
            //     [1 2 5 2 3]
            //     [9 4 1 4 8]
            //     [1 2 5 2 3]

            using (var bottomCpu = bottom.OnCpu())
            {
                var bottomData = bottomCpu.Data;
                for (int i = 0; i < 15 * num * channels; i += 15)
                {
                    bottomData[i + 0]  = 1;
                    bottomData[i + 1]  = 2;
                    bottomData[i + 2]  = 5;
                    bottomData[i + 3]  = 2;
                    bottomData[i + 4]  = 3;
                    bottomData[i + 5]  = 9;
                    bottomData[i + 6]  = 4;
                    bottomData[i + 7]  = 1;
                    bottomData[i + 8]  = 4;
                    bottomData[i + 9]  = 8;
                    bottomData[i + 10] = 1;
                    bottomData[i + 11] = 2;
                    bottomData[i + 12] = 5;
                    bottomData[i + 13] = 2;
                    bottomData[i + 14] = 3;
                }
            }

            var layer = new MaxPoolingLayer(2, 1, 0);

            layer.Setup(new TensorCollection {
                bottom
            }, topList);

            foreach (var top in topList)
            {
                Assert.Equal(num, top.Num);
                Assert.Equal(channels, top.Channels);
                Assert.Equal(2, top.Height);
                Assert.Equal(4, top.Width);
            }

            layer.Forward(new TensorCollection {
                bottom
            }, topList);

            // Expected output: 2x 2 channels of:
            //     [9 5 5 8]
            //     [9 5 5 8]
            for (int i = 0; i < 8 * num * channels; i += 8)
            {
                using (var topCpu = topList[0].OnCpu())
                {
                    var topData = topCpu.Data;
                    Assert.Equal(9, topData[i + 0]);
                    Assert.Equal(5, topData[i + 1]);
                    Assert.Equal(5, topData[i + 2]);
                    Assert.Equal(8, topData[i + 3]);
                    Assert.Equal(9, topData[i + 4]);
                    Assert.Equal(5, topData[i + 5]);
                    Assert.Equal(5, topData[i + 6]);
                    Assert.Equal(8, topData[i + 7]);
                }
            }

            if (topList.Length > 1)
            {
                // Expected mask output: 2x 2 channels of:
                //     [5  2  2 9]
                //     [5 12 12 9]
                for (int i = 0; i < 8 * num * channels; i += 8)
                {
                    using (var topCpu = topList[1].OnCpu())
                    {
                        var topData = topCpu.Data;
                        Assert.Equal(5, topData[i + 0]);
                        Assert.Equal(2, topData[i + 1]);
                        Assert.Equal(2, topData[i + 2]);
                        Assert.Equal(9, topData[i + 3]);
                        Assert.Equal(5, topData[i + 4]);
                        Assert.Equal(12, topData[i + 5]);
                        Assert.Equal(12, topData[i + 6]);
                        Assert.Equal(9, topData[i + 7]);
                    }
                }
            }
        }
Ejemplo n.º 27
0
        public void MaxPoolingLayer_BackwardsRectangularWithSquareKernel(int topLayer)
        {
            Contract.Requires(topLayer > 0);

            const int num      = 2;
            const int channels = 2;
            var       bottom   = new Tensor(num, channels, 3, 5);

            var topList = new Tensor[topLayer];

            for (int i = 0; i < topLayer; i++)
            {
                topList[i] = new Tensor();
            }

            var filler = new ConstantFiller(2);

            filler.Fill(bottom);

            // Input: 2x 2 channels of:
            //     [2 2 2 2 2]
            //     [2 2 2 2 2]
            //     [2 2 2 2 2]

            var layer = new MaxPoolingLayer(2, 1, 0);

            layer.Setup(new TensorCollection {
                bottom
            }, topList);
            layer.Forward(new TensorCollection {
                bottom
            }, topList);

            // Input: 2x 2 channels of:
            //     [1 1 1 1]
            //     [0 0 0 0]


            using (var topCpu = topList[0].OnCpu())
            {
                var topDiff = topCpu.Diff;
                for (int i = 0; i < 8 * num * channels; i += 8)
                {
                    topDiff[i + 0] = 1;
                    topDiff[i + 1] = 1;
                    topDiff[i + 2] = 1;
                    topDiff[i + 3] = 1;
                }
            }

            // Input: 2x 2 channels of:
            //     [1 2 2 2 1]
            //     [1 2 2 2 1]
            //     [0 0 0 0 0]

            layer.Backward(topList, new[] { true }, new TensorCollection {
                bottom
            });

            using (var bottomCpu = bottom.OnCpu())
            {
                var bottomDiff = bottomCpu.Diff;
                for (int i = 0; i < 15 * num * channels; i += 15)
                {
                    Assert.Equal(1, bottomDiff[i + 0]);
                    Assert.Equal(2, bottomDiff[i + 1]);
                    Assert.Equal(2, bottomDiff[i + 2]);
                    Assert.Equal(2, bottomDiff[i + 3]);
                    Assert.Equal(1, bottomDiff[i + 4]);
                    Assert.Equal(1, bottomDiff[i + 5]);
                    Assert.Equal(2, bottomDiff[i + 6]);
                    Assert.Equal(2, bottomDiff[i + 7]);
                    Assert.Equal(2, bottomDiff[i + 8]);
                    Assert.Equal(1, bottomDiff[i + 9]);
                    Assert.Equal(0, bottomDiff[i + 10]);
                    Assert.Equal(0, bottomDiff[i + 11]);
                    Assert.Equal(0, bottomDiff[i + 12]);
                    Assert.Equal(0, bottomDiff[i + 13]);
                    Assert.Equal(0, bottomDiff[i + 14]);
                }
            }
        }
Ejemplo n.º 28
0
        public void Filler_Uniform(double min, double max)
        {
            var blob = new Tensor(2, 3, 4, 5);
            var config = new UniformFillerConfiguration(min, max);
            var filler = new UniformFiller(config);
            filler.Fill(blob);

            using (var blobCpu = blob.OnCpu())
            {
                int count = blob.Count;
                var data = blobCpu.Data;
                for (int i = 0; i < count; i++)
                {
                    Assert.True(data[i] >= min);
                    Assert.True(data[i] <= max);
                }
            }
        }
Ejemplo n.º 29
0
        public void Filler_Xavier()
        {
            var blob = new Tensor(2, 3, 4, 5);
            var filler = new XavierFiller();
            filler.Fill(blob);

            int fanIn = blob.Count / blob.Num;
            double scale = Math.Sqrt(3 / fanIn);

            using (var blobCpu = blob.OnCpu())
            {
                int count = blob.Count;
                var data = blobCpu.Data;
                for (int i = 0; i < count; i++)
                {
                    Assert.True(data[i] >= -scale);
                    Assert.True(data[i] <= scale);
                }
            }
        }
Ejemplo n.º 30
0
        public void Filler_PositiveUnitball()
        {
            var blob = new Tensor(2, 3, 4, 5);
            var filler = new PositiveUnitballFiller();
            filler.Fill(blob);

            int num = blob.Num;
            int count = blob.Count;
            int dim = count / num;

            using (var blobCpu = blob.OnCpu())
            {
                var data = blobCpu.Data;
                for (int i = 0; i < count; i++)
                {
                    Assert.True(data[i] >= 0.0d);
                    Assert.True(data[i] <= 1.0d);
                }

                for (int i = 0; i < num; i++)
                {
                    double sum = 0;
                    for (int j = 0; j < dim; j++)
                        sum += blobCpu.DataAt(i * dim + j);

                    Assert.True(sum >= 0.999f);
                    Assert.True(sum <= 1.001f);
                }
            }
        }
Ejemplo n.º 31
0
        public void MaxPoolingLayer_BackwardsRectangularWithSquareKernel(int topLayer)
        {
            Contract.Requires(topLayer > 0);

            const int num = 2;
            const int channels = 2;
            var bottom = new Tensor(num, channels, 3, 5);

            var topList = new Tensor[topLayer];
            for (int i = 0; i < topLayer; i++)
                topList[i] = new Tensor();

            var filler = new ConstantFiller(2);
            filler.Fill(bottom);

            // Input: 2x 2 channels of:
            //     [2 2 2 2 2]
            //     [2 2 2 2 2]
            //     [2 2 2 2 2]

            var layer = new MaxPoolingLayer(2, 1, 0);
            layer.Setup(new TensorCollection { bottom }, topList);
            layer.Forward(new TensorCollection { bottom }, topList);

            // Input: 2x 2 channels of:
            //     [1 1 1 1]
            //     [0 0 0 0]

            using (var topCpu = topList[0].OnCpu())
            {
                var topDiff = topCpu.Diff;
                for (int i = 0; i < 8 * num * channels; i += 8)
                {

                    topDiff[i + 0] = 1;
                    topDiff[i + 1] = 1;
                    topDiff[i + 2] = 1;
                    topDiff[i + 3] = 1;
                }
            }

            // Input: 2x 2 channels of:
            //     [1 2 2 2 1]
            //     [1 2 2 2 1]
            //     [0 0 0 0 0]

            layer.Backward(topList, new[] { true }, new TensorCollection { bottom });

            using (var bottomCpu = bottom.OnCpu())
            {
                var bottomDiff = bottomCpu.Diff;
                for (int i = 0; i < 15 * num * channels; i += 15)
                {
                    Assert.Equal(1, bottomDiff[i + 0]);
                    Assert.Equal(2, bottomDiff[i + 1]);
                    Assert.Equal(2, bottomDiff[i + 2]);
                    Assert.Equal(2, bottomDiff[i + 3]);
                    Assert.Equal(1, bottomDiff[i + 4]);
                    Assert.Equal(1, bottomDiff[i + 5]);
                    Assert.Equal(2, bottomDiff[i + 6]);
                    Assert.Equal(2, bottomDiff[i + 7]);
                    Assert.Equal(2, bottomDiff[i + 8]);
                    Assert.Equal(1, bottomDiff[i + 9]);
                    Assert.Equal(0, bottomDiff[i + 10]);
                    Assert.Equal(0, bottomDiff[i + 11]);
                    Assert.Equal(0, bottomDiff[i + 12]);
                    Assert.Equal(0, bottomDiff[i + 13]);
                    Assert.Equal(0, bottomDiff[i + 14]);
                }
            }
        }