示例#1
0
            public void ConvolutionTest()
            {
                var input = new double[2][, ]
                {
                    new double[5, 5]
                    {
                        { 1, 1, 1, 0, 0 },
                        { 0, 1, 1, 1, 0 },
                        { 0, 0, 1, 1, 1 },
                        { 0, 0, 1, 1, 0 },
                        { 0, 1, 1, 0, 0 }
                    },
                    new double[5, 5]
                    {
                        { 1, 1, 1, 0, 0 },
                        { 0, 1, 1, 1, 0 },
                        { 0, 0, 1, 1, 1 },
                        { 0, 0, 1, 1, 0 },
                        { 0, 1, 1, 0, 0 }
                    },
                };

                var kernels = new double[2][, ]
                {
                    new double[3, 3]
                    {
                        { 1, 0, 1 },
                        { 0, 1, 0 },
                        { 1, 0, 1 }
                    },
                    new double[3, 3]
                    {
                        { 1, 0, 1 },
                        { 0, 1, 0 },
                        { 1, 0, 1 }
                    },
                };

                var expected = new double[][, ]
                {
                    new double[3, 3]
                    {
                        { 4, 3, 4 },
                        { 2, 4, 3 },
                        { 2, 3, 4 }
                    },
                    new double[3, 3]
                    {
                        { 4, 3, 4 },
                        { 2, 4, 3 },
                        { 2, 3, 4 }
                    },
                };

                var actual = MatrixProcessor.Convolute(input, kernels);

                Helper.CompareArrays(expected, actual);
            }
示例#2
0
        public override Value PassForward(Value value)
        {
            for (int i = 0; i < _kernels.Count; i++)
            {
                MatrixProcessor
                .Convolute(value.Multi, _kernels[i].Weights)
                .ForEach((q, j, k) => _featureMaps[i, j, k] = q);
            }

            return(new MultiValue(_featureMaps));
        }
示例#3
0
        public override Value PassBackward(Value value)
        {
            var output = new double[_inputeFm.Channels, _inputeFm.Size, _inputeFm.Size];

            for (int i = 0; i < _numberOfKernels; i++)
            {
                var kernel = value.Multi.GetSlice(i);
                kernel = MatrixProcessor.Pad(kernel, _kernelSize - 1);

                for (int j = 0; j < _inputeFm.Channels; j++)
                {
                    var weight = _kernels[i].Weights.GetSlice(j);
                    weight = MatrixProcessor.Flip(weight);
                    var conv = MatrixProcessor.Convolute(kernel, weight);

                    conv.ForEach((q, ii, jj) => output[j, ii, jj] += q);
                }
            }

            return(new MultiValue(output));
        }
示例#4
0
            public void NewConvolutionTest()
            {
                var input = new double[, , ]
                {
                    {
                        { -95, -38, 47, -65, 75 },
                        { -81, 34, -10, -32, -80 },
                        { -68, -14, -92, 11, -64 },
                        { -59, -74, -36, 67, 17 },
                        { 95, 86, -24, 44, 94 },
                    },
                    {
                        { -62, 78, -82, -17, 1 },
                        { 14, -78, 8, 56, 31 },
                        { -93, -76, 81, -98, -28 },
                        { 16, 60, 13, -94, 82 },
                        { 41, 30, 71, 2, 6 },
                    },
                };

                var k1 = new double[, , ]
                {
                    {
                        { 0, 1, 1 },
                        { -1, 0, 0 },
                        { -1, 1, -1 },
                    },
                    {
                        { 0, 0, -1 },
                        { 1, 1, 1 },
                        { 0, -1, -1 },
                    },
                };

                var k2 = new double[, , ]
                {
                    {
                        { 0, -1, 1 },
                        { -1, -1, -1 },
                        { -1, 1, 0 },
                    },
                    {
                        { 1, 1, 1 },
                        { -1, -1, 0 },
                        { -1, 1, 1 },
                    },
                };

                var k3 = new double[, , ]
                {
                    {
                        { -1, 0, 0 },
                        { 1, -1, -1 },
                        { 1, -1, -1 },
                    },
                    {
                        { 0, 0, -1 },
                        { 1, 0, -1 },
                        { -1, -1, -1 },
                    },
                };

                var e1 = new double[, ]
                {
                    { 257, -121, 407 },
                    { -56, -125, 2 },
                    { -125, -157, -22 }
                };

                var e2 = new double[, ]
                {
                    { 292, -74, -4 },
                    { 285, -49, 287 },
                    { -22, -87, -82 },
                };

                var e3 = new double[, ]
                {
                    { 204, 157, 37 },
                    { -101, -85, -72 },
                    { -68, 124, -310 },
                };

                MatrixProcessor.Convolute(input, k1)
                .ForEach((q, i, j) => Assert.AreEqual(q, e1[i, j]));

                MatrixProcessor.Convolute(input, k2)
                .ForEach((q, i, j) => Assert.AreEqual(q, e2[i, j]));

                MatrixProcessor.Convolute(input, k3)
                .ForEach((q, i, j) => Assert.AreEqual(q, e3[i, j]));
            }