/// <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)); }
/// <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)); }
/// <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)); }
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))); } } }
/// <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))); } } }
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)); }
/// <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)); } } }
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); } }
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))); }
public bool Equal(TorchTensor target) { return(THSTensor_equal(handle, target.Handle)); }
public void DivInPlace(TorchTensor target) { THSTensor_div_(handle, target.Handle); }
public TorchTensor Eq(TorchTensor target) { return(new TorchTensor(THSTensor_eq(handle, target.Handle))); }
public TorchTensor Bmm(TorchTensor batch2) { return(new TorchTensor(THSTensor_bmm(handle, batch2.Handle))); }
public TorchTensor Div(TorchTensor target) { return(new TorchTensor(THSTensor_div(handle, target.Handle))); }
public TorchTensor Sub(TorchTensor target) { return(new TorchTensor(THSTensor_sub(handle, target.Handle))); }
public static short ToInt16(this TorchTensor value) => value.ToScalar().ToInt16();
public TorchTensor AddInPlace(TorchTensor target, int scalar = 1) { return(new TorchTensor(THSTensor_add_(handle, scalar, target.Handle))); }
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))); }
public static double ToDouble(this TorchTensor value) => value.ToScalar().ToDouble();
public TorchTensor IndexSelect(long dimension, TorchTensor index) { return(new TorchTensor(THSTensor_index_select(handle, dimension, index.Handle))); }
public static bool ToBoolean(this TorchTensor value) => value.ToScalar().ToBoolean();
public static long ToInt64(this TorchTensor value) => value.ToScalar().ToInt64();
public static int ToInt32(this TorchTensor value) => value.ToScalar().ToInt32();
public TorchTensor Mul(TorchTensor target) { return(new TorchTensor(THSTensor_mul(handle, target.Handle))); }
public TorchTensor Addmm(TorchTensor mat1, TorchTensor mat2, float beta, float alpha) { return(new TorchTensor(THSTensor_addmm(handle, mat1.Handle, mat2.Handle, beta, alpha))); }
public void MulInPlace(TorchTensor target) { THSTensor_mul_(handle, target.Handle); }
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))); }
public void SubInPlace(TorchTensor target) { THSTensor_sub_(handle, target.Handle); }
public static byte ToByte(this TorchTensor value) => value.ToScalar().ToByte();