public static ComplexMap1D Reference(ComplexMap1D y, ComplexFilter1D w, int inw, int kwidth, int stride)
        {
            int inchannels = w.InChannels, outchannels = w.OutChannels, batch = y.Batch;
            int outw = (inw - kwidth) / stride + 1;

            if (y.Width != outw)
            {
                throw new ArgumentException("mismatch shape");
            }

            ComplexMap1D x = new ComplexMap1D(inchannels, inw, batch);

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

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

            return(x);
        }
Example #2
0
        public static ComplexMap1D Reference(ComplexMap1D x, ComplexFilter1D w, int kwidth, int stride)
        {
            int inchannels = x.Channels, outchannels = w.OutChannels, batch = x.Batch;
            int inw = x.Width, outw = (inw - kwidth) / stride + 1;

            ComplexMap1D y = new ComplexMap1D(outchannels, outw, batch);

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

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

                            y[outch, ox, th] = sum;
                        }
                    }
                }
            }

            return(y);
        }
        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 })
                    {
                        foreach (int kwidth in new int[] { 1, 3, 5 })
                        {
                            foreach (int stride in new int[] { 1, 2, 3 })
                            {
                                foreach (int inwidth in new int[] { 8, 9, 13, 17 })
                                {
                                    int outwidth = (inwidth - kwidth) / stride + 1;

                                    float[] yval = (new float[outwidth * outchannels * batch]).Select((_, idx) => idx * 1e-3f).ToArray();
                                    float[] wval = (new float[kwidth * inchannels * outchannels / 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();

                                    ComplexMap1D    y = new ComplexMap1D(outchannels / 2, outwidth, batch, ycval);
                                    ComplexFilter1D w = new ComplexFilter1D(inchannels / 2, outchannels / 2, kwidth, wcval);

                                    ComplexMap1D x = Reference(y, w, inwidth, kwidth, stride);

                                    OverflowCheckedTensor y_tensor = new OverflowCheckedTensor(Shape.Map1D(outchannels, outwidth, batch), yval);
                                    OverflowCheckedTensor w_tensor = new OverflowCheckedTensor(Shape.Kernel1D(inchannels, outchannels / 2, kwidth), wval);

                                    OverflowCheckedTensor x_tensor = new OverflowCheckedTensor(Shape.Map1D(inchannels, inwidth, batch));

                                    ComplexDeconvolution1D ope = new ComplexDeconvolution1D(inwidth, outchannels, inchannels, kwidth, stride, gradmode: false, batch);

                                    ope.Execute(y_tensor, w_tensor, x_tensor);

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

                                    CollectionAssert.AreEqual(yval, y_tensor.State);
                                    CollectionAssert.AreEqual(wval, w_tensor.State);

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

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

            Console.WriteLine($"maxerr:{max_err}");
        }
Example #4
0
        public void ReferenceTest()
        {
            int inchannels = 6, outchannels = 8, kwidth = 3, stride = 2, inwidth = 13, batch = 3;
            int outwidth = (inwidth - kwidth) / stride + 1;

            float[] xval = (new float[batch * inwidth * inchannels]).Select((_, idx) => idx * 1e-3f).ToArray();
            float[] wval = (new float[kwidth * 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();

            ComplexMap1D    x = new ComplexMap1D(inchannels / 2, inwidth, batch, xcval);
            ComplexFilter1D w = new ComplexFilter1D(inchannels / 2, outchannels / 2, kwidth, wcval);

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

            float[] y_expect =
            {
                -3.240000000e-04f, 5.037000000e-03f, -2.700000000e-04f, 4.119000000e-03f, -2.160000000e-04f, 3.201000000e-03f,
                -1.620000000e-04f, 2.283000000e-03f, -2.160000000e-04f, 1.464900000e-02f, -1.620000000e-04f, 1.243500000e-02f,
                -1.080000000e-04f, 1.022100000e-02f, -5.400000000e-05f, 8.007000000e-03f, -1.080000000e-04f, 2.426100000e-02f,
                -5.400000000e-05f, 2.075100000e-02f,  1.734723476e-18f, 1.724100000e-02f,  5.400000000e-05f, 1.373100000e-02f,
                0.000000000e+00f,  3.387300000e-02f,  5.400000000e-05f, 2.906700000e-02f,  1.080000000e-04f, 2.426100000e-02f,
                1.620000000e-04f,  1.945500000e-02f,  1.080000000e-04f, 4.348500000e-02f,  1.620000000e-04f, 3.738300000e-02f,
                2.160000000e-04f,  3.128100000e-02f,  2.700000000e-04f, 2.517900000e-02f,  2.160000000e-04f, 5.309700000e-02f,
                2.700000000e-04f,  4.569900000e-02f,  3.240000000e-04f, 3.830100000e-02f,  3.780000000e-04f, 3.090300000e-02f,
                3.780000000e-04f,  6.751500000e-02f,  4.320000000e-04f, 5.817300000e-02f,  4.860000000e-04f, 4.883100000e-02f,
                5.400000000e-04f,  3.948900000e-02f,  4.860000000e-04f, 7.712700000e-02f,  5.400000000e-04f, 6.648900000e-02f,
                5.940000000e-04f,  5.585100000e-02f,  6.480000000e-04f, 4.521300000e-02f,  5.940000000e-04f, 8.673900000e-02f,
                6.480000000e-04f,  7.480500000e-02f,  7.020000000e-04f, 6.287100000e-02f,  7.560000000e-04f, 5.093700000e-02f,
                7.020000000e-04f,  9.635100000e-02f,  7.560000000e-04f, 8.312100000e-02f,  8.100000000e-04f, 6.989100000e-02f,
                8.640000000e-04f,  5.666100000e-02f,  8.100000000e-04f, 1.059630000e-01f,  8.640000000e-04f, 9.143700000e-02f,
                9.180000000e-04f,  7.691100000e-02f,  9.720000000e-04f, 6.238500000e-02f,  9.180000000e-04f, 1.155750000e-01f,
                9.720000000e-04f,  9.975300000e-02f,  1.026000000e-03f, 8.393100000e-02f,  1.080000000e-03f, 6.810900000e-02f,
                1.080000000e-03f,  1.299930000e-01f,  1.134000000e-03f, 1.122270000e-01f,  1.188000000e-03f, 9.446100000e-02f,
                1.242000000e-03f,  7.669500000e-02f,  1.188000000e-03f, 1.396050000e-01f,  1.242000000e-03f, 1.205430000e-01f,
                1.296000000e-03f,  1.014810000e-01f,  1.350000000e-03f, 8.241900000e-02f,  1.296000000e-03f, 1.492170000e-01f,
                1.350000000e-03f,  1.288590000e-01f,  1.404000000e-03f, 1.085010000e-01f,  1.458000000e-03f, 8.814300000e-02f,
                1.404000000e-03f,  1.588290000e-01f,  1.458000000e-03f, 1.371750000e-01f,  1.512000000e-03f, 1.155210000e-01f,
                1.566000000e-03f,  9.386700000e-02f,  1.512000000e-03f, 1.684410000e-01f,  1.566000000e-03f, 1.454910000e-01f,
                1.620000000e-03f,  1.225410000e-01f,  1.674000000e-03f, 9.959100000e-02f,  1.620000000e-03f, 1.780530000e-01f,
                1.674000000e-03f,  1.538070000e-01f,  1.728000000e-03f, 1.295610000e-01f,  1.782000000e-03f, 1.053150000e-01f,
            };

            float[] y_actual = y.ToArray();

            AssertError.Tolerance(y_expect, y_actual, 1e-7f, 1e-5f, $"mismatch value {inchannels},{outchannels},{kwidth},{stride},{inwidth},{batch}");
        }
Example #5
0
        public void ReferenceTest()
        {
            int inchannels = 6, outchannels = 8, kwidth = 3, stride = 2, inwidth = 13;
            int outwidth = (inwidth - kwidth) / stride + 1, batch = 1;

            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).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();

            ComplexMap1D x = new ComplexMap1D(inchannels / 2, inwidth, batch, xcval);
            ComplexMap1D y = new ComplexMap1D(outchannels / 2, outwidth, batch, ycval);

            ComplexFilter1D gw = Reference(x, y, kwidth, stride);

            float[] gw_expect =
            {
                6.336000000e-03f, -3.420000000e-04f, 6.972000000e-03f, -3.540000000e-04f, 7.608000000e-03f, -3.660000000e-04f,
                5.604000000e-03f, -3.300000000e-04f, 6.192000000e-03f, -3.420000000e-04f, 6.780000000e-03f, -3.540000000e-04f,
                4.872000000e-03f, -3.180000000e-04f, 5.412000000e-03f, -3.300000000e-04f, 5.952000000e-03f, -3.420000000e-04f,
                4.140000000e-03f, -3.060000000e-04f, 4.632000000e-03f, -3.180000000e-04f, 5.124000000e-03f, -3.300000000e-04f,
                8.244000000e-03f, -3.780000000e-04f, 8.880000000e-03f, -3.900000000e-04f, 9.516000000e-03f, -4.020000000e-04f,
                7.368000000e-03f, -3.660000000e-04f, 7.956000000e-03f, -3.780000000e-04f, 8.544000000e-03f, -3.900000000e-04f,
                6.492000000e-03f, -3.540000000e-04f, 7.032000000e-03f, -3.660000000e-04f, 7.572000000e-03f, -3.780000000e-04f,
                5.616000000e-03f, -3.420000000e-04f, 6.108000000e-03f, -3.540000000e-04f, 6.600000000e-03f, -3.660000000e-04f,
                1.015200000e-02f, -4.140000000e-04f, 1.078800000e-02f, -4.260000000e-04f, 1.142400000e-02f, -4.380000000e-04f,
                9.132000000e-03f, -4.020000000e-04f, 9.720000000e-03f, -4.140000000e-04f, 1.030800000e-02f, -4.260000000e-04f,
                8.112000000e-03f, -3.900000000e-04f, 8.652000000e-03f, -4.020000000e-04f, 9.192000000e-03f, -4.140000000e-04f,
                7.092000000e-03f, -3.780000000e-04f, 7.584000000e-03f, -3.900000000e-04f, 8.076000000e-03f, -4.020000000e-04f,
            };

            float[] gw_actual = gw.ToArray();

            AssertError.Tolerance(gw_expect, gw_actual, 1e-7f, 1e-5f, $"mismatch value {inchannels},{outchannels},{kwidth},{stride},{inwidth},{batch}");
        }
Example #6
0
        public static ComplexFilter1D Reference(ComplexMap1D x, ComplexMap1D gy, int kwidth, int stride)
        {
            int inchannels = x.Channels, outchannels = gy.Channels, batch = x.Batch;
            int inw = x.Width, outw = gy.Width;

            if (outw != (inw - kwidth) / stride + 1)
            {
                throw new ArgumentException("mismatch shape");
            }

            ComplexFilter1D w = new ComplexFilter1D(inchannels, outchannels, kwidth);

            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 kx = 0; kx < kwidth; kx++)
            {
                for (int th = 0; th < batch; th++)
                {
                    for (int inch, outch = 0; outch < outchannels; outch++)
                    {
                        for (inch = 0; inch < inchannels; inch++)
                        {
                            System.Numerics.Complex sum = 0;

                            for (int ix = kx, ox = 0; ox < outw; ix += stride, ox++)
                            {
                                sum += mul_grad(gy[outch, ox, th], x[inch, ix, th]);
                            }

                            w[inch, outch, kx] += sum;
                        }
                    }
                }
            }

            return(w);
        }
        public void ReferenceTest()
        {
            int inchannels = 6, outchannels = 8, kwidth = 3, stride = 2, inwidth = 13, batch = 3;
            int outwidth = (inwidth - kwidth) / stride + 1;

            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();

            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();

            ComplexMap1D    y = new ComplexMap1D(outchannels / 2, outwidth, batch, ycval);
            ComplexFilter1D w = new ComplexFilter1D(inchannels / 2, outchannels / 2, kwidth, wcval);

            ComplexMap1D x = Reference(y, w, inwidth, kwidth, stride);

            float[] x_expect =
            {
                -2.320000000e-04f, 1.604000000e-03f, -2.240000000e-04f, 1.548000000e-03f, -2.160000000e-04f, 1.492000000e-03f,
                -1.360000000e-04f, 9.320000000e-04f, -1.280000000e-04f, 8.760000000e-04f, -1.200000000e-04f, 8.200000000e-04f,
                -2.400000000e-04f, 5.800000000e-03f, -2.240000000e-04f, 5.560000000e-03f, -2.080000000e-04f, 5.320000000e-03f,
                -1.040000000e-04f, 3.332000000e-03f, -9.600000000e-05f, 3.148000000e-03f, -8.800000000e-05f, 2.964000000e-03f,
                -1.760000000e-04f, 1.060000000e-02f, -1.600000000e-04f, 1.010400000e-02f, -1.440000000e-04f, 9.608000000e-03f,
                -7.200000000e-05f, 5.732000000e-03f, -6.400000000e-05f, 5.420000000e-03f, -5.600000000e-05f, 5.108000000e-03f,
                -1.120000000e-04f, 1.540000000e-02f, -9.600000000e-05f, 1.464800000e-02f, -8.000000000e-05f, 1.389600000e-02f,
                -4.000000000e-05f, 8.132000000e-03f, -3.200000000e-05f, 7.692000000e-03f, -2.400000000e-05f, 7.252000000e-03f,
                -4.800000000e-05f, 2.020000000e-02f, -3.200000000e-05f, 1.919200000e-02f, -1.600000000e-05f, 1.818400000e-02f,
                -8.000000000e-06f, 1.053200000e-02f,  0.000000000e+00f, 9.964000000e-03f,  8.000000000e-06f, 9.396000000e-03f,
                1.600000000e-05f,  2.500000000e-02f,  3.200000000e-05f, 2.373600000e-02f,  4.800000000e-05f, 2.247200000e-02f,
                2.400000000e-05f,  1.293200000e-02f,  3.200000000e-05f, 1.223600000e-02f,  4.000000000e-05f, 1.154000000e-02f,
                1.200000000e-04f,  4.580000000e-03f,  1.280000000e-04f, 3.884000000e-03f,  1.360000000e-04f, 3.188000000e-03f,
                -4.000000000e-05f, 2.522000000e-02f, -3.200000000e-05f, 2.439600000e-02f, -2.400000000e-05f, 2.357200000e-02f,
                5.600000000e-05f,  1.533200000e-02f,  6.400000000e-05f, 1.450800000e-02f,  7.200000000e-05f, 1.368400000e-02f,
                1.440000000e-04f,  3.460000000e-02f,  1.600000000e-04f, 3.282400000e-02f,  1.760000000e-04f, 3.104800000e-02f,
                8.800000000e-05f,  1.773200000e-02f,  9.600000000e-05f, 1.678000000e-02f,  1.040000000e-04f, 1.582800000e-02f,
                2.080000000e-04f,  3.940000000e-02f,  2.240000000e-04f, 3.736800000e-02f,  2.400000000e-04f, 3.533600000e-02f,
                1.200000000e-04f,  2.013200000e-02f,  1.280000000e-04f, 1.905200000e-02f,  1.360000000e-04f, 1.797200000e-02f,
                2.720000000e-04f,  4.420000000e-02f,  2.880000000e-04f, 4.191200000e-02f,  3.040000000e-04f, 3.962400000e-02f,
                1.520000000e-04f,  2.253200000e-02f,  1.600000000e-04f, 2.132400000e-02f,  1.680000000e-04f, 2.011600000e-02f,
                3.360000000e-04f,  4.900000000e-02f,  3.520000000e-04f, 4.645600000e-02f,  3.680000000e-04f, 4.391200000e-02f,
                1.840000000e-04f,  2.493200000e-02f,  1.920000000e-04f, 2.359600000e-02f,  2.000000000e-04f, 2.226000000e-02f,
                4.000000000e-04f,  5.380000000e-02f,  4.160000000e-04f, 5.100000000e-02f,  4.320000000e-04f, 4.820000000e-02f,
                2.160000000e-04f,  2.733200000e-02f,  2.240000000e-04f, 2.586800000e-02f,  2.320000000e-04f, 2.440400000e-02f,
                3.120000000e-04f,  9.764000000e-03f,  3.200000000e-04f, 8.300000000e-03f,  3.280000000e-04f, 6.836000000e-03f,
                1.520000000e-04f,  4.883600000e-02f,  1.600000000e-04f, 4.724400000e-02f,  1.680000000e-04f, 4.565200000e-02f,
                2.480000000e-04f,  2.973200000e-02f,  2.560000000e-04f, 2.814000000e-02f,  2.640000000e-04f, 2.654800000e-02f,
                5.280000000e-04f,  6.340000000e-02f,  5.440000000e-04f, 6.008800000e-02f,  5.600000000e-04f, 5.677600000e-02f,
                2.800000000e-04f,  3.213200000e-02f,  2.880000000e-04f, 3.041200000e-02f,  2.960000000e-04f, 2.869200000e-02f,
                5.920000000e-04f,  6.820000000e-02f,  6.080000000e-04f, 6.463200000e-02f,  6.240000000e-04f, 6.106400000e-02f,
                3.120000000e-04f,  3.453200000e-02f,  3.200000000e-04f, 3.268400000e-02f,  3.280000000e-04f, 3.083600000e-02f,
                6.560000000e-04f,  7.300000000e-02f,  6.720000000e-04f, 6.917600000e-02f,  6.880000000e-04f, 6.535200000e-02f,
                3.440000000e-04f,  3.693200000e-02f,  3.520000000e-04f, 3.495600000e-02f,  3.600000000e-04f, 3.298000000e-02f,
                7.200000000e-04f,  7.780000000e-02f,  7.360000000e-04f, 7.372000000e-02f,  7.520000000e-04f, 6.964000000e-02f,
                3.760000000e-04f,  3.933200000e-02f,  3.840000000e-04f, 3.722800000e-02f,  3.920000000e-04f, 3.512400000e-02f,
                7.840000000e-04f,  8.260000000e-02f,  8.000000000e-04f, 7.826400000e-02f,  8.160000000e-04f, 7.392800000e-02f,
                4.080000000e-04f,  4.173200000e-02f,  4.160000000e-04f, 3.950000000e-02f,  4.240000000e-04f, 3.726800000e-02f,
                5.040000000e-04f,  1.494800000e-02f,  5.120000000e-04f, 1.271600000e-02f,  5.200000000e-04f, 1.048400000e-02f,
            };

            float[] x_actual = x.ToArray();

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