Ejemplo n.º 1
0
        public static void ReshapeIP(this Session session, Tensor x, Shape shape)
        {
            const string ActionName = "reshape";

            session.RunOperation(
                ActionName,
                () =>
            {
#if !NOLEARNING
                if (session.CalculateGradients && x.CalculateGradient)
                {
                    Shape oldshape = new Shape(x.Shape);
                    if (x.Reshape(shape))
                    {
                        session.Push(ActionName, () => x.Reshape(oldshape));
                    }
                }
                else
#endif
                {
                    x.Reshape(shape);
                }

                return((Tensor)null);       // have to return something
            });
        }
Ejemplo n.º 2
0
        public unsafe void FullyConnectedBackwardData()
        {
            FullyConnectedLayer fc = new FullyConnectedLayer(TensorInfo.Linear(231), 125, ActivationType.Sigmoid, WeightsInitializationMode.GlorotUniform, BiasInitializationMode.Gaussian);
            Tensor dy = CreateRandomTensor(400, fc.OutputInfo.Size);

            fixed(float *pw = fc.Weights, pb = fc.Biases)
            {
                Tensor.Reshape(pw, fc.InputInfo.Size, fc.OutputInfo.Size, out Tensor w);
                Tensor.Reshape(pb, 1, fc.OutputInfo.Size, out Tensor b);
                Tensor.New(dy.Entities, fc.InputInfo.Size, out Tensor dx1);
                CpuDnn.FullyConnectedBackwardData(w, dy, dx1);
                Gpu gpu = Gpu.Default;

                using (DeviceMemory <float>
                       dy_gpu = gpu.AllocateDevice(dy),
                       w_gpu = gpu.AllocateDevice(w),
                       dx_gpu = gpu.AllocateDevice <float>(dx1.Size))
                {
                    Dnn.Get(gpu).FullyConnectedBackwardData(dy.Entities, fc.InputInfo.Size, fc.OutputInfo.Size, dy_gpu.Ptr, w_gpu.Ptr, dx_gpu.Ptr);
                    dx_gpu.CopyToHost(dx1.Entities, dx1.Length, out Tensor dx2);
                    Assert.IsTrue(dx1.ContentEquals(dx2));
                    Tensor.Free(dy, dx1, dx2);
                }
            }
        }
Ejemplo n.º 3
0
 public unsafe void ConvolutionFull1()
 {
     float[,]
     l =
     {
         {
             0, 1,
             -1, 2
         }
     },
     k =
     {
         {
             1, 0,
             1, 1
         }
     };
     float[,] expected =
     {
         {
             0, 1, 1,
             -1, 1, 3,
             0, -1, 2
         }
     };
     fixed(float *pl = l, pk = k)
     {
         Tensor.Reshape(pl, 1, 4, out Tensor lTensor);
         Tensor.Reshape(pk, 1, 4, out Tensor kTensor);
         Tensor.New(1, 9, out Tensor result);
         CpuDnn.ConvolutionBackwardData(lTensor, new TensorInfo(2, 2, 1), kTensor, new TensorInfo(2, 2, 1), result, new TensorInfo(3, 3, 1));
         Assert.IsTrue(result.ToArray2D().ContentEquals(expected));
         result.Free();
     }
 }
Ejemplo n.º 4
0
 public unsafe void HadamardProductTest()
 {
     // Test values
     float[,]
     m1 =
     {
         { 1, 2,  3 },
         { 5, 1, -2 },
         { 1, 2,  3 },
     },
     m2 =
     {
         {  5, 2, -1 },
         { -5, 2, -7 },
         {  1, 2,  2 }
     },
     r =
     {
         {   5, 4, -3 },
         { -25, 2, 14 },
         {   1, 4,  6 }
     };
     fixed(float *pm1 = m1, pm2 = m2)
     {
         Tensor.Reshape(pm1, 3, 3, out Tensor m1Tensor);
         Tensor.Reshape(pm2, 3, 3, out Tensor m2Tensor);
         Tensor.New(3, 3, out Tensor result);
         CpuBlas.MultiplyElementwise(m1Tensor, m2Tensor, result);
         Assert.IsTrue(result.ToArray2D().ContentEquals(r));
         result.Free();
     }
 }
Ejemplo n.º 5
0
 public unsafe void SpatialMultiplication()
 {
     // Test values
     float[,]
     m1 =
     {
         { 1,    2,  3 },
         { 5, 0.1f, -2 }
     },
     m2 =
     {
         {    5,    2,    -1,    3 },
         {   -5,    2,    -7, 0.9f },
         { 0.1f, 0.2f, -0.1f,    2 }
     },
     r =
     {
         { -4.7f,                6.6f, -15.3f,  10.8f },
         { 24.3f, 9.7999999999999989f,  -5.5f, 11.09f }
     };
     fixed(float *pm1 = m1, pm2 = m2)
     {
         Tensor.Reshape(pm1, 2, 3, out Tensor m1Tensor);
         Tensor.Reshape(pm2, 3, 4, out Tensor m2Tensor);
         Tensor.New(2, 4, out Tensor result);
         CpuBlas.Multiply(m1Tensor, m2Tensor, result);
         Assert.IsTrue(result.ToArray2D().ContentEquals(r));
         result.Free();
     }
 }
Ejemplo n.º 6
0
 public unsafe void Transposition()
 {
     // Test values
     float[,]
     m =
     {
         { 1, 1,  1, 1 },
         { 0, 2, -1, 0 }
     },
     r =
     {
         { 1,  0 },
         { 1,  2 },
         { 1, -1 },
         { 1,  0 }
     };
     fixed(float *pm = m)
     {
         Tensor.Reshape(pm, 2, 4, out Tensor mTensor);
         Tensor.New(4, 2, out Tensor result);
         CpuBlas.Transpose(mTensor, result);
         Assert.IsTrue(result.ToArray2D().ContentEquals(r));
         result.Free();
     }
 }
Ejemplo n.º 7
0
        public void TestDefaultConstructor()
        {
            Tensor <int> defaultTensor = default(Tensor <int>);

            Assert.Equal(0, defaultTensor.Buffer.Length);
            Assert.Equal(0, defaultTensor.Dimensions.Count);
            Assert.True(defaultTensor.IsFixedSize);
            Assert.False(defaultTensor.IsReadOnly);
            Assert.Equal(0, defaultTensor.Length);
            Assert.Equal(0, defaultTensor.Rank);

            // don't throw
            var clone = defaultTensor.Clone();

            clone = defaultTensor.CloneEmpty();
            var doubleClone = defaultTensor.CloneEmpty <double>();
            var arr         = new int[0];

            defaultTensor.CopyTo(arr, 0);
            defaultTensor.Fill(0);
            defaultTensor.Reshape();
            defaultTensor.ReshapeCopy();
            defaultTensor.ToString();

            Assert.Equal(default(Tensor <int>), defaultTensor);
            Assert.Equal(default(Tensor <int>).GetHashCode(), defaultTensor.GetHashCode());
            Assert.False(defaultTensor.GetEnumerator().MoveNext());

            // rank is too small
            Assert.Throws <InvalidOperationException>(() => defaultTensor.GetDiagonal());
            Assert.Throws <InvalidOperationException>(() => defaultTensor.GetTriangle());
            Assert.Throws <InvalidOperationException>(() => defaultTensor.GetUpperTriangle());

            Assert.Throws <ArgumentOutOfRangeException>(() => defaultTensor[0]);
            Assert.Throws <ArgumentOutOfRangeException>(() => defaultTensor[0, 0]);
            Assert.Throws <ArgumentOutOfRangeException>(() => defaultTensor[0, 0, 0]);

            Assert.Throws <ArgumentException>("tensor", () => defaultTensor + 1);
            Assert.Throws <ArgumentException>("left", () => defaultTensor + defaultTensor);
            Assert.Throws <ArgumentException>("tensor", () => + defaultTensor);
            Assert.Throws <ArgumentException>("tensor", () => defaultTensor - 1);
            Assert.Throws <ArgumentException>("left", () => defaultTensor - defaultTensor);
            Assert.Throws <ArgumentException>("tensor", () => - defaultTensor);
            Assert.Throws <ArgumentException>("tensor", () => defaultTensor++);
            Assert.Throws <ArgumentException>("tensor", () => defaultTensor--);
            Assert.Throws <ArgumentException>("tensor", () => defaultTensor * 1);
            Assert.Throws <ArgumentException>("left", () => defaultTensor * defaultTensor);
            Assert.Throws <ArgumentException>("tensor", () => defaultTensor / 1);
            Assert.Throws <ArgumentException>("left", () => defaultTensor / defaultTensor);
            Assert.Throws <ArgumentException>("tensor", () => defaultTensor % 1);
            Assert.Throws <ArgumentException>("left", () => defaultTensor % defaultTensor);
            Assert.Throws <ArgumentException>("left", () => defaultTensor & defaultTensor);
            Assert.Throws <ArgumentException>("tensor", () => defaultTensor & -1);
            Assert.Throws <ArgumentException>("left", () => defaultTensor | defaultTensor);
            Assert.Throws <ArgumentException>("tensor", () => defaultTensor | -1);
            Assert.Throws <ArgumentException>("left", () => defaultTensor ^ defaultTensor);
            Assert.Throws <ArgumentException>("tensor", () => defaultTensor ^ -1);
            Assert.Throws <ArgumentException>("tensor", () => defaultTensor >> 1);
            Assert.Throws <ArgumentException>("tensor", () => defaultTensor << 1);
        }
        public unsafe void SoftmaxBackwardOutput()
        {
            float[,]
            x = WeightsProvider.NewFullyConnectedWeights(TensorInfo.Linear(400), 250, WeightsInitializationMode.GlorotNormal).AsSpan().AsMatrix(400, 250),
            y = new float[400, 127];
            for (int i = 0; i < 400; i++)
            {
                y[i, ThreadSafeRandom.NextInt(max: 127)] = 1;
            }
            OutputLayerBase
                cpu = new SoftmaxLayer(TensorInfo.Linear(250), 127, WeightsInitializationMode.GlorotNormal, BiasInitializationMode.Gaussian),
                gpu = new CuDnnSoftmaxLayer(cpu.InputInfo, cpu.OutputInfo.Size, cpu.Weights, cpu.Biases);

            fixed(float *px = x, py = y)
            {
                Tensor.Reshape(px, x.GetLength(0), x.GetLength(1), out Tensor xt);
                cpu.Forward(xt, out Tensor z, out Tensor a);
                a.Duplicate(out Tensor a2);
                Tensor.Reshape(py, y.GetLength(0), y.GetLength(1), out Tensor yt);
                cpu.Backpropagate(a, yt, z);
                gpu.Backpropagate(a2, yt, z);
                Assert.IsTrue(a.ContentEquals(a2));
                a.Free();
                a2.Free();
                z.Free();
            }
        }
Ejemplo n.º 9
0
        public void Reshape1()
        {
            //arrange
            var t1 = new Tensor(new double[, , , ] {
                { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } }, { { { 9, 10 }, { 11, 12 } }, { { 13, 14 }, { 15, 16 } } }
            });
            var r1 = new double[, , , ] {
                { { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 } } }
            };

            //act
            var t2 = t1.Reshape(1, 1, 1, 16);

            // assert
            for (int b = 0; b < r1.GetLength(0); b++)
            {
                for (int d = 0; d < r1.GetLength(1); d++)
                {
                    for (int r = 0; r < r1.GetLength(2); r++)
                    {
                        for (int c = 0; c < r1.GetLength(3); c++)
                        {
                            Assert.Equal(r1[b, d, r, c], t2[b, d, r, c]);
                        }
                    }
                }
            }
        }
    void Start()
    {
        m_DataSet = LoadDataSet();

        var batch = m_DataSet.Item1[0].batch;

        m_SirenBuilder = new SirenModel(batch, false, 256, 3,
                                        8, 2, 6,
                                        "vector_observation", "continuous_actions", false);
        m_model      = m_SirenBuilder.model;
        m_parameters = m_SirenBuilder.parameters;

        if (loadModelFromOnnx)
        {
            m_model = ModelLoader.Load(model, false);
        }
        m_worker = WorkerFactory.CreateWorker(ms_workerType, m_model, false);

        m_input  = m_DataSet.Item1[9];
        m_target = m_DataSet.Item2[9];

        m_lr = new Tensor(1, 1, new[] { learningRate });

        InitPlot();

        var t = m_target.Reshape(new TensorShape(1, 100, 200, 1));

        t.ToRenderTexture(targetRT, batch: 0, fromChannel: 0);

        m_lastUpdateTime = Time.realtimeSinceStartup;

        StartCoroutine(TrainingLoop());
    }
        private static unsafe void TestBackward(OutputLayerBase cpu, OutputLayerBase gpu, float[,] y)
        {
            SetBackpropagationProperty(true);
            int n = y.GetLength(0);

            fixed(float *p = y)
            {
                Tensor.Reshape(p, n, y.GetLength(1), out Tensor yTensor);
                Tensor
                    x  = CreateRandomTensor(n, cpu.InputInfo.Size),
                    dy = CreateRandomTensor(n, cpu.OutputInfo.Size);

                Tensor.Like(x, out Tensor dx1);
                Tensor.Like(x, out Tensor dx2);
                cpu.Forward(x, out Tensor z_cpu, out Tensor a_cpu);
                gpu.Forward(x, out Tensor z_gpu, out Tensor a_gpu);
                cpu.Backpropagate(x, a_cpu, yTensor, z_cpu, dx1, out Tensor dJdw_cpu, out Tensor dJdb_cpu);
                gpu.Backpropagate(x, a_cpu, yTensor, z_cpu, dx2, out Tensor dJdw_gpu, out Tensor dJdb_gpu);
                Assert.IsTrue(dx1.ContentEquals(dx2));
                Assert.IsTrue(dJdw_cpu.ContentEquals(dJdw_gpu));
                Assert.IsTrue(dJdb_cpu.ContentEquals(dJdb_gpu, 1e-4f, 1e-5f));
                Tensor.Free(x, dy, dx1, dx2, z_cpu, a_cpu, z_gpu, a_gpu, dJdw_cpu, dJdw_gpu, dJdb_cpu, dJdb_gpu);
            }

            SetBackpropagationProperty(false);
        }
Ejemplo n.º 12
0
        public unsafe void FullyConnectedForward()
        {
            FullyConnectedLayer fc = new FullyConnectedLayer(TensorInfo.Linear(231), 125, ActivationType.Sigmoid, WeightsInitializationMode.GlorotUniform, BiasInitializationMode.Gaussian);
            Tensor x = CreateRandomTensor(400, fc.InputInfo.Size);

            fixed(float *pw = fc.Weights, pb = fc.Biases)
            {
                Tensor.Reshape(pw, fc.InputInfo.Size, fc.OutputInfo.Size, out Tensor w);
                Tensor.Reshape(pb, 1, fc.OutputInfo.Size, out Tensor b);
                Tensor.New(x.Entities, fc.OutputInfo.Size, out Tensor y1);
                CpuDnn.FullyConnectedForward(x, w, b, y1);
                Gpu gpu = Gpu.Default;

                using (DeviceMemory <float>
                       x_gpu = gpu.AllocateDevice(x),
                       w_gpu = gpu.AllocateDevice(w),
                       b_gpu = gpu.AllocateDevice(b),
                       y_gpu = gpu.AllocateDevice <float>(y1.Size))
                {
                    Dnn.Get(gpu).FullyConnectedForward(x.Entities, x.Length, y1.Length, x_gpu.Ptr, w_gpu.Ptr, b_gpu.Ptr, y_gpu.Ptr);
                    y_gpu.CopyToHost(y1.Entities, y1.Length, out Tensor y2);
                    Assert.IsTrue(y1.ContentEquals(y2));
                    Tensor.Free(x, y1, y2);
                }
            }
        }
Ejemplo n.º 13
0
        public override void Forward(Tensor x)
        {
            base.Forward(x);
            var(n, c, h, w) = x.GetConv2DShape();

            int pad = 0;

            if (Padding == PaddingType.Same)
            {
                pad = 1;
            }
            else if (Padding == PaddingType.Full)
            {
                pad = 2;
            }

            var h_out = (h - PoolSize.Item1 + 2 * pad) / Strides + 1;
            var w_out = (w - PoolSize.Item2 + 2 * pad) / Strides + 1;

            var x_reshaped = x.Reshape(n * c, 1, h, w);

            xCols  = K.Im2Col(x_reshaped, PoolSize, pad, Strides);
            Output = K.Max(xCols, 0);
            Output = Output.Reshape(h_out, w_out, n, c).Transpose(2, 3, 0, 1);
        }
Ejemplo n.º 14
0
        public void Reshape2()
        {
            //arrange
            var t1 = new Tensor(new double[, , , ] {
                { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } }, { { { 9, 10 }, { 11, 12 } }, { { 13, 14 }, { 15, 16 } } }
            });
            var r2 = new double[, , , ] {
                { { { 1 }, { 2 }, { 3 }, { 4 }, { 5 }, { 6 }, { 7 }, { 8 }, { 9 }, { 10 }, { 11 }, { 12 }, { 13 }, { 14 }, { 15 }, { 16 } } }
            };

            //act
            var t3 = t1.Reshape(1, 1, 16, 1);

            // assert
            for (int b = 0; b < r2.GetLength(0); b++)
            {
                for (int d = 0; d < r2.GetLength(1); d++)
                {
                    for (int r = 0; r < r2.GetLength(2); r++)
                    {
                        for (int c = 0; c < r2.GetLength(3); c++)
                        {
                            Assert.Equal(r2[b, d, r, c], t3[b, d, r, c]);
                        }
                    }
                }
            }
        }
Ejemplo n.º 15
0
        public unsafe void Compress2()
        {
            // Test values
            float[,]
            m =
            {
                {
                    1, 2, 3,
                    4, 5, 6,
                    7, 8, 9,

                    1, 99, 3,
                    4, 5, 6,
                    7, 8, 9
                },
                {
                    1, 2, 3,
                    4, 5, 66,
                    7, 8, 9,

                    1, 2, 3,
                    44, 5, 6,
                    7, 8, 9
                }
            };
            float[] r = { 150, 227 };
            fixed(float *pm = m)
            {
                Tensor.Reshape(pm, 2, 18, out Tensor mTensor);
                Tensor.New(1, 2, out Tensor v);
                CpuDnn.ConvolutionBackwardBias(mTensor, new TensorInfo(3, 3, 2), v);
                Assert.IsTrue(v.ToArray().ContentEquals(r));
                v.Free();
            }
        }
Ejemplo n.º 16
0
 public unsafe void Pool2()
 {
     // Test values
     float[,]
     m =
     {
         {
             0.77f, -0.11f, 0.11f, 0.33f, 0.55f, -0.11f, 0.33f,
             -0.11f, 1, -0.11f, 0.33f, -0.11f, 0.11f, -0.11f,
             0.11f, -0.11f, 1, -0.33f, 0.11f, -0.11f, 0.55f,
             0.33f, 0.33f, -0.33f, 0.55f, -0.33f, 0.33f, 0.33f,
             0.55f, -0.11f, 0.11f, -0.33f, 1, -0.11f, 0.11f,
             -0.11f, 0.11f, -0.11f, 0.33f, -0.11f, 1, -0.11f,
             0.33f, -0.11f, 0.55f, 0.33f, 0.11f, -0.11f, 0.77f
         }
     },
     r =
     {
         {
             1, 0.33f, 0.55f, 0.33f,
             0.33f, 1, 0.33f, 0.55f,
             0.55f, 0.33f, 1, 0.11f,
             0.33f, 0.55f, 0.11f, 0.77f
         }
     };
     fixed(float *pm = m)
     {
         Tensor.Reshape(pm, 1, 49, out Tensor mTensor);
         Tensor.New(1, 16, out Tensor result);
         CpuDnn.PoolingForward(mTensor, TensorInfo.Image <Alpha8>(7, 7), result);
         Assert.IsTrue(result.ToArray2D().ContentEquals(r));
         result.Free();
     }
 }
Ejemplo n.º 17
0
 public unsafe void Convolution2DValidRectangle1()
 {
     float[,]
     l =
     {
         {
             0, 1, 0,
             2, 0, 1
         }
     },
     k =
     {
         {
             1, 1,
             0, 1
         }
     };
     float[] b = { 0.9f };
     float[,] expected =
     {
         {
             2.9f, 2.9f
         }
     };
     fixed(float *pl = l, pk = k, pb = b)
     {
         Tensor.Reshape(pl, 1, 6, out Tensor lTensor);
         Tensor.Reshape(pk, 1, 4, out Tensor kTensor);
         Tensor.Reshape(pb, 1, 1, out Tensor bTensor);
         Tensor.New(1, 2, out Tensor result);
         CpuDnn.ConvolutionForward(lTensor, new TensorInfo(2, 3, 1), kTensor, new TensorInfo(2, 2, 1), bTensor, result);
         Assert.IsTrue(result.ToArray2D().ContentEquals(expected));
         result.Free();
     }
 }
        public void TestReshapeShort1DFail()
        {
            Tensor <short> x = ShortTorchTensor.Create(10);

            x.Fill((short)1);

            Assert.Throws <ArgumentException>(() => x.Reshape(new int[] { 9 }));
        }
        public void TestReshapeFloat1DFail()
        {
            Tensor <float> x = FloatTorchTensor.Create(10);

            x.Fill((float)1);

            Assert.Throws <ArgumentException>(() => x.Reshape(new int[] { 9 }));
        }
Ejemplo n.º 20
0
        public void TestReshapeInt1DFail()
        {
            Tensor <int> x = IntAtenTensor.Create(10);

            x.Fill((int)1);

            Assert.Throws <ArgumentException>(() => x.Reshape(new int[] { 9 }));
        }
Ejemplo n.º 21
0
        public void TestReshapeDouble1DFail()
        {
            Tensor <double> x = DoubleAtenTensor.Create(10);

            x.Fill((double)1);

            Assert.Throws <ArgumentException>(() => x.Reshape(new int[] { 9 }));
        }
Ejemplo n.º 22
0
        public void TestReshapeLong1DFail()
        {
            Tensor <long> x = LongAtenTensor.Create(10);

            x.Fill((long)1);

            Assert.Throws <ArgumentException>(() => x.Reshape(new int[] { 9 }));
        }
        public unsafe void InceptionPoolPipeline()
        {
            float[,] x = WeightsProvider.NewFullyConnectedWeights(TensorInfo.Linear(10), 12 * 12 * 3, WeightsInitializationMode.GlorotNormal).AsSpan().AsMatrix(10, 12 * 12 * 3);
            CuDnnPoolingLayer       pool      = new CuDnnPoolingLayer(TensorInfo.Image <Rgb24>(12, 12), PoolingInfo.New(PoolingMode.Max, 3, 3, 1, 1, 1, 1), ActivationType.ReLU);
            CuDnnConvolutionalLayer conv      = new CuDnnConvolutionalLayer(pool.OutputInfo, ConvolutionInfo.New(ConvolutionMode.CrossCorrelation), (1, 1), 10, ActivationType.ReLU, BiasInitializationMode.Gaussian);
            CuDnnInceptionLayer     inception = new CuDnnInceptionLayer(TensorInfo.Image <Rgb24>(12, 12), InceptionInfo.New(3, 2, 2, 2, 2, PoolingMode.Max, 10));

            fixed(float *pw = inception.Weights)
            Unsafe.InitBlock(pw, 0, (uint)(sizeof(float) * inception.Weights.Length));

            Buffer.BlockCopy(conv.Weights, 0, inception.Weights, sizeof(float) * (3 * 3 + 3 * 2 + 3 * 3 * 2 * 2 + 3 * 2 + 5 * 5 * 2 * 2), sizeof(float) * conv.Weights.Length);
            Buffer.BlockCopy(conv.Biases, 0, inception.Biases, sizeof(float) * (3 + 2 + 2 + 2 + 2), sizeof(float) * conv.Biases.Length);
            fixed(float *px = x)
            {
                // Forward + Z
                Tensor.Reshape(px, x.GetLength(0), x.GetLength(1), out Tensor xTensor);
                pool.Forward(xTensor, out Tensor zTemp, out Tensor aTemp);
                conv.Forward(aTemp, out Tensor zConv, out Tensor aConv);
                inception.Forward(xTensor, out Tensor zInc, out Tensor aInc);
                Tensor.New(zConv.Entities, zConv.Length, out Tensor reshaped);
                float *pzInc = (float *)zInc.Ptr.ToPointer() + 12 * 12 * (3 + 2 + 2), preshaped = (float *)reshaped.Ptr.ToPointer();

                for (int i = 0; i < zConv.Entities; i++)
                {
                    Buffer.MemoryCopy(pzInc + i * zInc.Length, preshaped + i * zConv.Length, sizeof(float) * zConv.Length, sizeof(float) * zConv.Length);
                }
                Assert.IsTrue(reshaped.ContentEquals(zConv));

                // A
                float *paInc = (float *)aInc.Ptr.ToPointer() + 12 * 12 * (3 + 2 + 2);

                for (int i = 0; i < aConv.Entities; i++)
                {
                    Buffer.MemoryCopy(paInc + i * aInc.Length, preshaped + i * aConv.Length, sizeof(float) * aConv.Length, sizeof(float) * aConv.Length);
                }
                Assert.IsTrue(reshaped.ContentEquals(aConv));

                // Backpropagation
                Tensor.Like(aTemp, out Tensor convdx);
                Tensor.Like(xTensor, out Tensor pooldx);
                Tensor.Like(xTensor, out Tensor incdx);
                conv.Backpropagate(aTemp, zConv, aConv, convdx, out Tensor convdJdw, out Tensor convdJdb);
                pool.Backpropagate(xTensor, zTemp, convdx, pooldx);
                inception.Backpropagate(xTensor, zInc, aInc, incdx, out Tensor incdJdw, out Tensor incdJdb);
                Assert.IsTrue(incdx.ContentEquals(pooldx));

                // Gradient
                Tensor.Reshape((float *)incdJdw.Ptr.ToPointer() + (3 * 3 + 3 * 2 + 3 * 3 * 2 * 2 + 3 * 2 + 5 * 5 * 2 * 2), 1, convdJdw.Size, out Tensor dJdwInc0);
                Tensor.Reshape((float *)incdJdb.Ptr.ToPointer() + 11, 1, convdJdb.Size, out Tensor dJdbInc0);
                Assert.IsTrue(convdJdw.ContentEquals(dJdwInc0, 1e-5f));
                Assert.IsTrue(convdJdb.ContentEquals(dJdbInc0, 1e-5f));

                // Cleanup
                Tensor.Free(zTemp, aTemp, zConv, aConv, zInc, aInc, reshaped, convdx, pooldx, incdx, convdJdw, convdJdb, incdJdw, incdJdb);
            }
        }
Ejemplo n.º 24
0
        public void MinMaxNormTest()
        {
            Tensor x = Tensor.FromArray(Global.Device, new float[] { -1, 2, 3, -4, 5, 6, 7, -8, 9 });

            x = x.Reshape(3, -1);
            Constraints.MinMaxNorm constraint = new Constraints.MinMaxNorm(0, 2, 2, null);
            var w = constraint.Call(x);

            w.Print();
        }
Ejemplo n.º 25
0
        public void NonNegTest()
        {
            Tensor x = Tensor.FromArray(Global.Device, new float[] { -1, 2, 3, -4, 5, 6, 7, -8, 9 });

            x = x.Reshape(3, -1);
            Constraints.NonNeg constraint = new Constraints.NonNeg();
            var w = constraint.Call(x);

            w.Print();
        }
Ejemplo n.º 26
0
        public void MaxNormTest()
        {
            Tensor x = Tensor.FromArray(Global.Device, new float[] { -1, 2, 3, -4, 5, 6, 7, -8, 9 });

            x = x.Reshape(3, -1);
            Constraints.MaxNorm maxNorm = new Constraints.MaxNorm(2, 0);
            var w = maxNorm.Call(x);

            w.Print();
        }
Ejemplo n.º 27
0
        public unsafe void Pool5()
        {
            // Test values
            float[,]
            m =
            {
                {
                    -1, 0, 1, 2,
                    1, 1, 1, 1,
                    0, -0.3f, -5, -0.5f,
                    -1, 10, -2, -1,

                    -1, 0, 1, 2,
                    1, 1, 1, 1,
                    0, -0.3f, -5, 1.2f,
                    -1, 10, -2, -1
                },
                {
                    -1, 0, 1, 2,
                    1, 1, 1, 1,
                    0, -0.3f, -5, 1.2f,
                    -1, 10, -2, -1,

                    -1, 0, 1, 2,
                    1, 1, 1, 1,
                    0, -0.3f, -5, 1.45f,
                    -1, 10, -2, -1
                }
            },
            r =
            {
                {
                    1, 2,
                    10, -0.5f,

                    1, 2,
                    10, 1.2f
                },
                {
                    1, 2,
                    10, 1.2f,

                    1, 2,
                    10, 1.45f
                },
            };
            fixed(float *pm = m)
            {
                Tensor.Reshape(pm, 2, 32, out Tensor mTensor);
                Tensor.New(2, 8, out Tensor result);
                CpuDnn.PoolingForward(mTensor, new TensorInfo(2, 2, 2), result);
                Assert.IsTrue(result.ToArray2D().ContentEquals(r));
                result.Free();
            }
        }
Ejemplo n.º 28
0
        public unsafe void Convolution2DValid5()
        {
            float[,]
            l =
            {
                {
                    0, 1, 0,
                    2, 0, 1,
                    1, 1, 0,

                    1, 0, 0,
                    0, 2, 1,
                    0, 1, 1
                }
            },
            k =
            {
                {
                    1, 1,
                    0, 1,

                    0, 1,
                    1, 0
                },
                {
                    1, 1,
                    0, 1,

                    0, 1,
                    1, 0
                }
            };
            float[] b = { 0, 0.2f };
            float[,] expected =
            {
                {
                    2, 4,
                    6, 3,

                    2.2f, 4.2f,
                    6.2f, 3.2f
                }
            };
            fixed(float *pl = l, pk = k, pb = b)
            {
                Tensor.Reshape(pl, 1, 18, out Tensor lTensor);
                Tensor.Reshape(pk, 2, 8, out Tensor kTensor);
                Tensor.Reshape(pb, 1, 2, out Tensor bTensor);
                Tensor.New(1, 8, out Tensor result);
                CpuDnn.ConvolutionForward(lTensor, new TensorInfo(3, 3, 2), kTensor, new TensorInfo(2, 2, 2), bTensor, result);
                Assert.IsTrue(result.ToArray2D().ContentEquals(expected));
                result.Free();
            }
        }
Ejemplo n.º 29
0
        private static Tensor <T> Reshape2D <T>(Tensor <T> a, int dimensionsAsRows)
        {
            if (dimensionsAsRows < 0)
            {
                dimensionsAsRows += a.NDim;
            }
            var before = a.Shape.Take(dimensionsAsRows).Aggregate((Dim)1, (x, y) => x * y);
            var after  = a.Shape.Skip(dimensionsAsRows).Aggregate((Dim)1, (x, y) => x * y);

            return(a.Reshape(new[] { before, after }));
        }
 private static unsafe Tensor CreateRandomTensor(int entities, int length)
 {
     float[] v = WeightsProvider.NewFullyConnectedWeights(TensorInfo.Linear(entities), length, WeightsInitializationMode.GlorotNormal);
     Tensor.New(entities, length, out Tensor tensor);
     fixed(float *pv = v)
     {
         Tensor.Reshape(pv, entities, length, out Tensor source);
         tensor.Overwrite(source);
         return(tensor);
     }
 }
Ejemplo n.º 31
0
        public void Blob_Reshape()
        {
            var blob = new Tensor();
            blob.Reshape(2, 3, 4, 5);
            Assert.Equal(2, blob.Num);
            Assert.Equal(3, blob.Channels);
            Assert.Equal(4, blob.Height);
            Assert.Equal(5, blob.Width);
            Assert.Equal(120, blob.Count);

            using (var blobCpu = blob.OnCpu())
            {
                Assert.NotNull(blobCpu.Data);
                Assert.NotNull(blobCpu.Diff);
                Assert.Equal(blobCpu.Count, blobCpu.Data.Count);
                Assert.Equal(blobCpu.Count, blobCpu.Diff.Count);
            }
        }