Esempio n. 1
0
        public void ReferenceTest()
        {
            int length = 24;

            float[] xval = (new float[length]).Select((_, idx) => (float)idx / 12).ToArray();
            float[] yval = (new float[length]).Select((_, idx) => (float)idx / 24).ToArray();

            ParameterField x = new Tensor(Shape.Vector(length), xval);
            ParameterField p = new Tensor(Shape.Scalar(), new float[] { 1.5f });

            VariableField y_actual = new Tensor(Shape.Vector(length), yval);

            Field y_expect = Pow(x, p);
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");

            float[] gp_actual = p.GradTensor.State;

            AssertError.Tolerance(gp_expect, gp_actual, 1e-7f, 1e-5f, $"not equal gp");
        }
        public void ReferenceTest()
        {
            int scale = 2;
            int channels = 2, inwidth = 5, inheight = 4, indepth = 3, batch = 2;
            int outwidth = inwidth * scale, outheight = inheight * scale, outdepth = indepth * scale;

            float[] xval = (new float[channels * inwidth * inheight * indepth * batch]).Select((_, idx) => idx * 2e-3f).ToArray();
            float[] yval = (new float[channels * outwidth * outheight * outdepth * batch]).Select((_, idx) => idx * 1e-3f).ToArray();

            Tensor xtensor = new Tensor(Shape.Map3D(channels, inwidth, inheight, indepth, batch), xval);
            Tensor ytensor = new Tensor(Shape.Map3D(channels, outwidth, outheight, outdepth, batch), yval);

            ParameterField x        = xtensor;
            VariableField  y_actual = ytensor;

            Field y_expect = NeighborZoom3D(x);
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
Esempio n. 3
0
        public void ReferenceTest()
        {
            int channels = 7, width = 3, height = 5, batch = 2;

            float[] xval    = (new float[channels * width * height * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            Tensor  xtensor = new Tensor(Shape.Map2D(channels, width, height, batch), xval);

            float[] yval    = (new float[width * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            Tensor  ytensor = new Tensor(Shape.Map0D(width, batch), yval);

            VariableField x        = xtensor;
            VariableField y_actual = ytensor;

            Field y_expect = Max(x, new int[] { Axis.Map2D.Channels, Axis.Map2D.Height }, keepdims: false);
            Field err      = y_expect - y_actual;

            OutputNode errnode = err.Value.Save();

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] err_actual = errnode.Tensor.State;

            AssertError.Tolerance(err_expect, err_actual, 1e-7f, 1e-5f, $"not equal err");
        }
Esempio n. 4
0
        public void ExecuteTest()
        {
            const int length = 256;
            Random    rd     = new Random(1234);

            int[] idxes = (new int[length]).Select((_, idx) => idx).ToArray();

            float[] x = (new float[length]).Select((_) => (float)rd.NextDouble() * 8f - 4f).ToArray();

            {
                Tensor t = new Tensor(Shape.Vector(length), x);

                Tensor o = Tensor.Floor(t);

                AssertError.Tolerance(idxes.Select((idx) => (float)Math.Floor(x[idx])).ToArray(), o.State, 1e-7f, 1e-5f);
            }

            {
                InputNode t = new Tensor(Shape.Vector(length), x);

                var n = t + 0;

                OutputNode o = VariableNode.Floor(n).Save();

                Flow flow = Flow.FromOutputs(o);
                flow.Execute();

                AssertError.Tolerance(idxes.Select((idx) => (float)Math.Floor(x[idx])).ToArray(), o.State, 1e-7f, 1e-5f);
            }
        }
        public void OptimizeTest()
        {
            float max_err = 0;

            foreach (int batch in new int[] { 1, 2 })
            {
                foreach (int inchannels in new int[] { 1, 2, 3, 4, 5, 10, 15, 20 })
                {
                    foreach (int outchannels in new int[] { 7, 13 })
                    {
                        float[] yval = (new float[outchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
                        float[] wval = (new float[inchannels * outchannels]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

                        Map0D    y = new Map0D(outchannels, batch, yval);
                        Filter0D w = new Filter0D(inchannels, outchannels, 1, wval);

                        Map0D x           = Reference(y, w);
                        Map0D x_optimized = OptimizedReference(y, w);

                        float[] x_expect = x.ToArray();
                        float[] x_actual = x_optimized.ToArray();

                        AssertError.Tolerance(x_expect, x_actual, 1e-7f, 1e-5f, ref max_err, $"mismatch value {inchannels},{outchannels},{batch}");

                        Console.WriteLine($"pass: {inchannels},{outchannels},{batch}");
                    }
                }
            }

            Console.WriteLine($"maxerr:{max_err}");
        }
        public void ReferenceTest()
        {
            int inchannels = 9, outchannels = 12, batch = 3;

            float[] xval = (new float[inchannels * batch]).Select((_, idx) => idx * 1e-2f).ToArray();
            float[] yval = (new float[outchannels * batch]).Select((_, idx) => idx * 1e-2f).ToArray();
            float[] wval = (new float[outchannels * inchannels / 9 * 4]).Select((_, idx) => idx * 1e-2f).Reverse().ToArray();

            Tensor xtensor = new Tensor(Shape.Map0D(inchannels, batch), xval);
            Tensor ytensor = new Tensor(Shape.Map0D(outchannels, batch), yval);
            Tensor wtensor = new Tensor(Shape.Kernel0D(inchannels / 3 * 4, outchannels / 3), wval);

            ParameterField x        = xtensor;
            ParameterField w        = wtensor;
            VariableField  y_actual = ytensor;

            Field y_expect = TrivectorDense(x, w);
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;
            float[] gw_actual = w.GradTensor.State;

            AssertError.Tolerance(gw_expect, gw_actual, 1e-6f, 1e-5f, $"not equal gw");

            AssertError.Tolerance(gx_expect, gx_actual, 1e-6f, 1e-5f, $"not equal gx");
        }
Esempio n. 7
0
        public void ReferenceTest()
        {
            int channel = 5, width = 4, height = 3, batch = 2;

            float[] x1val = (new float[channel]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] x2val = (new float[channel * width * height * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] yval  = (new float[channel * width * height * batch]).Select((_, idx) => idx * 1e-3f).ToArray();

            Tensor x1tensor = new Tensor(Shape.Vector(channel), x1val);
            Tensor x2tensor = new Tensor(Shape.Map2D(channel, width, height, batch), x2val);

            Tensor ytensor = new Tensor(Shape.Map2D(channel, width, height, batch), yval);

            ParameterField x1       = x1tensor;
            ParameterField x2       = x2tensor;
            VariableField  y_actual = ytensor;

            Field y_expect = x1 + x2;
            Field err      = Abs(y_expect - y_actual);

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx1_actual = x1.GradTensor.State;
            float[] gx2_actual = x2.GradTensor.State;

            AssertError.Tolerance(gx1_expect, gx1_actual, 1e-7f, 1e-5f, $"not equal gx1");

            AssertError.Tolerance(gx2_expect, gx2_actual, 1e-7f, 1e-5f, $"not equal gx2");
        }
        public void ReferenceTest()
        {
            int inchannels = 6, outchannels = 8, batch = 3;

            float[] yval = (new float[outchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] wval = (new float[outchannels * inchannels / 2]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

            System.Numerics.Complex[] ycval = (new System.Numerics.Complex[yval.Length / 2])
                                              .Select((_, idx) => new System.Numerics.Complex(yval[idx * 2], yval[idx * 2 + 1])).ToArray();

            System.Numerics.Complex[] wcval = (new System.Numerics.Complex[wval.Length / 2])
                                              .Select((_, idx) => new System.Numerics.Complex(wval[idx * 2], wval[idx * 2 + 1])).ToArray();

            ComplexMap0D    y = new ComplexMap0D(outchannels / 2, batch, ycval);
            ComplexFilter0D w = new ComplexFilter0D(inchannels / 2, outchannels / 2, wcval);

            ComplexMap0D x = Reference(y, w);

            float[] x_expect =
            {
                -4.000000e-05f, 2.600000e-04f, -3.200000e-05f, 2.040000e-04f, -2.400000e-05f, 1.480000e-04f,
                -8.000000e-06f, 1.124000e-03f,  0.000000e+00f, 9.400000e-04f,  8.000000e-06f, 7.560000e-04f,
                2.400000e-05f,  1.988000e-03f,  3.200000e-05f, 1.676000e-03f,  4.000000e-05f, 1.364000e-03f,
            };

            float[] x_actual = x.ToArray();

            AssertError.Tolerance(x_expect, x_actual, 1e-9f, 1e-5f, $"mismatch value {inchannels},{outchannels},{batch}");
        }
        public void ExecuteTest()
        {
            const int length = 256;
            Random    rd     = new Random(1234);

            int[] idxes = (new int[length]).Select((_, idx) => idx).ToArray();

            float[] x = (new float[length]).Select((_) => (float)rd.NextDouble() * 4 - 2).ToArray();
            float   c = 0.25f;

            {
                Tensor t = new Tensor(Shape.Vector(length), x);

                Tensor o = Tensor.LeakyRelu(t, c);

                AssertError.Tolerance(idxes.Select((idx) => Math.Max(x[idx], 0) + c * Math.Min(x[idx], 0)).ToArray(), o.State, 1e-7f, 1e-5f);
            }

            {
                InputNode t = new Tensor(Shape.Vector(length), x);

                var n = t + 0;

                OutputNode o = VariableNode.LeakyRelu(n, c).Save();

                Flow flow = Flow.FromOutputs(o);
                flow.Execute();

                AssertError.Tolerance(idxes.Select((idx) => Math.Max(x[idx], 0) + c * Math.Min(x[idx], 0)).ToArray(), o.State, 1e-7f, 1e-5f);
            }
        }
        public void TheoreticalTest()
        {
            int length = 24;

            float[] xval = (new float[length]).Select((_, idx) => (float)idx * 2 - length).ToArray();
            float[] tval = (new float[length]).Select((_, idx) => (float)idx / 2).Reverse().ToArray();

            Tensor xtensor = new Tensor(Shape.Vector(length), xval);
            Tensor ttensor = new Tensor(Shape.Vector(length), tval);

            ParameterField x = xtensor;
            VariableField  t = ttensor;

            Field x_real = ComplexReal(x), x_imag = ComplexImag(x);

            Field xy  = ComplexCast(x_real * x_real - x_imag * x_imag, 2 * x_imag * x_real);
            Field err = xy - t;

            (Flow flow, Parameters parameters) = Flow.Optimize(err);
            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
        public void ReferenceTest()
        {
            int channels = 7, kwidth = 3, kheight = 5, stride = 2, inwidth = 13, inheight = 17;
            int outwidth = (inwidth - kwidth) / stride + 1, outheight = (inheight - kheight) / stride + 1, batch = 2;

            float[] xval = (new float[inwidth * inheight * channels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] yval = (new float[outwidth * outheight * channels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] wval = (new float[kwidth * kheight * channels]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

            Tensor xtensor = new Tensor(Shape.Map2D(channels, inwidth, inheight, batch), xval);
            Tensor ytensor = new Tensor(Shape.Map2D(channels, outwidth, outheight, batch), yval);
            Tensor wtensor = new Tensor(Shape.Kernel2D(channels, 1, kwidth, kheight), wval);

            VariableField  x_actual = xtensor;
            ParameterField w        = wtensor;
            ParameterField y        = ytensor;

            Field      x_expect = ChannelwiseDeconvolution2D(y, w, stride, Shape.Map2D(channels, inwidth, inheight, batch));
            Field      err      = Abs(x_expect - x_actual);
            OutputNode err_node = err.Value.Save();

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] err_actual = err_node.Tensor.State;
            float[] gy_actual  = y.GradTensor.State;
            float[] gw_actual  = w.GradTensor.State;

            AssertError.Tolerance(gw_expect, gw_actual, 1e-7f, 1e-5f, $"not equal gw");

            AssertError.Tolerance(gy_expect, gy_actual, 1e-7f, 1e-5f, $"not equal gy");
        }
Esempio n. 12
0
        public void ReferenceTest()
        {
            int stride = 3;
            int channels = 2, inwidth = 10, batch = 2;
            int outwidth = inwidth / stride;

            float[] xval = (new float[channels * inwidth * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] yval = (new float[channels * outwidth * batch]).Select((_, idx) => idx * 2e-3f).ToArray();

            Tensor xtensor = new Tensor(Shape.Map1D(channels, inwidth, batch), xval);
            Tensor ytensor = new Tensor(Shape.Map1D(channels, outwidth, batch), yval);

            ParameterField x        = xtensor;
            VariableField  y_actual = ytensor;

            Field y_expect = AveragePooling1D(x, stride);
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
Esempio n. 13
0
        public void ReferenceTest()
        {
            int inchannels = 7, outchannels = 11, width = 13, batch = 2;

            float[] xval = (new float[width * inchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] yval = (new float[width * outchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] wval = (new float[outchannels * inchannels]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

            Tensor xtensor = new Tensor(Shape.Map1D(inchannels, width, batch), xval);
            Tensor ytensor = new Tensor(Shape.Map1D(outchannels, width, batch), yval);
            Tensor wtensor = new Tensor(Shape.Kernel0D(inchannels, outchannels), wval);

            ParameterField x        = xtensor;
            ParameterField w        = wtensor;
            VariableField  y_actual = ytensor;

            Field y_expect = PointwiseConvolution1D(x, w);
            Field err      = Abs(y_expect - y_actual);

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;
            float[] gw_actual = w.GradTensor.State;

            AssertError.Tolerance(gw_expect, gw_actual, 1e-7f, 1e-5f, $"not equal gw");

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
Esempio n. 14
0
        public void ReferenceTest()
        {
            int channels = 7, kwidth = 3, stride = 2, inwidth = 13, batch = 2;
            int outwidth = (inwidth - kwidth) / stride + 1;

            float[] xval = (new float[batch * inwidth * channels]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] wval = (new float[kwidth * channels]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

            Map1D    x = new Map1D(channels, inwidth, batch, xval);
            Filter1D w = new Filter1D(channels, 1, kwidth, wval);

            Map1D y = Reference(x, w, kwidth, stride);

            float[] y_expect =
            {
                1.7500000e-04f, 1.9000000e-04f, 1.9900000e-04f, 2.0200000e-04f, 1.9900000e-04f, 1.9000000e-04f, 1.7500000e-04f,
                7.2100000e-04f, 6.9400000e-04f, 6.6100000e-04f, 6.2200000e-04f, 5.7700000e-04f, 5.2600000e-04f, 4.6900000e-04f,
                1.2670000e-03f, 1.1980000e-03f, 1.1230000e-03f, 1.0420000e-03f, 9.5500000e-04f, 8.6200000e-04f, 7.6300000e-04f,
                1.8130000e-03f, 1.7020000e-03f, 1.5850000e-03f, 1.4620000e-03f, 1.3330000e-03f, 1.1980000e-03f, 1.0570000e-03f,
                2.3590000e-03f, 2.2060000e-03f, 2.0470000e-03f, 1.8820000e-03f, 1.7110000e-03f, 1.5340000e-03f, 1.3510000e-03f,
                2.9050000e-03f, 2.7100000e-03f, 2.5090000e-03f, 2.3020000e-03f, 2.0890000e-03f, 1.8700000e-03f, 1.6450000e-03f,
                3.7240000e-03f, 3.4660000e-03f, 3.2020000e-03f, 2.9320000e-03f, 2.6560000e-03f, 2.3740000e-03f, 2.0860000e-03f,
                4.2700000e-03f, 3.9700000e-03f, 3.6640000e-03f, 3.3520000e-03f, 3.0340000e-03f, 2.7100000e-03f, 2.3800000e-03f,
                4.8160000e-03f, 4.4740000e-03f, 4.1260000e-03f, 3.7720000e-03f, 3.4120000e-03f, 3.0460000e-03f, 2.6740000e-03f,
                5.3620000e-03f, 4.9780000e-03f, 4.5880000e-03f, 4.1920000e-03f, 3.7900000e-03f, 3.3820000e-03f, 2.9680000e-03f,
                5.9080000e-03f, 5.4820000e-03f, 5.0500000e-03f, 4.6120000e-03f, 4.1680000e-03f, 3.7180000e-03f, 3.2620000e-03f,
                6.4540000e-03f, 5.9860000e-03f, 5.5120000e-03f, 5.0320000e-03f, 4.5460000e-03f, 4.0540000e-03f, 3.5560000e-03f,
            };

            float[] y_actual = y.ToArray();

            AssertError.Tolerance(y_expect, y_actual, 1e-7f, 1e-5f, $"mismatch value {channels},{kwidth},{stride},{inwidth},{batch}");
        }
        public void ReferenceTest()
        {
            int inchannels = 6, outchannels = 8, batch = 3;

            float[] xval = (new float[batch * inchannels]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] wval = (new float[outchannels * inchannels / 2]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

            System.Numerics.Complex[] xcval = (new System.Numerics.Complex[xval.Length / 2])
                                              .Select((_, idx) => new System.Numerics.Complex(xval[idx * 2], xval[idx * 2 + 1])).ToArray();

            System.Numerics.Complex[] wcval = (new System.Numerics.Complex[wval.Length / 2])
                                              .Select((_, idx) => new System.Numerics.Complex(wval[idx * 2], wval[idx * 2 + 1])).ToArray();

            ComplexMap0D    x = new ComplexMap0D(inchannels / 2, batch, xcval);
            ComplexFilter0D w = new ComplexFilter0D(inchannels / 2, outchannels / 2, wcval);

            ComplexMap0D y = Reference(x, w);

            float[] y_expect =
            {
                -5.400000e-05f, 2.930000e-04f, -3.600000e-05f, 2.030000e-04f, -1.800000e-05f, 1.130000e-04f,
                0.000000e+00f,  2.300000e-05f, -3.600000e-05f, 1.031000e-03f, -1.800000e-05f, 7.250000e-04f,
                -2.710505e-20f, 4.190000e-04f,  1.800000e-05f, 1.130000e-04f, -1.800000e-05f, 1.769000e-03f,
                0.000000e+00f,  1.247000e-03f,  1.800000e-05f, 7.250000e-04f,  3.600000e-05f, 2.030000e-04f,
            };

            float[] y_actual = y.ToArray();

            AssertError.Tolerance(y_expect, y_actual, 1e-9f, 1e-5f, $"mismatch value {inchannels},{outchannels},{batch}");
        }
        public void ReferenceTest()
        {
            int length = 24;

            float[] vval = (new float[length]).Select((_, idx) => ((float)idx * 2 - length) * 0.01f).ToArray();
            float[] qval = (new float[length / 3 * 4]).Select((_, idx) => ((float)idx * 3 - length) * 0.01f).Reverse().ToArray();
            float[] tval = (new float[length]).Select((_, idx) => ((float)idx / 2) * 0.01f).ToArray();

            Tensor vtensor = new Tensor(Shape.Vector(length), vval);
            Tensor qtensor = new Tensor(Shape.Vector(length / 3 * 4), qval);
            Tensor ttensor = new Tensor(Shape.Vector(length), tval);

            ParameterField v = vtensor;
            ParameterField q = qtensor;
            VariableField  t = ttensor;

            Field vq  = TrivectorQuaternionMul(v, q);
            Field err = vq - t;

            (Flow flow, Parameters parameters) = Flow.Optimize(err);
            flow.Execute();

            float[] gv_actual = v.GradTensor.State;
            float[] gq_actual = q.GradTensor.State;

            AssertError.Tolerance(gv_expect, gv_actual, 1e-7f, 1e-5f, $"not equal gv");

            AssertError.Tolerance(gq_expect, gq_actual, 1e-7f, 1e-5f, $"not equal gq");
        }
        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 OptimizeTest()
        {
            float max_err = 0;

            foreach (int batch in new int[] { 1, 2 })
            {
                foreach (int inchannels in new int[] { 1, 2, 3, 4, 5, 10, 15, 20 })
                {
                    foreach (int outchannels in new int[] { 7, 13 })
                    {
                        foreach (int inwidth in new int[] { 8, 9, 13, 17 })
                        {
                            float[] xval = (new float[inwidth * inchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
                            float[] wval = (new float[inchannels * outchannels]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

                            Map1D    x = new Map1D(inchannels, inwidth, batch, xval);
                            Filter1D w = new Filter1D(inchannels, outchannels, 1, wval);

                            Map1D y           = Reference(x, w);
                            Map1D y_optimized = OptimizedReference(x, w);

                            float[] y_expect = y.ToArray();
                            float[] y_actual = y_optimized.ToArray();

                            AssertError.Tolerance(y_expect, y_actual, 1e-7f, 1e-5f, ref max_err, $"mismatch value {inchannels},{outchannels},{inwidth},{batch}");

                            Console.WriteLine($"pass: {inchannels},{outchannels},{inwidth},{batch}");
                        }
                    }
                }
            }

            Console.WriteLine($"maxerr:{max_err}");
        }
        public void TheoreticalTest()
        {
            int length = 24;

            float[] xval = (new float[length]).Select((_, idx) => (float)idx * 2 - length).ToArray();
            float[] tval = (new float[length]).Select((_, idx) => (float)idx / 2).Reverse().ToArray();

            Tensor xtensor = new Tensor(Shape.Vector(length), xval);
            Tensor ttensor = new Tensor(Shape.Vector(length), tval);

            ParameterField x = xtensor;
            VariableField  t = ttensor;

            Field xr = QuaternionR(x), xi = QuaternionI(x), xj = QuaternionJ(x), xk = QuaternionK(x);
            Field norm = Square(xr) + Square(xi) + Square(xj) + Square(xk);
            Field s    = norm / (norm + 1);

            Field xy = QuaternionCast(
                xr * s,
                xi * s,
                xj * s,
                xk * s);
            Field err = xy - t;

            (Flow flow, Parameters parameters) = Flow.Optimize(err);
            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
        public void ReferenceTest()
        {
            Shape inshape  = new Shape(ShapeType.Map, 85, 32);
            Shape outshape = new Shape(ShapeType.Map, 23, 32);

            float[] xval = (new float[inshape.Length]).Select((_, idx) => (float)(idx)).Reverse().ToArray();

            float[] tval = (new float[outshape.Length]).Select((_, idx) => (float)(idx * 2)).ToArray();

            Tensor xtensor = new Tensor(inshape, xval);
            Tensor ttensor = new Tensor(outshape, tval);

            ParameterField x = xtensor;
            VariableField  t = ttensor;

            Field y = ExtractChannel(x, 13, 23);

            StoreField o = y.Save();

            Field err = y - t;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
Esempio n. 21
0
        public void ReferenceTest()
        {
            int length = 24;

            float[] xval = (new float[length]).Select((_, idx) => (float)idx - 12).ToArray();
            float[] yval = (new float[length]).Select((_, idx) => (float)idx / 24).ToArray();

            Tensor xtensor = new Tensor(Shape.Vector(length), xval);

            Tensor ytensor = new Tensor(Shape.Vector(length), yval);

            VariableField x        = xtensor;
            VariableField y_actual = ytensor;

            Field y_expect = Sign(x);
            Field err      = y_expect - y_actual;

            OutputNode errnode = err.Value.Save();

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] err_actual = errnode.Tensor.State;

            AssertError.Tolerance(err_expect, err_actual, 1e-7f, 1e-5f, $"not equal err");
        }
Esempio n. 22
0
        public void ReferenceTest()
        {
            int length = 24;

            float[] xval = (new float[length]).Select((_, idx) => (idx - 11.5f) / 12f).ToArray();
            float[] yval = (new float[length]).Select((_, idx) => (float)idx / 24).ToArray();

            Tensor xtensor = new Tensor(Shape.Vector(length), xval);

            Tensor ytensor = new Tensor(Shape.Vector(length), yval);

            ParameterField x        = xtensor;
            VariableField  y_actual = ytensor;

            Field y_expect = Arcsin(x);
            Field err      = y_expect - y_actual;

            OutputNode n = err.Value.Save();

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] div = n.Tensor.State;

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-6f, 8e-4f, $"not equal gx"); /*nonlinear tolerance*/
        }
Esempio n. 23
0
        public void ReferenceTest()
        {
            int inchannels = 9, outchannels = 12, kwidth = 3, kheight = 5, stride = 2, inwidth = 7, inheight = 8;
            int outwidth = (inwidth - kwidth) / stride + 1, outheight = (inheight - kheight) / stride + 1, batch = 3;

            float[] xval = (new float[inwidth * inheight * inchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] yval = (new float[outwidth * outheight * outchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] wval = (new float[kwidth * kheight * outchannels * inchannels / 9 * 4]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

            Tensor xtensor = new Tensor(Shape.Map2D(inchannels, inwidth, inheight, batch), xval);
            Tensor ytensor = new Tensor(Shape.Map2D(outchannels, outwidth, outheight, batch), yval);
            Tensor wtensor = new Tensor(Shape.Kernel2D(inchannels / 3 * 4, outchannels / 3, kwidth, kheight), wval);

            VariableField  x_actual = xtensor;
            ParameterField w        = wtensor;
            ParameterField y        = ytensor;

            Field x_expect = TrivectorDeconvolution2D(y, w, stride, Shape.Map2D(inchannels, inwidth, inheight, batch));
            Field err      = x_expect - x_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gy_actual = y.GradTensor.State;
            float[] gw_actual = w.GradTensor.State;

            AssertError.Tolerance(gw_expect, gw_actual, 1e-6f, 1e-5f, $"not equal gw");

            AssertError.Tolerance(gy_expect, gy_actual, 1e-6f, 1e-5f, $"not equal gy");
        }
Esempio n. 24
0
        public void ReferenceTest()
        {
            int length = 24;

            float[] vval = (new float[length]).Select((_, idx) => ((float)idx * 2 - length)).ToArray();
            float[] uval = (new float[length]).Select((_, idx) => ((float)idx * 3 - length)).Reverse().ToArray();
            float[] tval = (new float[length]).Select((_, idx) => ((float)idx / 2)).ToArray();

            Tensor vtensor = new Tensor(Shape.Vector(length), vval);
            Tensor utensor = new Tensor(Shape.Vector(length), uval);
            Tensor ttensor = new Tensor(Shape.Vector(length), tval);

            ParameterField v = vtensor;
            ParameterField u = utensor;
            VariableField  t = ttensor;

            Field vu  = TrivectorCross(v, u);
            Field err = vu - t;

            (Flow flow, Parameters parameters) = Flow.Optimize(err);
            flow.Execute();

            float[] gv_actual = v.GradTensor.State;
            float[] gu_actual = u.GradTensor.State;

            AssertError.Tolerance(gv_expect, gv_actual, 1e-7f, 1e-5f, $"not equal gv");

            AssertError.Tolerance(gu_expect, gu_actual, 1e-7f, 1e-5f, $"not equal gu");
        }
Esempio n. 25
0
        public void ReferenceTest()
        {
            int length = 24;

            float[] xval = (new float[length]).Select((_, idx) => ((float)idx - 12) / 12f).ToArray();
            float[] yval = (new float[length]).Select((_, idx) => (float)idx / 24).ToArray();

            Tensor xtensor = new Tensor(Shape.Vector(length), xval);

            Tensor ytensor = new Tensor(Shape.Vector(length), yval);

            ParameterField x        = xtensor;
            VariableField  y_actual = ytensor;

            Field y_expect = 5 - x;
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
        public void ReferenceTest()
        {
            int pad_left = 2, pad_right = 1, pad_top = 3, pad_bottom = 5, pad_front = 4, pad_rear = 2;
            int channels = 3, inwidth = 5, inheight = 6, indepth = 7, batch = 2;
            int outwidth  = inwidth + pad_left + pad_right;
            int outheight = inheight + pad_top + pad_bottom;
            int outdepth  = indepth + pad_front + pad_rear;

            float[] xval = (new float[channels * inwidth * inheight * indepth * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] yval = (new float[channels * outwidth * outheight * outdepth * batch]).Select((_, idx) => idx * 2e-3f).ToArray();

            Tensor xtensor = new Tensor(Shape.Map3D(channels, inwidth, inheight, indepth, batch), xval);
            Tensor ytensor = new Tensor(Shape.Map3D(channels, outwidth, outheight, outdepth, batch), yval);

            ParameterField x        = xtensor;
            VariableField  y_actual = ytensor;

            Field y_expect = EdgePadding3D(x, pad_left, pad_right, pad_top, pad_bottom, pad_front, pad_rear);
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
        public void ReferenceTest()
        {
            int inchannels = 6, outchannels = 8, kwidth = 3, stride = 2, inwidth = 13;
            int outwidth = (inwidth - kwidth) / stride + 1, batch = 3;

            float[] xval = (new float[inwidth * inchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] yval = (new float[outwidth * outchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] wval = (new float[kwidth * outchannels * inchannels / 2]).Select((_, idx) => idx * 1e-3f).Reverse().ToArray();

            Tensor xtensor = new Tensor(Shape.Map1D(inchannels, inwidth, batch), xval);
            Tensor ytensor = new Tensor(Shape.Map1D(outchannels, outwidth, batch), yval);
            Tensor wtensor = new Tensor(Shape.Kernel1D(inchannels, outchannels / 2, kwidth), wval);

            ParameterField x        = xtensor;
            ParameterField w        = wtensor;
            VariableField  y_actual = ytensor;

            Field y_expect = ComplexConvolution1D(x, w, stride);
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;
            float[] gw_actual = w.GradTensor.State;

            AssertError.Tolerance(gw_expect, gw_actual, 1e-7f, 1e-5f, $"not equal gw");

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
Esempio n. 28
0
        public void ReferenceTest()
        {
            int scale = 3;
            int inchannels = 2, outwidth = 4, outheight = 3, batch = 2;
            int outchannels = inchannels * scale * scale, inwidth = outwidth * scale, inheight = outheight * scale;

            float[] xval = (new float[inchannels * inwidth * inheight * batch]).Select((_, idx) => idx * 2e-3f).ToArray();
            float[] yval = (new float[outchannels * outwidth * outheight * batch]).Select((_, idx) => idx * 1e-3f).ToArray();

            Tensor xtensor = new Tensor(Shape.Map2D(inchannels, inwidth, inheight, batch), xval);
            Tensor ytensor = new Tensor(Shape.Map2D(outchannels, outwidth, outheight, batch), yval);

            ParameterField x        = xtensor;
            VariableField  y_actual = ytensor;

            Field y_expect = SpaceToChannel2D(x, scale);
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }
Esempio n. 29
0
        public void ReferenceTest()
        {
            int length = 24;

            float[] x1val = (new float[length]).Select((_, idx) => (float)idx).ToArray();
            float[] x2val = (new float[length]).Select((_, idx) => (float)idx + 1).Reverse().ToArray();
            float[] yval  = (new float[length]).Select((_, idx) => (float)idx * 2).ToArray();

            Tensor x1tensor = new Tensor(Shape.Vector(length), x1val);
            Tensor x2tensor = new Tensor(Shape.Vector(length), x2val);

            Tensor ytensor = new Tensor(Shape.Vector(length), yval);

            ParameterField x1       = x1tensor;
            ParameterField x2       = x2tensor;
            VariableField  y_actual = ytensor;

            Field y_expect = x1 + x2;
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx1_actual = x1.GradTensor.State;
            float[] gx2_actual = x2.GradTensor.State;

            AssertError.Tolerance(gx1_expect, gx1_actual, 1e-7f, 1e-5f, $"not equal gx1");

            AssertError.Tolerance(gx2_expect, gx2_actual, 1e-7f, 1e-5f, $"not equal gx2");
        }
Esempio n. 30
0
        public void TheoreticalTest()
        {
            int length = 24;

            float[] xval = (new float[length]).Select((_, idx) => (float)idx * 2 - length).ToArray();
            float[] yval = (new float[length]).Select((_, idx) => (float)idx / 2).ToArray();

            Tensor xtensor = new Tensor(Shape.Vector(length), xval);

            Tensor ytensor = new Tensor(Shape.Vector(length), yval);

            ParameterField x        = xtensor;
            VariableField  y_actual = ytensor;

            Field r = QuaternionR(x), i = QuaternionI(x), j = QuaternionJ(x), k = QuaternionK(x);

            Field y_expect = QuaternionCast(Relu(r), i, j, k);
            Field err      = y_expect - y_actual;

            (Flow flow, Parameters Parameters) = Flow.Optimize(err);

            flow.Execute();

            float[] gx_actual = x.GradTensor.State;

            AssertError.Tolerance(gx_expect, gx_actual, 1e-7f, 1e-5f, $"not equal gx");
        }