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, bool)

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.

bool 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(int)

Initialize a 1-dimensional tensor of the specified length

Declaration
protected Tensor(int length)
Parameters
Type Name Description
int length

Size of the 1-dimensional tensor

| Improve this Doc View Source

Tensor(ReadOnlySpan<int>, bool)

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<int> dimensions

An span of integers that represent the size of each dimension of the Tensor to create.

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

IsFixedSize

Always fixed size Tensor

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

always true

| Improve this Doc View Source

IsReadOnly

Tensor is not readonly

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

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

this[int[]]

Obtains the value at the specified indices

Declaration
public virtual T this[params int[] indices] { get; set; }
Parameters
Type Name Description
int[] 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

this[ReadOnlySpan<int>]

Obtains the value at the specified indices

Declaration
public virtual T this[ReadOnlySpan<int> indices] { get; set; }
Parameters
Type Name Description
ReadOnlySpan<int> 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
long
| Improve this Doc View Source

Rank

Rank of the tensor: number of dimensions.

Declaration
public int Rank { get; }
Property Value
Type Description
int
| 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<int>

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<int>)

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<int> 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<int>)

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<int> 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
int
| 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
bool

true if item is found in the Tensor<T>; otherwise, false.

| Improve this Doc View Source

CopyTo(T[], int)

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.

int 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
bool
| 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(bool)

Get a string representation of Tensor

Declaration
public string GetArrayString(bool includeWhitespace = true)
Parameters
Type Name Description
bool 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(int)

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
int 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(int)

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
int 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(int, bool)

Implementation method for GetTriangle, GetLowerTriangle, GetUpperTriangle

Declaration
public Tensor<T> GetTriangle(int offset, bool upper)
Parameters
Type Name Description
int offset

Offset of diagonal to set in returned tensor.

bool 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(int)

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
int 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(int)

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
int 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
int

The index of item if found in the tensor; otherwise, -1.

| Improve this Doc View Source

Reshape(ReadOnlySpan<int>)

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<int> 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(int, 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
int 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>

Implements

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