Class Tensor<T>
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Implements
Inherited Members
Namespace: Microsoft.ML.OnnxRuntime.Tensors
Assembly: Microsoft.ML.OnnxRuntime.dll
Syntax
public abstract class Tensor<T> : TensorBase, IList, ICollection, IList<T>, ICollection<T>, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable, IStructuralComparable, IStructuralEquatable
Type Parameters
Name | Description |
---|---|
T | type contained within the Tensor. Typically a value type such as int, double, float, etc. |
Constructors
| Improve this Doc View SourceTensor(Array, Boolean)
Initializes tensor with same dimensions as array, content of array is ignored.
ReverseStride=true gives a stride of 1-element width to the first dimension (0).
ReverseStride=false gives a stride of 1-element width to the last dimension (n-1).
Declaration
protected Tensor(Array fromArray, bool reverseStride)
Parameters
Type | Name | Description |
---|---|---|
Array | fromArray | Array from which to derive dimensions. |
Boolean | reverseStride | False (default) to indicate that the first dimension is most major (farthest apart) and the
last dimension is most minor (closest together): akin to row-major in a rank-2 tensor. |
Tensor(Int32)
Initialize a 1-dimensional tensor of the specified length
Declaration
protected Tensor(int length)
Parameters
Type | Name | Description |
---|---|---|
Int32 | length | Size of the 1-dimensional tensor |
Tensor(ReadOnlySpan<Int32>, Boolean)
Initialize an n-dimensional tensor with the specified dimensions and layout.
ReverseStride=true gives a stride of 1-element width to the first dimension (0).
ReverseStride=false gives a stride of 1-element width to the last dimension (n-1).
Declaration
protected Tensor(ReadOnlySpan<int> dimensions, bool reverseStride)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<Int32> | dimensions | An span of integers that represent the size of each dimension of the Tensor to create. |
Boolean | reverseStride | False (default) to indicate that the first dimension is most major (farthest apart) and the last dimension
is most minor (closest together): akin to row-major in a rank-2 tensor. |
Remarks
If you pass null
for dimensions it will implicitly convert to an empty ReadOnlySpan, which is
equivalent to the dimensions for a scalar value.
Properties
| Improve this Doc View SourceDimensions
Returns a readonly view of the dimensions of this tensor.
Declaration
public ReadOnlySpan<int> Dimensions { get; }
Property Value
Type | Description |
---|---|
ReadOnlySpan<Int32> |
IsFixedSize
Always fixed size Tensor
Declaration
public bool IsFixedSize { get; }
Property Value
Type | Description |
---|---|
Boolean | always true |
IsReadOnly
Tensor is not readonly
Declaration
public bool IsReadOnly { get; }
Property Value
Type | Description |
---|---|
Boolean | always false |
IsReversedStride
True if strides are reversed (AKA Column-major)
Declaration
public bool IsReversedStride { get; }
Property Value
Type | Description |
---|---|
Boolean |
Item[Int32[]]
Obtains the value at the specified indices
Declaration
public virtual T this[params int[] indices] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
Int32[] | indices | A one-dimensional array of integers that represent the indices specifying the position of the element to get. |
Property Value
Type | Description |
---|---|
T | The value at the specified position in this Tensor. |
Item[ReadOnlySpan<Int32>]
Obtains the value at the specified indices
Declaration
public virtual T this[ReadOnlySpan<int> indices] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<Int32> | indices | A span integers that represent the indices specifying the position of the element to get. |
Property Value
Type | Description |
---|---|
T | The value at the specified position in this Tensor. |
Length
Total length of the Tensor.
Declaration
public long Length { get; }
Property Value
Type | Description |
---|---|
Int64 |
Rank
Rank of the tensor: number of dimensions.
Declaration
public int Rank { get; }
Property Value
Type | Description |
---|---|
Int32 |
Strides
Returns a readonly view of the strides of this tensor.
Declaration
public ReadOnlySpan<int> Strides { get; }
Property Value
Type | Description |
---|---|
ReadOnlySpan<Int32> |
Methods
| Improve this Doc View SourceClone()
Creates a shallow copy of this tensor, with new backing storage.
Declaration
public abstract Tensor<T> Clone()
Returns
Type | Description |
---|---|
Tensor<T> | A shallow copy of this tensor. |
CloneEmpty()
Creates a new Tensor with the same layout and dimensions as this tensor with elements initialized to their default value.
Declaration
public virtual Tensor<T> CloneEmpty()
Returns
Type | Description |
---|---|
Tensor<T> | A new Tensor with the same layout and dimensions as this tensor with elements initialized to their default value. |
CloneEmpty(ReadOnlySpan<Int32>)
Creates a new Tensor with the specified dimensions and the same layout as this tensor with elements initialized to their default value.
Declaration
public virtual Tensor<T> CloneEmpty(ReadOnlySpan<int> dimensions)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<Int32> | dimensions | An span of integers that represent the size of each dimension of the DenseTensor to create. |
Returns
Type | Description |
---|---|
Tensor<T> | A new Tensor with the same layout as this tensor and specified |
CloneEmpty<TResult>()
Creates a new Tensor of a different type with the same layout and size as this tensor with elements initialized to their default value.
Declaration
public virtual Tensor<TResult> CloneEmpty<TResult>()
Returns
Type | Description |
---|---|
Tensor<TResult> | A new Tensor with the same layout and dimensions as this tensor with elements of |
Type Parameters
Name | Description |
---|---|
TResult | Type contained within the new Tensor. Typically a value type such as int, double, float, etc. |
CloneEmpty<TResult>(ReadOnlySpan<Int32>)
Creates a new Tensor of a different type with the specified dimensions and the same layout as this tensor with elements initialized to their default value.
Declaration
public abstract Tensor<TResult> CloneEmpty<TResult>(ReadOnlySpan<int> dimensions)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<Int32> | dimensions | An span of integers that represent the size of each dimension of the DenseTensor to create. |
Returns
Type | Description |
---|---|
Tensor<TResult> | A new Tensor with the same layout as this tensor of specified |
Type Parameters
Name | Description |
---|---|
TResult | Type contained within the new Tensor. Typically a value type such as int, double, float, etc. |
Compare(Tensor<T>, Tensor<T>)
Performs a value comparison of the content and shape of two tensors. Two tensors are equal if they have the same shape and same value at every set of indices. If not equal a tensor is greater or less than another tensor based on the first non-equal element when enumerating in linear order.
Declaration
public static int Compare(Tensor<T> left, Tensor<T> right)
Parameters
Type | Name | Description |
---|---|---|
Tensor<T> | left | |
Tensor<T> | right |
Returns
Type | Description |
---|---|
Int32 |
Contains(T)
Determines whether an element is in the Tensor<T>.
Declaration
protected virtual bool Contains(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item | The object to locate in the Tensor<T>. The value can be null for reference types. |
Returns
Type | Description |
---|---|
Boolean | true if item is found in the Tensor<T>; otherwise, false. |
CopyTo(T[], Int32)
Copies the elements of the Tensor<T> to an Array, starting at a particular Array index.
Declaration
protected virtual void CopyTo(T[] array, int arrayIndex)
Parameters
Type | Name | Description |
---|---|---|
T[] | array | The one-dimensional Array that is the destination of the elements copied from Tensor<T>. The Array must have zero-based indexing. |
Int32 | arrayIndex | The zero-based index in array at which copying begins. |
Equals(Tensor<T>, Tensor<T>)
Performs a value equality comparison of the content of two tensors. Two tensors are equal if they have the same shape and same value at every set of indices.
Declaration
public static bool Equals(Tensor<T> left, Tensor<T> right)
Parameters
Type | Name | Description |
---|---|---|
Tensor<T> | left | |
Tensor<T> | right |
Returns
Type | Description |
---|---|
Boolean |
Fill(T)
Sets all elements in Tensor to value
.
Declaration
public virtual void Fill(T value)
Parameters
Type | Name | Description |
---|---|---|
T | value | Value to fill |
GetArrayString(Boolean)
Get a string representation of Tensor
Declaration
public string GetArrayString(bool includeWhitespace = true)
Parameters
Type | Name | Description |
---|---|---|
Boolean | includeWhitespace |
Returns
Type | Description |
---|---|
String |
GetDiagonal()
Gets the n-1 dimension diagonal from the n dimension tensor.
Declaration
public Tensor<T> GetDiagonal()
Returns
Type | Description |
---|---|
Tensor<T> | An n-1 dimension tensor with the values from the main diagonal of this tensor. |
GetDiagonal(Int32)
Gets the n-1 dimension diagonal from the n dimension tensor at the specified offset from center.
Declaration
public Tensor<T> GetDiagonal(int offset)
Parameters
Type | Name | Description |
---|---|---|
Int32 | offset | Offset of diagonal to set in returned tensor. 0 for the main diagonal, less than zero for diagonals below, greater than zero from diagonals above. |
Returns
Type | Description |
---|---|
Tensor<T> | An n-1 dimension tensor with the values from the specified diagonal of this tensor. |
GetTriangle()
Gets a tensor representing the elements below and including the diagonal, with the rest of the elements zero-ed.
Declaration
public Tensor<T> GetTriangle()
Returns
Type | Description |
---|---|
Tensor<T> | A tensor with the values from this tensor at and below the main diagonal and zeros elsewhere. |
GetTriangle(Int32)
Gets a tensor representing the elements below and including the specified diagonal, with the rest of the elements zero-ed.
Declaration
public Tensor<T> GetTriangle(int offset)
Parameters
Type | Name | Description |
---|---|---|
Int32 | offset | Offset of diagonal to set in returned tensor. 0 for the main diagonal, less than zero for diagonals below, greater than zero from diagonals above. |
Returns
Type | Description |
---|---|
Tensor<T> | A tensor with the values from this tensor at and below the specified diagonal and zeros elsewhere. |
GetTriangle(Int32, Boolean)
Implementation method for GetTriangle, GetLowerTriangle, GetUpperTriangle
Declaration
public Tensor<T> GetTriangle(int offset, bool upper)
Parameters
Type | Name | Description |
---|---|---|
Int32 | offset | Offset of diagonal to set in returned tensor. |
Boolean | upper | true for upper triangular and false otherwise |
Returns
Type | Description |
---|---|
Tensor<T> |
GetUpperTriangle()
Gets a tensor representing the elements above and including the diagonal, with the rest of the elements zero-ed.
Declaration
public Tensor<T> GetUpperTriangle()
Returns
Type | Description |
---|---|
Tensor<T> | A tensor with the values from this tensor at and above the main diagonal and zeros elsewhere. |
GetUpperTriangle(Int32)
Gets a tensor representing the elements above and including the specified diagonal, with the rest of the elements zero-ed.
Declaration
public Tensor<T> GetUpperTriangle(int offset)
Parameters
Type | Name | Description |
---|---|---|
Int32 | offset | Offset of diagonal to set in returned tensor. 0 for the main diagonal, less than zero for diagonals below, greater than zero from diagonals above. |
Returns
Type | Description |
---|---|
Tensor<T> | A tensor with the values from this tensor at and above the specified diagonal and zeros elsewhere. |
GetValue(Int32)
Gets the value at the specified index, where index is a linearized version of n-dimension indices using strides.
Declaration
public abstract T GetValue(int index)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | An integer index computed as a dot-product of indices. |
Returns
Type | Description |
---|---|
T | The value at the specified position in this Tensor. |
IndexOf(T)
Determines the index of a specific item in the Tensor<T>.
Declaration
protected virtual int IndexOf(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item | The object to locate in the Tensor<T>. |
Returns
Type | Description |
---|---|
Int32 | The index of item if found in the tensor; otherwise, -1. |
Reshape(ReadOnlySpan<Int32>)
Reshapes the current tensor to new dimensions, using the same backing storage if possible.
Declaration
public abstract Tensor<T> Reshape(ReadOnlySpan<int> dimensions)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySpan<Int32> | dimensions | An span of integers that represent the size of each dimension of the Tensor to create. |
Returns
Type | Description |
---|---|
Tensor<T> | A new tensor that reinterprets this tensor with different dimensions. |
SetValue(Int32, T)
Sets the value at the specified index, where index is a linearized version of n-dimension indices using strides.
Declaration
public abstract void SetValue(int index, T value)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | An integer index computed as a dot-product of indices. |
T | value | The new value to set at the specified position in this Tensor. |
ToDenseTensor()
Creates a copy of this tensor as a DenseTensor<T>. If this tensor is already a DenseTensor<T> calling this method is equivalent to calling Clone().
Declaration
public virtual DenseTensor<T> ToDenseTensor()
Returns
Type | Description |
---|---|
DenseTensor<T> |
Explicit Interface Implementations
| Improve this Doc View SourceICollection<T>.Add(T)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
void ICollection<T>.Add(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item |
ICollection<T>.Clear()
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
void ICollection<T>.Clear()
ICollection<T>.Contains(T)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
bool ICollection<T>.Contains(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item |
Returns
Type | Description |
---|---|
Boolean |
ICollection<T>.CopyTo(T[], Int32)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
void ICollection<T>.CopyTo(T[] array, int arrayIndex)
Parameters
Type | Name | Description |
---|---|---|
T[] | array | |
Int32 | arrayIndex |
ICollection<T>.Count
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
int ICollection<T>.Count { get; }
Returns
Type | Description |
---|---|
Int32 |
ICollection<T>.Remove(T)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
bool ICollection<T>.Remove(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item |
Returns
Type | Description |
---|---|
Boolean |
IEnumerable<T>.GetEnumerator()
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
IEnumerator<T> IEnumerable<T>.GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator<T> |
IList<T>.IndexOf(T)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
int IList<T>.IndexOf(T item)
Parameters
Type | Name | Description |
---|---|---|
T | item |
Returns
Type | Description |
---|---|
Int32 |
IList<T>.Insert(Int32, T)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
void IList<T>.Insert(int index, T item)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | |
T | item |
IList<T>.Item[Int32]
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
T IList<T>.this[int index] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
Int32 | index |
Returns
Type | Description |
---|---|
T |
IList<T>.RemoveAt(Int32)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
void IList<T>.RemoveAt(int index)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index |
IReadOnlyCollection<T>.Count
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
int IReadOnlyCollection<T>.Count { get; }
Returns
Type | Description |
---|---|
Int32 |
IReadOnlyList<T>.Item[Int32]
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
T IReadOnlyList<T>.this[int index] { get; }
Parameters
Type | Name | Description |
---|---|---|
Int32 | index |
Returns
Type | Description |
---|---|
T |
ICollection.CopyTo(Array, Int32)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
void ICollection.CopyTo(Array array, int index)
Parameters
Type | Name | Description |
---|---|---|
Array | array | |
Int32 | index |
ICollection.Count
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
int ICollection.Count { get; }
Returns
Type | Description |
---|---|
Int32 |
ICollection.IsSynchronized
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
bool ICollection.IsSynchronized { get; }
Returns
Type | Description |
---|---|
Boolean |
ICollection.SyncRoot
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
object ICollection.SyncRoot { get; }
Returns
Type | Description |
---|---|
Object |
IEnumerable.GetEnumerator()
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
IEnumerator IEnumerable.GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator |
IList.Add(Object)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
int IList.Add(object value)
Parameters
Type | Name | Description |
---|---|---|
Object | value |
Returns
Type | Description |
---|---|
Int32 |
IList.Clear()
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
void IList.Clear()
IList.Contains(Object)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
bool IList.Contains(object value)
Parameters
Type | Name | Description |
---|---|---|
Object | value |
Returns
Type | Description |
---|---|
Boolean |
IList.IndexOf(Object)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
int IList.IndexOf(object value)
Parameters
Type | Name | Description |
---|---|---|
Object | value |
Returns
Type | Description |
---|---|
Int32 |
IList.Insert(Int32, Object)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
void IList.Insert(int index, object value)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | |
Object | value |
IList.Item[Int32]
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
object IList.this[int index] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
Int32 | index |
Returns
Type | Description |
---|---|
Object |
IList.Remove(Object)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
void IList.Remove(object value)
Parameters
Type | Name | Description |
---|---|---|
Object | value |
IList.RemoveAt(Int32)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
void IList.RemoveAt(int index)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index |
IStructuralComparable.CompareTo(Object, IComparer)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
int IStructuralComparable.CompareTo(object other, IComparer comparer)
Parameters
Type | Name | Description |
---|---|---|
Object | other | |
IComparer | comparer |
Returns
Type | Description |
---|---|
Int32 |
IStructuralEquatable.Equals(Object, IEqualityComparer)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
bool IStructuralEquatable.Equals(object other, IEqualityComparer comparer)
Parameters
Type | Name | Description |
---|---|---|
Object | other | |
IEqualityComparer | comparer |
Returns
Type | Description |
---|---|
Boolean |
IStructuralEquatable.GetHashCode(IEqualityComparer)
Represents a multi-dimensional collection of objects of type T that can be accessed by indices.
Declaration
int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
Parameters
Type | Name | Description |
---|---|---|
IEqualityComparer | comparer |
Returns
Type | Description |
---|---|
Int32 |