public void ExecuteTest()
        {
            float max_err = 0;

            Random rd = new Random(1234);

            foreach (int batch in new int[] { 1, 2 })
            {
                foreach (int channels in new int[] { 1, 2, 3, 4, 5, 6, 7, 8 })
                {
                    foreach (int stride in new int[] { 2, 3, 4 })
                    {
                        foreach (int inwidth in new int[] { 5, 7, 11 })
                        {
                            foreach (int inheight in new int[] { 5, 7, 11 })
                            {
                                int outwidth = inwidth / stride, outheight = inheight / stride;

                                float[] xval  = (new float[inwidth * inheight * channels * batch]).Select((_) => (float)rd.NextDouble()).ToArray();
                                float[] gyval = (new float[outwidth * outheight * channels * batch]).Select((_) => (float)rd.NextDouble()).ToArray();

                                Map2D x  = new Map2D(channels, inwidth, inheight, batch, xval);
                                Map2D gy = new Map2D(channels, outwidth, outheight, batch, gyval);

                                Map2D gx = Reference(x, gy, stride);

                                OverflowCheckedTensor x_tensor  = new OverflowCheckedTensor(Shape.Map2D(channels, inwidth, inheight, batch), xval);
                                OverflowCheckedTensor y_tensor  = new OverflowCheckedTensor(Shape.Map2D(channels, outwidth, outheight, batch));
                                OverflowCheckedTensor gy_tensor = new OverflowCheckedTensor(Shape.Map2D(channels, outwidth, outheight, batch), gyval);
                                OverflowCheckedTensor gx_tensor = new OverflowCheckedTensor(Shape.Map2D(channels, inwidth, inheight, batch));

                                MaxPooling ope_pool = new MaxPooling(inwidth, inheight, channels, stride, batch);
                                ope_pool.Execute(x_tensor, y_tensor);

                                MaxUnpooling ope_unpool = new MaxUnpooling(inwidth, inheight, channels, stride, batch);
                                ope_unpool.Execute(gy_tensor, x_tensor, y_tensor, gx_tensor);

                                float[] gx_expect = gx.ToArray();
                                float[] gx_actual = gx_tensor.State;

                                int gx_expect_nonzero = gx_expect.Count((v) => v != 0);
                                int gx_actual_nonzero = gx_expect.Count((v) => v != 0);

                                CollectionAssert.AreEqual(xval, x_tensor.State);
                                CollectionAssert.AreEqual(gyval, gy_tensor.State);

                                Assert.AreEqual(y_tensor.Length, gx_expect_nonzero);
                                Assert.AreEqual(y_tensor.Length, gx_actual_nonzero);

                                AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, ref max_err, $"mismatch value {channels},{stride},{inwidth},{inheight},{batch}");

                                Console.WriteLine($"pass: {channels},{stride},{inwidth},{inheight},{batch}");
                            }
                        }
                    }
                }
            }

            Console.WriteLine($"maxerr:{max_err}");
        }
        public void SpeedTest()
        {
            int inwidth = 512, inheight = 512, channels = 32, stride = 2;
            int outwidth = inwidth / stride, outheight = inheight / stride;

            OverflowCheckedTensor x_tensor  = new OverflowCheckedTensor(Shape.Map2D(channels, inwidth, inheight));
            OverflowCheckedTensor y_tensor  = new OverflowCheckedTensor(Shape.Map2D(channels, outwidth, outheight));
            OverflowCheckedTensor gy_tensor = new OverflowCheckedTensor(Shape.Map2D(channels, outwidth, outheight));
            OverflowCheckedTensor gx_tensor = new OverflowCheckedTensor(Shape.Map2D(channels, inwidth, inheight));

            MaxUnpooling ope = new MaxUnpooling(inwidth, inheight, channels, stride);

            Stopwatch sw = new Stopwatch();

            sw.Start();

            ope.Execute(gy_tensor, x_tensor, y_tensor, gx_tensor);
            ope.Execute(gy_tensor, x_tensor, y_tensor, gx_tensor);
            ope.Execute(gy_tensor, x_tensor, y_tensor, gx_tensor);
            ope.Execute(gy_tensor, x_tensor, y_tensor, gx_tensor);

            sw.Stop();

            Console.WriteLine($"{sw.ElapsedMilliseconds / 4} msec");
        }
示例#3
0
文件: Network.cs 项目: xuan2261/XNet
        public bool CreateLayer(int nCount, ELayerType type, ActivationSettings activationSettings)
        {
            Layer.Utility.Layer layer;
            switch (type)
            {
            case ELayerType.Invalid:
                throw new ArgumentException("Invalid \"type\" argument.");

            case ELayerType.AveragePooling:
                layer = new AveragePooling(nCount, Layers.Count, activationSettings);
                Layers.Add(layer);
                return(true);

            case ELayerType.AverageUnpooling:
                layer = new AverageUnpooling(nCount, Layers.Count, activationSettings);
                Layers.Add(layer);
                return(true);

            case ELayerType.Convolutional:
                layer = new Convolutional(nCount, Layers.Count, activationSettings);
                Layers.Add(layer);
                return(true);

            case ELayerType.Deconvolutional:
                layer = new Deconvolutional(nCount, Layers.Count, activationSettings);
                Layers.Add(layer);
                return(true);

            case ELayerType.Dropout:
                layer = new Dropout(nCount, Layers.Count, activationSettings);
                Layers.Add(layer);
                return(true);

            case ELayerType.FullyConnected:
                layer = new FullyConnected(nCount, Layers.Count, activationSettings);
                Layers.Add(layer);
                return(true);

            case ELayerType.GatedRecurrent:
                layer = new GatedRecurrent(nCount, Layers.Count, activationSettings);
                Layers.Add(layer);
                return(true);

            case ELayerType.LSTM:
                layer = new LSTM(nCount, Layers.Count, activationSettings);
                Layers.Add(layer);
                return(true);

            case ELayerType.MaxPooling:
                layer = new MaxPooling(nCount, Layers.Count, activationSettings);
                Layers.Add(layer);
                return(true);

            case ELayerType.MaxUnpooling:
                layer = new MaxUnpooling(nCount, Layers.Count, activationSettings);
                Layers.Add(layer);
                return(true);

            case ELayerType.Recurrent:
                layer = new Recurrent(nCount, Layers.Count, activationSettings);
                Layers.Add(layer);
                return(true);

            default:
                throw new ArgumentException("Invalid \"type\" argument.");
            }
        }