Tensor at a glance
This page provides an overview of most commonly used tensor functions by category.
For a complete, alphabetical reference of all tensor functions see Tensor<'T> and the device-specific functions in HostTensor and CudaTensor.
Creation functions
Use these functions to create a new tensor.
Function | Description |
---|---|
arange | Creates a new vector filled with equaly spaced values using a specifed increment. |
counting | Creates a new vector filled with the integers from zero to the specified maximum. |
empty | Creates a new, empty tensor with the given number of dimensions. |
falses | Creates a new boolean tensor filled with falses. |
filled | Creates a new tensor filled with the specified value. |
identity | Creates a new identity matrix. |
ones | Creates a new tensor filled with ones (1). |
onesLike | Creates a new tensor filled with ones using the specified tensor as template. |
linspace | Creates a new vector of given size filled with equaly spaced values. |
scalar | Creates a new zero-dimensional (scalar) tensor with the specified value. |
scalarLike | Creates a new zero-dimensional (scalar) tensor using the specified tensor as template and with the specified value. |
trues | Creates a new boolean tensor filled with trues. |
zeros | Creates a new tensor filled with zeros (0). |
zerosLike | Creates a new tensor filled with zeros using the specified tensor as template. |
Slicing and element access functions
Use these functions to slice tensors or access individual elements of them.
Function | Description |
---|---|
Item | Accesses a slice (part) of the tensor. |
M | Picks elements from a tensor using one or more boolean mask tensors. |
Value | Gets the value of a zero-dimensional (scalar) tensor. |
Element-wise operations
These mathematical operations are applied element-wise to each element of the tensor(s).
Function | Description |
---|---|
( ~- ) | Element-wise negation. |
( + ) | Element-wise addition. |
( - ) | Element-wise substraction. |
( * ) | Element-wise multiplication. |
( / ) | Element-wise division. |
( % ) | Element-wise remainder of division. |
Abs | Element-wise absolute value. |
Acos | Element-wise arccosine (inverse cosine). |
Asin | Element-wise arcsine (inverse sine). |
Atan | Element-wise arctanget (inverse tangent). |
Ceiling | Element-wise ceiling (round towards positive infinity). |
Cos | Element-wise cosine. |
Cosh | Element-wise hyperbolic cosine. |
Exp | Element-wise exponential function. |
Floor | Element-wise floor (round towards negative infinity). |
Log | Element-wise natural logarithm. |
Log10 | Element-wise common logarithm. |
Pow | Element-wise exponentiation. |
Round | Element-wise rounding. |
Sgn | Element-wise sign. |
Sin | Element-wise sine. |
Sinh | Element-wise hyperbolic sine. |
Sqrt | Element-wise square root. |
Tan | Element-wise tangent. |
Tanh | Element-wise hyperbolic tangent. |
Truncate | Element-wise truncation (rounding towards zero). |
Tensor operations
These functions perform various operations on one or more tensors.
Function | Description |
---|---|
concat | Concatenates tensors along an axis. |
copy | Returns a copy of the tensor. |
diag | Returns a view of the diagonal of the matrix. |
diagAxis | Returns a view of the diagonal along the given axes. |
diagMat | Creates a matrix with the specified diagonal. |
diagMatAxis | Creates a tensor with the specified diagonal along the given axes. |
diff | Calculates the difference between adjoining elements of the vector. |
diffAxis | Calculates the difference between adjoining elements along the specified axes. |
ofBlocks | Builds a tensor out of tensor blocks. |
replicate | Repeats the tensor along an axis. |
T | Transpose of a matrix. |
Linear algebra functions
Use these functions to perform basic linear algebra operations on tensors.
Function | Description |
---|---|
( .* ) | Computes the (batched) matrix product, (batched) matrix-vector product or scalar product. |
norm | Calculates the norm of the (flattened) tensor. |
normAxis | Calculates the norm along the specified axis. |
invert | (Batch) inverts a matrix. |
pseudoInvert | Computes the (batched) Moore-Penrose pseudo-inverse of the specified matrix. |
SVD | Computes the (batched) singular value decomposition (SVD) of the specified matrix. |
symmetricEigenDecomposition | Computes the (real) eigendecomposition of a symmetric matrix. |
tensorProduct | Computes the tensor product between two tensors. |
Shape functions
Use these functions to work with the shape and memory layout of a tensor.
Function | Description |
---|---|
atLeastND | Pads the tensor from the left with size-one dimensions until it has at least the specified number of dimensions. |
broadcastDim | Broadcast a dimension to a specified size. |
broadcastTo | Broadcasts the specified tensor to the specified shape. |
broadcastToSame | Broadcasts all specified tensors to have the same shape. |
broadcastToSameInDims | Broadcasts all specified tensors to have the same size in the specified dimensions. |
cutLeft | Removes the first dimension. |
cutRight | Removes the last dimension. |
flatten | Flattens the tensor into a (one-dimensional) vector. |
insertAxis | Insert a dimension of size one before the specifed dimension. |
isBroadcasted | Checks if the specified tensor is broadcasted in at least one dimension. |
Layout | Memory layout of the tensor. |
NDims | Dimensionality of the tensor. |
NElems | Total number of elements within the tensor. |
padLeft | Insert a dimension of size one as the first dimension. |
padRight | Append a dimension of size one after the last dimension. |
padToSame | Pads all specified tensors from the left with dimensions of size one until they have the same dimensionality. |
permuteAxes | Permutes the axes as specified. |
reshape | Changes the shape of a tensor. |
reverseAxis | Reverses the elements in the specified dimension. |
relayout | Creates a tensor with the specified layout sharing its storage with the original tensor. |
Shape | Shape of the tensor. |
swapDim | Swaps the specified dimensions of the tensor. |
Data type functions
Use these functions to query or change the data type of the elements of a tensor.
Function | Description |
---|---|
convert | Convert the elements of a tensor to the specifed type. |
DataType | Type of data stored within the tensor. |
Device and storage functions
Use these functions to query or change the storage device of a tensor.
Function | Description |
---|---|
Dev | Device the data of tensor is stored on. |
Storage | The storage object that holds the data of this tensor. |
transfer | Transfers a tensor to the specifed device. |
Comparison functions
Use these functions to perform comparisons of tensors. The results are mostly boolean tensors.
Function | Description |
---|---|
( ==== ) | Element-wise equality test. |
( <<<< ) | Element-wise less-than test. |
( <<== ) | Element-wise less-than-or-equal test. |
( >>>> ) | Element-wise greater-than test. |
( >>== ) | Element-wise greater-than-or-equal test. |
( <<>> ) | Element-wise not-equality test. |
almostEqual | Checks if two tensors have the same (within machine precision) values in all elements. |
isClose | Element-wise check if two tensors have same (within machine precision) values. |
isFinite | Element-wise finity check (not -Inf, Inf or NaN). |
maxElemwise | Element-wise maximum. |
minElemwise | Element-wise minimum. |
allFinite | Checks that all elements of the tensor are finite. |
Logical functions
Use these functions to work with boolean tensors.
Function | Description |
---|---|
( ~~~~ ) | Element-wise logical negation. |
( &&&& ) | Element-wise loigcal and. |
( |||| ) | Element-wise loigcal or. |
( ^^^^ ) | Element-wise loigcal xor. |
all | Checks if all elements of the tensor are true. |
allAxis | Checks if all elements along the specified axis are true. |
allElems | Gets a sequence of all all elements within the tensor. |
allTensor | Checks if all elements of the tensor are true returning the result as a tensor. |
any | Checks if any elements of the tensor are true. |
anyAxis | Checks if any element along the specified axis is true. |
anyTensor | Checks if any element of the tensor is true returning the result as a tensor. |
countTrue | Counts the elements being true. |
countTrueAxis | Counts the elements being true along the specified axis. |
ifThenElse | Element-wise choice between two sources depending on a condition. |
Index functions
These functions return tensor of indices or work with them.
Function | Description |
---|---|
allIdx | Gets a sequence of all indices to enumerate all elements within the tensor. |
argMax | Finds the indicies of the maximum value of the tensor. |
argMaxAxis | Finds the index of the maximum value along the specified axis. |
argMin | Finds the indicies of the minimum value of the tensor. |
argMinAxis | Finds the index of the minimum value along the specified axis. |
find | Finds the first occurence of the specfied value and returns its indices. |
findAxis | Finds the first occurence of the specfied value along the specified axis and returns its index. |
gather | Selects elements from a tensor according to specified indices. |
scatter | Disperses elements from a source tensor to a new tensor according to the specified indices. |
trueIdx | Finds the indices of all element that are true. |
Reduction functions
These functions perform operations on tensors that reduce their dimensionality.
Function | Description |
---|---|
max | Calculates the maximum of all elements. |
maxAxis | Calculates the maximum value of the elements along the specified axis. |
min | Calculates the minimum of all elements. |
minAxis | Calculates the minimum value of the elements along the specified axis. |
mean | Calculates the mean of the tensor. |
meanAxis | Calculates the mean of the elements along the specified axis. |
product | Calculates the product of all elements. |
productAxis | Calculates the product of the elements along the specified axis. |
std | Calculates the standard deviation of the tensor. |
stdAxis | Calculates the standard deviation of the elements along the specified axis. |
sum | Sums all elements. |
sumAxis | Sums the elements along the specified axis. |
var | Calculates the variance of the tensor. |
varAxis | Calculates the variance of the elements along the specified axis. |
trace | Calculates the trace of the matrix. |
traceAxis | Calculates the trace along the specified axes. |
Functional operations (host only)
Use these functions to perform operations that are common in functional programming languages. They require the tensor to be stored in host memory.
Function | Description |
---|---|
HostTensor.foldAxis | Applies to specified function to all elements of the tensor, threading an accumulator through the computation. |
HostTensor.init | Creates a new tensor with values returned by the specified function. |
HostTensor.map | Applies to specified function to all elements of the tensor. |
HostTensor.map2 | Applies to specified function to all elements of the two tensors. |
HostTensor.mapi | Applies to specified indexed function to all elements of the tensor. |
HostTensor.mapi2 | Applies to specified indexed function to all elements of the two tensors. |
Data exchange (host only)
Use these functions to convert tensors to and from other storage modalities. They require the tensor to be stored in host memory.
Function | Description |
---|---|
HostTensor.ofArray | Creates a one-dimensional tensor copying the specified data. |
HostTensor.ofList | Creates a one-dimensional tensor from the specified list. |
HostTensor.ofSeq | Creates a one-dimensional tensor from the specified sequence. |
HostTensor.read | Reads a tensor from the specified HDF5 object path in an HDF5 file. |
HostTensor.readUntyped | Reads a tensor with unspecified data type from the specified HDF5 object path in an HDF5 file. |
HostTensor.toArray | Creates an array from a one-dimensional tensor. |
HostTensor.toList | Creates a list from a one-dimensional tensor. |
HostTensor.toSeq | A sequence of all elements contained in the tensor. |
HostTensor.usingArray | Creates a one-dimensional tensor referencing the specified data. |
HostTensor.write | Writes the tensor into the HDF5 file under the specfied HDF5 object path. |
Random number generation (host only)
Use these functions to generate tensors filled with random numbers.
Function | Description |
---|---|
HostTensor.randomInt | Creates a tensor filled with random integer numbers from a uniform distribution. |
HostTensor.randomNormal | Creates a tensor filled with random numbers from a normale distribution. |
HostTensor.randomUniform | Creates a tensor filled with random floating-point numbers from a uniform distribution. |