Show / Hide Table of Contents

Class Tensor<T>

Represents a multi-dimensional collection of objects of type T that can be accessed by indices.

Inheritance
Object
TensorBase
Tensor<T>
DenseTensor<T>
Implements
IList
ICollection
IList<T>
ICollection<T>
IReadOnlyList<T>
IReadOnlyCollection<T>
IEnumerable<T>
IEnumerable
IStructuralComparable
IStructuralEquatable
Inherited Members
TensorBase.GetTypeInfo(Type)
TensorBase.GetElementTypeInfo(TensorElementType)
TensorBase.GetTypeInfo()
Object.Equals(Object)
Object.Equals(Object, Object)
Object.GetHashCode()
Object.GetType()
Object.MemberwiseClone()
Object.ReferenceEquals(Object, Object)
Object.ToString()
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 Source

Tensor(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.
True to indicate that the last dimension is most major (farthest apart) and the first dimension is most minor (closest together): akin to column-major in a rank-2 tensor.

| Improve this Doc View Source

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

| Improve this Doc View Source

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.
True to indicate that the last dimension is most major (farthest apart) and the first dimension is most minor (closest together): akin to column-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 Source

Dimensions

Returns a readonly view of the dimensions of this tensor.

Declaration
public ReadOnlySpan<int> Dimensions { get; }
Property Value
Type Description
ReadOnlySpan<Int32>
| Improve this Doc View Source

IsFixedSize

Always fixed size Tensor

Declaration
public bool IsFixedSize { get; }
Property Value
Type Description
Boolean

always true

| Improve this Doc View Source

IsReadOnly

Tensor is not readonly

Declaration
public bool IsReadOnly { get; }
Property Value
Type Description
Boolean

always false

| Improve this Doc View Source

IsReversedStride

True if strides are reversed (AKA Column-major)

Declaration
public bool IsReversedStride { get; }
Property Value
Type Description
Boolean
| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

Length

Total length of the Tensor.

Declaration
public long Length { get; }
Property Value
Type Description
Int64
| Improve this Doc View Source

Rank

Rank of the tensor: number of dimensions.

Declaration
public int Rank { get; }
Property Value
Type Description
Int32
| Improve this Doc View Source

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 Source

Clone()

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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 dimensions with elements initialized to their default value.

| Improve this Doc View Source

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 TResult type initialized to their default value.

Type Parameters
Name Description
TResult

Type contained within the new Tensor. Typically a value type such as int, double, float, etc.

| Improve this Doc View Source

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 dimensions with elements of TResult type initialized to their default value.

Type Parameters
Name Description
TResult

Type contained within the new Tensor. Typically a value type such as int, double, float, etc.

| Improve this Doc View Source

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
| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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
| Improve this Doc View Source

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

| Improve this Doc View Source

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
| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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>
| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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 Source

ICollection<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
| Improve this Doc View Source

ICollection<T>.Clear()

Represents a multi-dimensional collection of objects of type T that can be accessed by indices.

Declaration
void ICollection<T>.Clear()
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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>
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

IList.Clear()

Represents a multi-dimensional collection of objects of type T that can be accessed by indices.

Declaration
void IList.Clear()
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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
| Improve this Doc View Source

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

Implements

System.Collections.IList
System.Collections.ICollection
System.Collections.Generic.IList<T>
System.Collections.Generic.ICollection<T>
System.Collections.Generic.IReadOnlyList<T>
System.Collections.Generic.IReadOnlyCollection<T>
System.Collections.Generic.IEnumerable<T>
System.Collections.IEnumerable
System.Collections.IStructuralComparable
System.Collections.IStructuralEquatable
  • Improve this Doc
  • View Source
In This Article
Back to top