コード例 #1
0
ファイル: UnaryTensorOps.cs プロジェクト: ganik/TorchSharp
        public void FloatTensorTrigonometrics()
        {
            var x1 = FloatTensor.Range(2f, 15f, 1f);

            Assert.IsNotNull(x1);
            Assert.AreEqual(1, x1.Shape.Length);

            var xsin = x1.Sin();

            Assert.AreEqual(x1.Shape.Length, xsin.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xsin.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Sin(x1[i]), (double)xsin[i]));
            }

            var xcos = x1.Cos();

            Assert.AreEqual(x1.Shape.Length, xcos.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xcos.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Cos(x1[i]), (double)xcos[i]));
            }

            var xtan = x1.Tan();

            Assert.AreEqual(x1.Shape.Length, xtan.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xtan.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Tan(x1[i]), (double)xtan[i]));
            }
        }
コード例 #2
0
ファイル: UnaryTensorOps.cs プロジェクト: ganik/TorchSharp
        public void DoubleTensorPower()
        {
            var x1 = DoubleTensor.Range(2f, 15f, 1f);

            Assert.IsNotNull(x1);
            Assert.AreEqual(1, x1.Shape.Length);

            var x2 = x1.Pow(2.0f);

            Assert.AreEqual(x1.Shape.Length, x2.Shape.Length);
            Assert.AreEqual(x1.Shape[0], x2.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Pow(x1[i], 2.0f), x2[i]));
            }

            var x3 = x1.TPow(2.0f);

            Assert.AreEqual(x1.Shape.Length, x3.Shape.Length);
            Assert.AreEqual(x1.Shape[0], x3.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Pow(2.0f, x1[i]), x3[i]));
            }
        }
コード例 #3
0
ファイル: UnaryTensorOps.cs プロジェクト: ganik/TorchSharp
        public void DoubleTensorArcTrigonometrics()
        {
            var x1 = DoubleTensor.Range(0.0f, 0.99f, 0.05f);

            Assert.IsNotNull(x1);
            Assert.AreEqual(1, x1.Shape.Length);

            var xsin = x1.Asin();

            Assert.AreEqual(x1.Shape.Length, xsin.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xsin.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Asin(x1[i]), (double)xsin[i]));
            }

            var xcos = x1.Acos();

            Assert.AreEqual(x1.Shape.Length, xcos.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xcos.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Acos(x1[i]), (double)xcos[i]));
            }

            var xtan = x1.Atan();

            Assert.AreEqual(x1.Shape.Length, xtan.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xtan.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Atan(x1[i]), (double)xtan[i]));
            }
        }
コード例 #4
0
        public void DoubleTensorHyperbolics()
        {
            var x1 = DoubleTensor.Range(2f, 15f, 1f);

            Assert.NotNull(x1);
            Assert.True(1 == x1.Shape.Length);

            var xsin = x1.Sinh();

            Assert.Equal(x1.Shape.Length, xsin.Shape.Length);
            Assert.Equal(x1.Shape[0], xsin.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.True(BasicTensorAPI.IsApproximatelyEqual(Math.Sinh(x1[i]), (double)xsin[i]));
            }

            var xcos = x1.Cosh();

            Assert.Equal(x1.Shape.Length, xcos.Shape.Length);
            Assert.Equal(x1.Shape[0], xcos.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.True(BasicTensorAPI.IsApproximatelyEqual(Math.Cosh(x1[i]), (double)xcos[i]));
            }

            var xtan = x1.Tanh();

            Assert.Equal(x1.Shape.Length, xtan.Shape.Length);
            Assert.Equal(x1.Shape[0], xtan.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.True(BasicTensorAPI.IsApproximatelyEqual(Math.Tanh(x1[i]), (double)xtan[i]));
            }
        }
コード例 #5
0
ファイル: UnaryTensorOps.cs プロジェクト: ganik/TorchSharp
        public void FloatTensorExp()
        {
            var x1 = FloatTensor.Range(2f, 15f, 1f);

            Assert.IsNotNull(x1);
            Assert.AreEqual(1, x1.Shape.Length);

            var x2 = x1.Exp();

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

            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Exp(x1[i]), (double)x2[i]));
            }
        }
コード例 #6
0
ファイル: UnaryTensorOps.cs プロジェクト: ganik/TorchSharp
        public void DoubleTensorLog2()
        {
            var x1 = DoubleTensor.Range(2f, 100f, 1f);

            Assert.IsNotNull(x1);
            Assert.AreEqual(1, x1.Shape.Length);

            var x2 = x1.Log2();

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

            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Log(x1[i], 2.0), (double)x2[i]));
            }
        }
コード例 #7
0
        public void FloatTensorLog()
        {
            var x1 = FloatTensor.Range(2f, 100f, 1f);

            Assert.NotNull(x1);
            Assert.True(1 == x1.Shape.Length);

            var x2 = x1.Log();

            Assert.Equal(x1.Shape.Length, x2.Shape.Length);
            Assert.Equal(x1.Shape[0], x2.Shape[0]);

            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.True(BasicTensorAPI.IsApproximatelyEqual(Math.Log(x1[i]), (double)x2[i]));
            }
        }
コード例 #8
0
ファイル: UnaryTensorOps.cs プロジェクト: ganik/TorchSharp
        public void DoubleTensorUnaries()
        {
            var x1 = DoubleTensor.Range(2f, 15f, 1f);

            Assert.IsNotNull(x1);
            Assert.AreEqual(1, x1.Shape.Length);

            var xsqrt = x1.Sqrt();

            Assert.AreEqual(x1.Shape.Length, xsqrt.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xsqrt.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Sqrt(x1[i]), (double)xsqrt[i]));
            }

            x1 = DoubleTensor.Range(-15f, 15f, .1f);

            var xceil = x1.Ceil();

            Assert.AreEqual(x1.Shape.Length, xceil.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xceil.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Ceiling(x1[i]), (double)xceil[i]));
            }

            var xfloor = x1.Floor();

            Assert.AreEqual(x1.Shape.Length, xfloor.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xfloor.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Floor(x1[i]), (double)xfloor[i]));
            }

            var xabs = x1.Abs();

            Assert.AreEqual(x1.Shape.Length, xabs.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xabs.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Abs(x1[i]), (double)xabs[i]));
            }

            var xneg = x1.neg();

            Assert.AreEqual(x1.Shape.Length, xneg.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xneg.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(-x1[i], (double)xneg[i]));
            }

#if false
            // DISABLED: libtorch appears to round away from zero for N.5, which means that comparing
            //           against Math.Round() will fail.
            x1 = DoubleTensor.Range(0f, 15f, .1f);

            var xrnd = x1.Round();
            Assert.AreEqual(x1.Shape.Length, xrnd.Shape.Length);
            Assert.AreEqual(x1.Shape[0], xrnd.Shape[0]);
            for (var i = 0; i < x1.Shape[0]; ++i)
            {
                Console.WriteLine($"{x1[i]}, {Math.Round(x1[i])}, {(double)xrnd[i]}");
                Assert.IsTrue(BasicTensorAPI.IsApproximatelyEqual(Math.Round(x1[i]), (double)xrnd[i]));
            }
#endif
        }