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
tensorATensor<T>The first tensor.
tensorBTensor<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
tensorTensor<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
sizeintThe size of the 1D tensor to create.
Returns
- Tensor<T>
A new 1D tensor filled with ones.
Type Parameters
TThe 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
shapeint[]The shape of the tensor to create.
stddevdoubleStandard deviation for the random initialization.
randomRandomThe random number generator to use.
Returns
- Tensor<T>
A new tensor initialized with random values scaled by stddev.
Type Parameters
TThe 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
tensorTensor<T>The tensor to iterate through.
actionFunc<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
TThe 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
fanInintThe 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
aTensor<T>bTensor<T>
Returns
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
tensorTensor<T>The tensor to populate with values.
flattenedValuesVector<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
TThe 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
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.