You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.
Dismiss alert
if derivative.nelement <>0&& derivative.shape <> t.shape then failwithf "Expecting derivative shape (%A) to match the tensor shape (%A)" derivative.shape t.shape
/// Convert a scalar tensor to a signed byte value
membert.toSByte()= t.toScalar().toSByte()
/// Convert a scalar tensor to an int16 value
membert.toInt16()= t.toScalar().toInt16()
/// Convert a scalar tensor to an int32 value
membert.toInt32()= t.toScalar().toInt32()
/// Convert a scalar tensor to an int64 value
membert.toInt64()= t.toScalar().toInt64()
/// Convert a scalar tensor to a boolean value
membert.toBool()= t.toScalar().toBool()
/// Returns the size in bytes of an individual element in this tensor. Depending on dtype, backend configuration, this is not guaranteed to be correct and can behave differently in different runtime environments.
membert.elementSize=
letbitsPerElement=
match t.backend, t.dtype with
| Backend.Reference, Dtype.BFloat16 ->32// Backed by float32
| Backend.Reference, Dtype.Float16 ->32// Backed by float32
| Backend.Reference, Dtype.Float32 ->32
| Backend.Reference, Dtype.Float64 ->64
| Backend.Reference, Dtype.Int8 ->8
| Backend.Reference, Dtype.Byte ->8
| Backend.Reference, Dtype.Int16 ->16
| Backend.Reference, Dtype.Int32 ->32
| Backend.Reference, Dtype.Int64 ->64
| Backend.Reference, Dtype.Bool ->8// Not reliable https://stackoverflow.com/a/28515361
|_-> failwithf "Unknown backend, dtype configuration to compute memory size"
bitsPerElement /8
/// Returns the size in bytes of the total memory used by this tensor. Depending on dtype, backend configuration, this is not guaranteed to be correct and can behave differently in different runtime environments.
/// <summary>Computes element-wise \(a < b\), returning a boolean tensor containing a <c>true</c> at each location where the comparison is true</summary>
/// <summary>Computes element-wise \(a > b\), returning a boolean tensor containing a <c>true</c> at each location where the comparison is true</summary>
/// <summary>Computes element-wise \(a \leq b\), returning a boolean tensor containing a <c>true</c> at each location where the comparison is true</summary>
/// <summary>Computes element-wise \(a \geq b\), returning a boolean tensor containing a <c>true</c> at each location where the comparison is true</summary>
/// <summary>Computes element-wise \(a = b\), returning a boolean tensor containing a <c>true</c> at each location where the comparison is true</summary>
/// <summary>Computes element-wise \(a \neq b\), returning a boolean tensor containing a <c>true</c> at each location where the comparison is true</summary>
membera.ne(b:Tensor)=lete= a.eq(b)in e.lt(e.onesLike())// Implement "not equal" relying on "equal"
/// <summary>Returns a new tensor with boolean elements representing if each element is +/-INF or not.</summary>
membera.isinf()= TensorC(a.primalRaw.IsInfT())
/// <summary>Returns a new tensor with boolean elements representing if each element is NaN or not. Complex values are considered NaN when either their real and/or imaginary part is NaN.</summary>
/// Gets if any value in the tensor is NaN or +/- INF.
membera.hasinfnan()= a.hasinf()|| a.hasnan()
/// Gets the index of a maximum value in the tensor.
membera.argmax()=
a.primalRaw.MaxIndexT()
/// <summary>Returns the indexes of maximum values of the primal of the tensor, reducing the given dimension.</summary>
/// <remarks>The resulting tensor does not participate in reverse or forward differentiation. It can be used as input to another operation such as <c>dsharp.gather</c>.</remarks>
membera.argmax(dim:int,?keepDim:bool)=
letkeepDim= defaultArg keepDim false
Shape.checkCanMinMaxReduce dim keepDim a.shape |> ignore
/// Gets the index of a minimum value in the tensor.
membera.argmin()=
a.primalRaw.MinIndexT()
/// <summary>Returns the indexes of minimum values of the primal of the tensor, reducing the given dimension.</summary>
/// <remarks>The resulting tensor does not participate in reverse or forward differentiation. It can be used as input to another operation such as <c>dsharp.gather</c>.</remarks>
membera.argmin(dim:int,?keepDim:bool)=
letkeepDim= defaultArg keepDim false
Shape.checkCanMinMaxReduce dim keepDim a.shape |> ignore
// Create a new Tensor from a structure holding scalar Tensors. Maintains differentiability.
letres= value |> DataConverter.tryFlatArrayAndShape<Tensor>
match res with
| Some (tensors, shape)->
letallScalar= tensors |> Array.forall (fun t -> t.dim =0)
ifnot allScalar then failwithf "Combining tensors in an array is only supported where all tensors in the array are scalar (zero-dimensional). Check other operations like stack, cat to combine tensors."