Exemple #1
0
        /// <summary>
        /// Element-wise arctangent of input / other with consideration of the quadrant.
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public TorchTensor atan2_(TorchTensor other)
        {
            var res = THSTensor_atan2_(handle, other.Handle);

            if (res == IntPtr.Zero)
            {
                Torch.CheckForErrors();
            }
            return(new TorchTensor(res));
        }
Exemple #2
0
        /// <summary>
        /// Solves a linear system of equations with a positive semidefinite matrix to be inverted given its Cholesky factor matrix u.
        /// </summary>
        /// <param name="input2"></param>
        /// <param name="upper"></param>
        /// <returns></returns>
        public TorchTensor cholesky_solve(TorchTensor input2, bool upper = false)
        {
            var res = THSTensor_cholesky_solve(handle, input2.Handle, upper);

            if (res == IntPtr.Zero)
            {
                Torch.CheckForErrors();
            }
            return(new TorchTensor(res));
        }
Exemple #3
0
        /// <summary>
        /// Performs a matrix-vector product of the matrix input and the vector vec.
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        public TorchTensor mv(TorchTensor target)
        {
            var res = THSTensor_mv(handle, target.Handle);

            if (res == IntPtr.Zero)
            {
                Torch.CheckForErrors();
            }
            return(new TorchTensor(res));
        }
Exemple #4
0
        public static TorchTensor Sparse(TorchTensor indices, TorchTensor values, long[] size, string device = "cpu", bool requiresGrad = false)
        {
            TorchTensor.CheckForCUDA(device);

            unsafe
            {
                fixed(long *psizes = size)
                {
                    return(new TorchTensor(THSTensor_sparse(indices.Handle, values.Handle, (IntPtr)psizes, size.Length, (sbyte)ATenScalarMapping.Float, device, requiresGrad)));
                }
            }
        }
Exemple #5
0
        /// <summary>
        ///  Create a new tensor filled with random values taken from a normal distribution with mean 0 and variance 1.
        /// </summary>
        static public TorchTensor RandomN(long[] size, string device = "cpu", bool requiresGrad = false)
        {
            TorchTensor.CheckForCUDA(device);

            unsafe
            {
                fixed(long *psizes = size)
                {
                    return(new TorchTensor(THSTensor_randn((IntPtr)psizes, size.Length, (sbyte)ATenScalarMapping.Byte, device, requiresGrad)));
                }
            }
        }
Exemple #6
0
 public static void Save(this TorchTensor tensor, System.IO.BinaryWriter writer)
 {
     // First, write the type
     writer.Encode((int)tensor.Type);    // 4 bytes
     // Then, the shape.
     writer.Encode(tensor.shape.Length); // 4 bytes
     foreach (var s in tensor.shape)
     {
         writer.Encode(s);                             // n * 8 bytes
     }
     // Then, the data
     writer.Write(tensor.Bytes()); // ElementSize * NumberofElements
 }
        /// <summary>
        /// Computes the dot product of two 1D tensors.
        /// </summary>
        /// <param name="target"></param>
        /// <returns></returns>
        /// <remarks>
        /// The vdot(a, b) function handles complex numbers differently than dot(a, b).
        /// If the first argument is complex, the complex conjugate of the first argument is used for the calculation of the dot product.
        /// </remarks>
        public TorchTensor vdot(TorchTensor target)
        {
            if (shape.Length != 1 || target.shape.Length != 1 || shape[0] != target.shape[0])
            {
                throw new InvalidOperationException("vdot arguments must have the same shape.");
            }
            var res = THSTensor_vdot(handle, target.Handle);

            if (res == IntPtr.Zero)
            {
                Torch.CheckForErrors();
            }
            return(new TorchTensor(res));
        }
Exemple #8
0
        /// <summary>
        ///  Create a new tensor filled with random values taken from a uniform distribution in [0, 1).
        /// </summary>
        static public TorchTensor Random(long[] size, string device = "cpu", bool requiresGrad = false)
        {
            TorchTensor.CheckForCUDA(device);

            unsafe
            {
                fixed(long *psizes = size)
                {
                    var tptr = THSTensor_rand((IntPtr)psizes, size.Length, (sbyte)ATenScalarMapping.Float, device, requiresGrad);

                    Torch.AssertNoErrors();
                    return(new TorchTensor(tptr));
                }
            }
        }
Exemple #9
0
        internal static bool IsIntegral(this TorchTensor tensor)
        {
            switch (tensor.Type)
            {
            case ScalarType.Byte:
            case ScalarType.Int8:
            case ScalarType.Int16:
            case ScalarType.Int32:
            case ScalarType.Int64:
            case ScalarType.Bool:
                return(true);

            default:
                return(false);
            }
        }
Exemple #10
0
        public static void Load(this TorchTensor tensor, System.IO.BinaryReader reader)
        {
            // First, read the type
            var type = (ScalarType)reader.Decode();

            if (type != tensor.Type)
            {
                throw new ArgumentException("Mismatched tensor data types while loading.");
            }

            // Then, the shape
            var shLen = reader.Decode();

            long[] loadedShape = new long[shLen];

            long totalSize = 1;

            for (int i = 0; i < shLen; ++i)
            {
                loadedShape[i] = reader.Decode();
                totalSize     *= loadedShape[i];
            }

            if (!loadedShape.SequenceEqual(tensor.shape))
            {
                throw new ArgumentException("Mismatched tensor shape while loading.");
            }

            //
            // TODO: Fix this so that you can read large tensors. Right now, they are limited to 2GB
            //
            if (totalSize > int.MaxValue)
            {
                throw new NotImplementedException("Loading tensors larger than 2GB");
            }

            tensor.SetBytes(reader.ReadBytes((int)(totalSize * tensor.ElementSize)));
        }
Exemple #11
0
 public bool Equal(TorchTensor target)
 {
     return(THSTensor_equal(handle, target.Handle));
 }
Exemple #12
0
 public void DivInPlace(TorchTensor target)
 {
     THSTensor_div_(handle, target.Handle);
 }
Exemple #13
0
 public TorchTensor Eq(TorchTensor target)
 {
     return(new TorchTensor(THSTensor_eq(handle, target.Handle)));
 }
Exemple #14
0
 public TorchTensor Bmm(TorchTensor batch2)
 {
     return(new TorchTensor(THSTensor_bmm(handle, batch2.Handle)));
 }
Exemple #15
0
 public TorchTensor Div(TorchTensor target)
 {
     return(new TorchTensor(THSTensor_div(handle, target.Handle)));
 }
Exemple #16
0
 public TorchTensor Sub(TorchTensor target)
 {
     return(new TorchTensor(THSTensor_sub(handle, target.Handle)));
 }
Exemple #17
0
 public static short ToInt16(this TorchTensor value) => value.ToScalar().ToInt16();
Exemple #18
0
 public TorchTensor AddInPlace(TorchTensor target, int scalar = 1)
 {
     return(new TorchTensor(THSTensor_add_(handle, scalar, target.Handle)));
 }
Exemple #19
0
 public TorchTensor Addbmm(TorchTensor batch1, TorchTensor batch2, float beta = 1, float alpha = 1)
 {
     return(new TorchTensor(THSTensor_addbmm(handle, batch1.Handle, batch2.Handle, beta, alpha)));
 }
Exemple #20
0
 public static double ToDouble(this TorchTensor value) => value.ToScalar().ToDouble();
Exemple #21
0
 public TorchTensor IndexSelect(long dimension, TorchTensor index)
 {
     return(new TorchTensor(THSTensor_index_select(handle, dimension, index.Handle)));
 }
Exemple #22
0
 public static bool ToBoolean(this TorchTensor value) => value.ToScalar().ToBoolean();
Exemple #23
0
 public static long ToInt64(this TorchTensor value) => value.ToScalar().ToInt64();
Exemple #24
0
 public static int ToInt32(this TorchTensor value) => value.ToScalar().ToInt32();
Exemple #25
0
 public TorchTensor Mul(TorchTensor target)
 {
     return(new TorchTensor(THSTensor_mul(handle, target.Handle)));
 }
Exemple #26
0
 public TorchTensor Addmm(TorchTensor mat1, TorchTensor mat2, float beta, float alpha)
 {
     return(new TorchTensor(THSTensor_addmm(handle, mat1.Handle, mat2.Handle, beta, alpha)));
 }
Exemple #27
0
 public void MulInPlace(TorchTensor target)
 {
     THSTensor_mul_(handle, target.Handle);
 }
Exemple #28
0
 public TorchTensor Baddbmm(TorchTensor batch2, TorchTensor mat, float beta = 1, float alpha = 1)
 {
     return(new TorchTensor(THSTensor_baddbmm(handle, batch2.Handle, mat.Handle, beta, alpha)));
 }
Exemple #29
0
 public void SubInPlace(TorchTensor target)
 {
     THSTensor_sub_(handle, target.Handle);
 }
Exemple #30
0
 public static byte ToByte(this TorchTensor value) => value.ToScalar().ToByte();