Пример #1
0
        public void DiagFloat()
        {
            var x1 = new FloatTensor(9);

            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                x1[i] = i + 1;
            }

            x1.Resize2d(3, 3);

            var diag0 = x1.Diagonal(0);
            var diag1 = x1.Diagonal(1);

            Assert.AreEqual(1, diag0.Shape.Length);
            Assert.AreEqual(3, diag0.Shape[0]);
            Assert.AreEqual(1, diag1.Shape.Length);
            Assert.AreEqual(2, diag1.Shape[0]);

            Assert.AreEqual(1, diag0[0]);
            Assert.AreEqual(5, diag0[1]);
            Assert.AreEqual(9, diag0[2]);

            Assert.AreEqual(2, diag1[0]);
            Assert.AreEqual(6, diag1[1]);
        }
Пример #2
0
        public void ReshapeFloatTensor()
        {
            var x2 = new FloatTensor(200);

            Assert.AreEqual(1, x2.Shape.Length);
            Assert.AreEqual(200, x2.Shape[0]);

            for (var i = 0; i < x2.Shape[0]; ++i)
            {
                x2[i] = i * 47.11f;
            }

            x2.Resize2d(10, 20);
            Assert.AreEqual(2, x2.Shape.Length);
            Assert.AreEqual(10, x2.Shape[0]);
            Assert.AreEqual(20, x2.Shape[1]);

            x2.Resize3d(4, 25, 2);
            Assert.AreEqual(3, x2.Shape.Length);
            Assert.AreEqual(4, x2.Shape[0]);
            Assert.AreEqual(25, x2.Shape[1]);
            Assert.AreEqual(2, x2.Shape[2]);

            x2.Resize4d(4, 5, 5, 2);
            Assert.AreEqual(4, x2.Shape.Length);
            Assert.AreEqual(4, x2.Shape[0]);
            Assert.AreEqual(5, x2.Shape[1]);
            Assert.AreEqual(5, x2.Shape[2]);
            Assert.AreEqual(2, x2.Shape[3]);

            x2.Resize5d(2, 2, 5, 5, 2);
            Assert.AreEqual(5, x2.Shape.Length);
            Assert.AreEqual(2, x2.Shape[0]);
            Assert.AreEqual(2, x2.Shape[1]);
            Assert.AreEqual(5, x2.Shape[2]);
            Assert.AreEqual(5, x2.Shape[3]);
            Assert.AreEqual(2, x2.Shape[4]);

            // Check that the values are retained across resizings.

            x2.Resize1d(200);
            Assert.AreEqual(1, x2.Shape.Length);
            Assert.AreEqual(200, x2.Shape[0]);

            for (var i = 0; i < x2.Shape[0]; ++i)
            {
                Assert.AreEqual(i * 47.11f, x2[i]);
            }
        }