Table of Contents

Class TensorExtensions

Namespace
AiDotNet.Extensions
Assembly
AiDotNet.dll
public static class TensorExtensions
Inheritance
TensorExtensions
Inherited Members

Methods

ConcatenateTensors<T>(Tensor<T>, Tensor<T>)

Concatenates two tensors along the last dimension.

public static Tensor<T> ConcatenateTensors<T>(this Tensor<T> tensorA, Tensor<T> tensorB)

Parameters

tensorA Tensor<T>

The first tensor.

tensorB Tensor<T>

The second tensor.

Returns

Tensor<T>

The concatenated tensor.

Type Parameters

T

ConvertToMatrix<T>(Tensor<T>)

Converts a tensor to a matrix for various calculations.

public static Matrix<T> ConvertToMatrix<T>(this Tensor<T> tensor)

Parameters

tensor Tensor<T>

The tensor to convert.

Returns

Matrix<T>

A matrix representation of the tensor.

Type Parameters

T

Remarks

Helper method for converting tensors to matrices for mathematical operations.

CreateOnesTensor<T>(int)

Creates a tensor initialized with all ones.

public static Tensor<T> CreateOnesTensor<T>(int size)

Parameters

size int

The size of the 1D tensor to create.

Returns

Tensor<T>

A new 1D tensor filled with ones.

Type Parameters

T

The numeric type of the tensor elements.

Remarks

This is commonly used for initializing layer normalization gamma parameters.

CreateXavierInitializedTensor<T>(int[], double, Random)

Creates and initializes a tensor with Xavier/Glorot initialization using random values.

public static Tensor<T> CreateXavierInitializedTensor<T>(int[] shape, double stddev, Random random)

Parameters

shape int[]

The shape of the tensor to create.

stddev double

Standard deviation for the random initialization.

random Random

The random number generator to use.

Returns

Tensor<T>

A new tensor initialized with random values scaled by stddev.

Type Parameters

T

The numeric type of the tensor elements.

Remarks

This method implements Xavier/Glorot initialization, which helps neural networks train more effectively by keeping the variance of activations consistent across layers.

For Beginners: When training neural networks, the initial values of weights matter a lot. Xavier initialization chooses random values that aren't too big or too small, helping the network learn faster and more reliably.

ForEachPosition<T>(Tensor<T>, Func<int[], T, bool>)

Iterates through all positions in the tensor and applies a function to each position.

public static void ForEachPosition<T>(this Tensor<T> tensor, Func<int[], T, bool> action)

Parameters

tensor Tensor<T>

The tensor to iterate through.

action Func<int[], T, bool>

The action to apply at each position. Takes the position array and current value as parameters. Return true to continue iteration, false to stop.

Type Parameters

T

The numeric type of the tensor elements.

Remarks

This method provides a way to iterate through all positions in a tensor and perform an operation at each position. The action receives the current position (as an array of indices) and the current value at that position.

For Beginners: This is like visiting every cell in a spreadsheet one by one and performing some action at each cell. You can use this to transform, inspect, or extract data from the tensor.

HeStddev(int)

Calculates the He standard deviation for weight initialization with ReLU activations.

public static double HeStddev(int fanIn)

Parameters

fanIn int

The number of input units.

Returns

double

The standard deviation to use for initialization.

Remarks

He initialization (Kaiming initialization) uses sqrt(2 / fanIn), which is designed for networks with ReLU activations. For sigmoid/tanh activations, consider using Xavier/Glorot initialization with sqrt(1 / fanIn) or sqrt(2 / (fanIn + fanOut)).

TensorEquals<T>(Tensor<T>, Tensor<T>)

public static bool TensorEquals<T>(this Tensor<T> a, Tensor<T> b)

Parameters

a Tensor<T>
b Tensor<T>

Returns

bool

Type Parameters

T

Unflatten<T>(Tensor<T>, Vector<T>)

Converts a flattened vector back into a tensor with the specified shape.

public static Tensor<T> Unflatten<T>(this Tensor<T> tensor, Vector<T> flattenedValues)

Parameters

tensor Tensor<T>

The tensor to populate with values.

flattenedValues Vector<T>

The flattened vector containing values to reshape into the tensor.

Returns

Tensor<T>

The tensor populated with the values from the flattened vector.

Type Parameters

T

The numeric type of the tensor elements.

Remarks

This method takes a flattened vector and reshapes it back into a tensor with the shape of the current tensor. It's the inverse operation of flattening a tensor into a vector.

For Beginners: This is like taking a long line of numbers and arranging them back into a multi-dimensional structure (like rows and columns). It's similar to taking a string of text and formatting it back into paragraphs.

XavierStddev(int, int)

Calculates the Xavier/Glorot standard deviation for weight initialization.

public static double XavierStddev(int fanIn, int fanOut)

Parameters

fanIn int

The number of input units.

fanOut int

The number of output units.

Returns

double

The standard deviation to use for initialization.

Remarks

Xavier/Glorot initialization uses sqrt(2 / (fanIn + fanOut)), which is designed to maintain gradient variance across layers with sigmoid/tanh activations.