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

System.Object
TensorBase
Tensor<T>
DenseTensor<T>

##### Implements

IList
IList<T>
IReadOnlyList<T>
IStructuralComparable
IStructuralEquatable

##### Inherited Members

TensorBase.GetTypeInfo(Type)
TensorBase.GetElementTypeInfo(TensorElementType)
TensorBase.GetTypeInfo()
System.Object.ToString()
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()

###### Namespace: Microsoft.ML.OnnxRuntime.Tensors

###### Assembly: cs.temp.dll.dll

##### Syntax

    public abstract class Tensor<T> : TensorBase

##### Type Parameters

Name | Description
--- | ---
T | type contained within the Tensor. Typically a value type such as int, double, float, etc.

### Constructors

#### 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.
System.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.

#### 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<System.Int32> | dimensions | An span of integers that represent the size of each dimension of the Tensor to create.
System.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.

#### Tensor(Int32)

Initialize a 1-dimensional tensor of the specified length

##### Declaration

    protected Tensor(int length)

##### Parameters

Type | Name | Description
--- | --- | ---
System.Int32 | length | Size of the 1-dimensional tensor

### Properties

#### Dimensions

Returns a readonly view of the dimensions of this tensor.

##### Declaration

    public ReadOnlySpan<int> Dimensions { get; }

##### Property Value

Type | Description
--- | ---
ReadOnlySpan<System.Int32> |

#### IsFixedSize

Always fixed size Tensor

##### Declaration

    public bool IsFixedSize { get; }

##### Property Value

Type | Description
--- | ---
System.Boolean | always true

#### IsReadOnly

Tensor is not readonly

##### Declaration

    public bool IsReadOnly { get; }

##### Property Value

Type | Description
--- | ---
System.Boolean | always false

#### IsReversedStride

True if strides are reversed (AKA Column-major)

##### Declaration

    public bool IsReversedStride { get; }

##### Property Value

Type | Description
--- | ---
System.Boolean |

#### 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<System.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.

#### Item[Int32[]]

Obtains the value at the specified indices

##### Declaration

    public virtual T this[params int[] indices] { get; set; }

##### Parameters

Type | Name | Description
--- | --- | ---
System.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.

#### Length

Total length of the Tensor.

##### Declaration

    public long Length { get; }

##### Property Value

Type | Description
--- | ---
System.Int64 |

#### Rank

Rank of the tensor: number of dimensions.

##### Declaration

    public int Rank { get; }

##### Property Value

Type | Description
--- | ---
System.Int32 |

#### Strides

Returns a readonly view of the strides of this tensor.

##### Declaration

    public ReadOnlySpan<int> Strides { get; }

##### Property Value

Type | Description
--- | ---
ReadOnlySpan<System.Int32> |

### Methods

#### 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.

#### 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<System.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.

#### 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.

#### 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<System.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.

#### 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
--- | ---
System.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
--- | ---
System.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.
System.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
--- | ---
System.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
--- | --- | ---
System.Boolean | includeWhitespace |

##### Returns

Type | Description
--- | ---
System.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
--- | --- | ---
System.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
--- | --- | ---
System.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
--- | --- | ---
System.Int32 | offset | Offset of diagonal to set in returned tensor.
System.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
--- | --- | ---
System.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 specied index, where index is a linearized version of n-dimension indices using strides.

##### Declaration

    public abstract T GetValue(int index)

##### Parameters

Type | Name | Description
--- | --- | ---
System.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
--- | ---
System.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<System.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 specied 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
--- | --- | ---
System.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> |

### Implements

IList
IList<>
IReadOnlyList<>
IStructuralComparable
IStructuralEquatable