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 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}");
        }
示例#3
0
        public void ReferenceTest()
        {
            int inchannels = 6, outchannels = 8, batch = 3;

            float[] xval = (new float[inchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] yval = (new float[outchannels * batch]).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[] ycval = (new System.Numerics.Complex[yval.Length / 2])
                                              .Select((_, idx) => new System.Numerics.Complex(yval[idx * 2], yval[idx * 2 + 1])).ToArray();

            ComplexMap0D x = new ComplexMap0D(inchannels / 2, batch, xcval);
            ComplexMap0D y = new ComplexMap0D(outchannels / 2, batch, ycval);

            ComplexFilter0D gw = Reference(x, y);

            float[] gw_expect =
            {
                3.720000000e-04f, -6.300000000e-05f, 5.460000000e-04f, -6.900000000e-05f, 7.200000000e-04f, -7.500000000e-05f,
                2.940000000e-04f, -5.700000000e-05f, 4.440000000e-04f, -6.300000000e-05f, 5.940000000e-04f, -6.900000000e-05f,
                2.160000000e-04f, -5.100000000e-05f, 3.420000000e-04f, -5.700000000e-05f, 4.680000000e-04f, -6.300000000e-05f,
                1.380000000e-04f, -4.500000000e-05f, 2.400000000e-04f, -5.100000000e-05f, 3.420000000e-04f, -5.700000000e-05f,
            };

            float[] gw_actual = gw.ToArray();

            AssertError.Tolerance(gw_expect, gw_actual, 1e-9f, 1e-5f, $"mismatch value {inchannels},{outchannels},{batch}");
        }
示例#4
0
        public static ComplexFilter0D Reference(ComplexMap0D x, ComplexMap0D gy)
        {
            int inchannels = x.Channels, outchannels = gy.Channels, batch = x.Batch;

            ComplexFilter0D w = new ComplexFilter0D(inchannels, outchannels);

            Func <System.Numerics.Complex, System.Numerics.Complex, System.Numerics.Complex> mul_grad = (z1, z2) => {
                return(new System.Numerics.Complex(z1.Real * z2.Real + z1.Imaginary * z2.Imaginary, z1.Imaginary * z2.Real - z1.Real * z2.Imaginary));
            };

            for (int inch, outch = 0; outch < outchannels; outch++)
            {
                for (inch = 0; inch < inchannels; inch++)
                {
                    System.Numerics.Complex sum = 0;
                    for (int th = 0; th < batch; th++)
                    {
                        sum += mul_grad(gy[outch, th], x[inch, th]);
                    }

                    w[inch, outch] = sum;
                }
            }

            return(w);
        }
        public void ExecuteTest()
        {
            float max_err = 0;

            foreach (int batch in new int[] { 1, 2, 3 })
            {
                foreach (int inchannels in new int[] { 2, 4, 10, 20 })
                {
                    foreach (int outchannels in new int[] { 6, 14 })
                    {
                        float[] xval = (new float[inchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
                        float[] wval = (new float[inchannels * outchannels / 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);

                        OverflowCheckedTensor x_tensor = new OverflowCheckedTensor(Shape.Map0D(inchannels, batch), xval);
                        OverflowCheckedTensor w_tensor = new OverflowCheckedTensor(Shape.Kernel0D(inchannels, outchannels / 2), wval);

                        OverflowCheckedTensor y_tensor = new OverflowCheckedTensor(Shape.Map0D(outchannels, batch));

                        ComplexDense ope = new ComplexDense(inchannels, outchannels, gradmode: false, batch);

                        ope.Execute(x_tensor, w_tensor, y_tensor);

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

                        CollectionAssert.AreEqual(xval, x_tensor.State);
                        CollectionAssert.AreEqual(wval, w_tensor.State);

                        AssertError.Tolerance(y_expect, y_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 static ComplexMap0D Reference(ComplexMap0D y, ComplexFilter0D w)
        {
            int inchannels = w.InChannels, outchannels = w.OutChannels, batch = y.Batch;

            ComplexMap0D x = new ComplexMap0D(inchannels, batch);

            for (int th = 0; th < batch; th++)
            {
                for (int outch = 0; outch < outchannels; outch++)
                {
                    System.Numerics.Complex v = y[outch, th];

                    for (int inch = 0; inch < inchannels; inch++)
                    {
                        x[inch, th] += v * w[inch, outch];
                    }
                }
            }

            return(x);
        }
        public static ComplexMap0D Reference(ComplexMap0D x, ComplexFilter0D w)
        {
            int inchannels = x.Channels, outchannels = w.OutChannels, batch = x.Batch;

            ComplexMap0D y = new ComplexMap0D(outchannels, batch);

            for (int th = 0; th < batch; th++)
            {
                for (int outch = 0; outch < outchannels; outch++)
                {
                    System.Numerics.Complex sum = y[outch, th];

                    for (int inch = 0; inch < inchannels; inch++)
                    {
                        sum += x[inch, th] * w[inch, outch];
                    }

                    y[outch, th] = sum;
                }
            }

            return(y);
        }