Deep Learning Algorithm Implementations 1.0.0
C++ implementations of fundamental deep learning algorithms
Loading...
Searching...
No Matches
utils::Tensor< T > Class Template Reference

#include <tensor.hpp>

Public Member Functions

xt::xarray< T > & data ()
 Get the underlying xtensor array.
 
const xt::xarray< T > & data () const
 Get the underlying xtensor array (const)
 
Constructors
 Tensor ()
 Default constructor creating an empty tensor.
 
 Tensor (const std::vector< size_t > &shape)
 Constructor creating a tensor with specified shape.
 
 Tensor (size_t rows, size_t cols)
 Constructor creating a 2D tensor (matrix) with specified dimensions.
 
 Tensor (const std::vector< size_t > &shape, T value)
 Constructor creating a tensor filled with a specific value.
 
 Tensor (size_t rows, size_t cols, T value)
 Constructor creating a 2D tensor (matrix) filled with a specific value.
 
 Tensor (std::initializer_list< std::initializer_list< T > > list)
 Constructor from initializer list (2D matrix)
 
 Tensor (const xt::xarray< T > &data)
 Constructor from xtensor array.
 
Element Access
template<typename... Args>
T & operator() (Args... indices)
 Access tensor element at specified position (mutable)
 
template<typename... Args>
const T & operator() (Args... indices) const
 Access tensor element at specified position (const)
 
T & at (size_t row, size_t col)
 Access 2D tensor element at specified position (mutable) - backward compatibility.
 
const T & at (size_t row, size_t col) const
 Access 2D tensor element at specified position (const) - backward compatibility.
 
Arithmetic Operations
Tensor operator+ (const Tensor &other) const
 Tensor element-wise addition operator.
 
Tensor operator- (const Tensor &other) const
 Tensor element-wise subtraction operator.
 
Tensor operator* (const Tensor &other) const
 Tensor element-wise multiplication operator.
 
Tensor matmul (const Tensor &other) const
 Matrix multiplication operator (for 2D tensors)
 
Tensor operator* (T scalar) const
 Scalar multiplication operator.
 
Tensor Operations
Tensor transpose () const
 Compute the transpose of the tensor (for 2D tensors)
 
Tensor transpose (const std::vector< size_t > &axes) const
 Transpose along specified axes.
 
Tensor reshape (const std::vector< size_t > &new_shape) const
 Reshape the tensor to new dimensions.
 
Tensor reshape (size_t new_rows, size_t new_cols) const
 Reshape the tensor to new 2D dimensions (backward compatibility)
 
Tensor view (const std::vector< size_t > &new_shape) const
 Create a view of the tensor with new shape.
 
Tensor squeeze (int axis=-1) const
 Squeeze dimensions of size 1.
 
Tensor unsqueeze (size_t axis) const
 Add a dimension of size 1.
 
determinant () const
 Calculate the determinant of the matrix (for 2D square tensors)
 
Tensor inverse () const
 Calculate the inverse of the matrix (for 2D square tensors)
 
auto eigenvalues () const
 Calculate eigenvalues of the matrix (for 2D square tensors)
 
Utility Methods
size_t rows () const
 Get the number of rows.
 
size_t cols () const
 Get the number of columns.
 
size_t size () const
 Get the total number of elements.
 
std::tuple< size_t, size_t > shape () const
 Get the shape of the matric in one step.
 

Static Public Member Functions

Static Factory Methods
static Tensor zeros (const std::vector< size_t > &shape)
 Create a zero tensor.
 
static Tensor zeros (size_t rows, size_t cols)
 Create a zero matrix (backward compatibility)
 
static Tensor ones (const std::vector< size_t > &shape)
 Create a tensor filled with ones.
 
static Tensor ones (size_t rows, size_t cols)
 Create a matrix filled with ones (backward compatibility)
 
static Tensor full (const std::vector< size_t > &shape, T value)
 Create a tensor filled with a specific value.
 
static Tensor identity (size_t size)
 Create an identity matrix (2D tensor)
 
static Tensor random (const std::vector< size_t > &shape)
 Create a random tensor with values between 0 and 1.
 
static Tensor random (const std::vector< size_t > &shape, T min, T max)
 Create a random tensor with values between min and max.
 
static Tensor random (size_t rows, size_t cols)
 Create a matrix with random values between 0 and 1 (backward compatibility)
 
static Tensor random (size_t rows, size_t cols, T min, T max)
 Create a matrix with random values (backward compatibility)
 
static Tensor from_array (const xt::xarray< T > &array)
 Create a tensor from an existing xt::xarray.
 

Friends

template<typename U >
std::ostream & operator<< (std::ostream &os, const Tensor< U > &tensor)
 
template<typename U >
Tensor< U > dot (const Tensor< U > &a, const Tensor< U > &b)
 
template<typename U >
sum (const Tensor< U > &tensor)
 
template<typename U >
mean (const Tensor< U > &tensor)
 

Detailed Description

Constructor & Destructor Documentation

◆ Tensor() [1/7]

template<typename T >
utils::Tensor< T >::Tensor ( )
inline

Default constructor creating an empty tensor.

Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 58 of file tensor.hpp.

◆ Tensor() [2/7]

template<typename T >
utils::Tensor< T >::Tensor ( const std::vector< size_t > &  shape)
explicit

Constructor creating a tensor with specified shape.

Parameters
shapeShape of the tensor

Definition at line 13 of file tensor.cpp.

◆ Tensor() [3/7]

template<typename T >
utils::Tensor< T >::Tensor ( size_t  rows,
size_t  cols 
)

Constructor creating a 2D tensor (matrix) with specified dimensions.

Parameters
rowsNumber of rows
colsNumber of columns

Definition at line 21 of file tensor.cpp.

◆ Tensor() [4/7]

template<typename T >
utils::Tensor< T >::Tensor ( const std::vector< size_t > &  shape,
value 
)

Constructor creating a tensor filled with a specific value.

Parameters
shapeShape of the tensor
valueValue to fill the tensor with

Definition at line 17 of file tensor.cpp.

◆ Tensor() [5/7]

template<typename T >
utils::Tensor< T >::Tensor ( size_t  rows,
size_t  cols,
value 
)

Constructor creating a 2D tensor (matrix) filled with a specific value.

Parameters
rowsNumber of rows
colsNumber of columns
valueValue to fill the tensor with

Definition at line 25 of file tensor.cpp.

◆ Tensor() [6/7]

template<typename T >
utils::Tensor< T >::Tensor ( std::initializer_list< std::initializer_list< T > >  list)

Constructor from initializer list (2D matrix)

Parameters
listNested initializer list representing matrix data

Definition at line 37 of file tensor.cpp.

◆ Tensor() [7/7]

template<typename T >
utils::Tensor< T >::Tensor ( const xt::xarray< T > &  data)
explicit

Constructor from xtensor array.

Parameters
dataxtensor array data

Definition at line 29 of file tensor.cpp.

Member Function Documentation

◆ at() [1/2]

template<typename T >
T & utils::Tensor< T >::at ( size_t  row,
size_t  col 
)

Access 2D tensor element at specified position (mutable) - backward compatibility.

Parameters
rowRow index (0-based)
colColumn index (0-based)
Returns
Reference to the element
Exceptions
std::out_of_rangeif indices are invalid
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 55 of file tensor.cpp.

◆ at() [2/2]

template<typename T >
const T & utils::Tensor< T >::at ( size_t  row,
size_t  col 
) const

Access 2D tensor element at specified position (const) - backward compatibility.

Parameters
rowRow index (0-based)
colColumn index (0-based)
Returns
Const reference to the element
Exceptions
std::out_of_rangeif indices are invalid

Definition at line 60 of file tensor.cpp.

◆ cols()

template<typename T >
size_t utils::Tensor< T >::cols ( ) const
inline

Get the number of columns.

Returns
Number of columns
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 296 of file tensor.hpp.

◆ data() [1/2]

template<typename T >
xt::xarray< T > & utils::Tensor< T >::data ( )
inline

Get the underlying xtensor array.

Returns
Reference to the xtensor array
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 409 of file tensor.hpp.

◆ data() [2/2]

template<typename T >
const xt::xarray< T > & utils::Tensor< T >::data ( ) const
inline

Get the underlying xtensor array (const)

Returns
Const reference to the xtensor array

Definition at line 415 of file tensor.hpp.

◆ determinant()

template<typename T >
T utils::Tensor< T >::determinant ( ) const

Calculate the determinant of the matrix (for 2D square tensors)

Returns
Determinant value
Exceptions
std::invalid_argumentif tensor is not 2D square
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 233 of file tensor.cpp.

◆ eigenvalues()

template<typename T >
auto utils::Tensor< T >::eigenvalues ( ) const

Calculate eigenvalues of the matrix (for 2D square tensors)

Returns
Eigenvalues
Exceptions
std::invalid_argumentif tensor is not 2D square
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 256 of file tensor.cpp.

◆ from_array()

template<typename T >
Tensor< T > utils::Tensor< T >::from_array ( const xt::xarray< T > &  array)
static

Create a tensor from an existing xt::xarray.

Parameters
arrayThe xt::xarray to wrap
Returns
Tensor wrapping the array
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 348 of file tensor.cpp.

◆ full()

template<typename T >
Tensor< T > utils::Tensor< T >::full ( const std::vector< size_t > &  shape,
value 
)
static

Create a tensor filled with a specific value.

Parameters
shapeShape of the tensor
valueValue to fill the tensor with
Returns
Tensor filled with the specified value
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 289 of file tensor.cpp.

◆ identity()

template<typename T >
Tensor< T > utils::Tensor< T >::identity ( size_t  size)
static

Create an identity matrix (2D tensor)

Parameters
sizeSize of the square identity matrix
Returns
Identity tensor
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 294 of file tensor.cpp.

◆ inverse()

template<typename T >
Tensor< T > utils::Tensor< T >::inverse ( ) const

Calculate the inverse of the matrix (for 2D square tensors)

Returns
Inverse tensor
Exceptions
std::invalid_argumentif tensor is not 2D square or singular
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 243 of file tensor.cpp.

◆ matmul()

template<typename T >
Tensor< T > utils::Tensor< T >::matmul ( const Tensor< T > &  other) const

Matrix multiplication operator (for 2D tensors)

Parameters
otherTensor to multiply with
Returns
Result of matrix multiplication
Exceptions
std::invalid_argumentif tensor dimensions are incompatible for matrix multiplication
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 111 of file tensor.cpp.

◆ ones() [1/2]

template<typename T >
Tensor< T > utils::Tensor< T >::ones ( const std::vector< size_t > &  shape)
static

Create a tensor filled with ones.

Parameters
shapeShape of the tensor
Returns
Tensor filled with ones
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 277 of file tensor.cpp.

◆ ones() [2/2]

template<typename T >
Tensor< T > utils::Tensor< T >::ones ( size_t  rows,
size_t  cols 
)
static

Create a matrix filled with ones (backward compatibility)

Parameters
rowsNumber of rows
colsNumber of columns
Returns
Tensor filled with ones with 2D shape

Definition at line 284 of file tensor.cpp.

◆ operator()() [1/2]

template<typename T >
template<typename... Args>
T & utils::Tensor< T >::operator() ( Args...  indices)

Access tensor element at specified position (mutable)

Parameters
indicesVariable number of indices for n-dimensional access
Returns
Reference to the element
Exceptions
std::out_of_rangeif indices are invalid
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 432 of file tensor.hpp.

◆ operator()() [2/2]

template<typename T >
template<typename... Args>
const T & utils::Tensor< T >::operator() ( Args...  indices) const

Access tensor element at specified position (const)

Parameters
indicesVariable number of indices for n-dimensional access
Returns
Const reference to the element
Exceptions
std::out_of_rangeif indices are invalid

Definition at line 438 of file tensor.hpp.

◆ operator*() [1/2]

template<typename T >
Tensor< T > utils::Tensor< T >::operator* ( const Tensor< T > &  other) const

Tensor element-wise multiplication operator.

Parameters
otherTensor to multiply with
Returns
Result of element-wise tensor multiplication
Exceptions
std::invalid_argumentif tensor shapes don't match
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 93 of file tensor.cpp.

◆ operator*() [2/2]

template<typename T >
Tensor< T > utils::Tensor< T >::operator* ( scalar) const

Scalar multiplication operator.

Parameters
scalarScalar value to multiply with
Returns
Result of scalar multiplication

Definition at line 104 of file tensor.cpp.

◆ operator+()

template<typename T >
Tensor< T > utils::Tensor< T >::operator+ ( const Tensor< T > &  other) const

Tensor element-wise addition operator.

Parameters
otherTensor to add
Returns
Result of tensor addition
Exceptions
std::invalid_argumentif tensor shapes don't match
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 71 of file tensor.cpp.

◆ operator-()

template<typename T >
Tensor< T > utils::Tensor< T >::operator- ( const Tensor< T > &  other) const

Tensor element-wise subtraction operator.

Parameters
otherTensor to subtract
Returns
Result of tensor subtraction
Exceptions
std::invalid_argumentif tensor shapes don't match
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 82 of file tensor.cpp.

◆ random() [1/4]

template<typename T >
Tensor< T > utils::Tensor< T >::random ( const std::vector< size_t > &  shape)
static

Create a random tensor with values between 0 and 1.

Parameters
shapeShape of the tensor
Returns
Random tensor
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 302 of file tensor.cpp.

◆ random() [2/4]

template<typename T >
Tensor< T > utils::Tensor< T >::random ( const std::vector< size_t > &  shape,
min,
max 
)
static

Create a random tensor with values between min and max.

Parameters
shapeShape of the tensor
minMinimum random value
maxMaximum random value
Returns
Random tensor

Definition at line 313 of file tensor.cpp.

◆ random() [3/4]

template<typename T >
Tensor< T > utils::Tensor< T >::random ( size_t  rows,
size_t  cols 
)
static

Create a matrix with random values between 0 and 1 (backward compatibility)

Parameters
rowsNumber of rows
colsNumber of columns
Returns
Random tensor with 2D shape

Definition at line 324 of file tensor.cpp.

◆ random() [4/4]

template<typename T >
Tensor< T > utils::Tensor< T >::random ( size_t  rows,
size_t  cols,
min,
max 
)
static

Create a matrix with random values (backward compatibility)

Parameters
rowsNumber of rows
colsNumber of columns
minMinimum random value
maxMaximum random value
Returns
Random tensor with 2D shape

Definition at line 336 of file tensor.cpp.

◆ reshape() [1/2]

template<typename T >
Tensor< T > utils::Tensor< T >::reshape ( const std::vector< size_t > &  new_shape) const

Reshape the tensor to new dimensions.

Parameters
new_shapeNew shape for the tensor
Returns
Reshaped tensor
Exceptions
std::invalid_argumentif total size doesn't match
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 156 of file tensor.cpp.

◆ reshape() [2/2]

template<typename T >
Tensor< T > utils::Tensor< T >::reshape ( size_t  new_rows,
size_t  new_cols 
) const

Reshape the tensor to new 2D dimensions (backward compatibility)

Parameters
new_rowsNew number of rows
new_colsNew number of columns
Returns
Reshaped tensor
Exceptions
std::invalid_argumentif total size doesn't match

Definition at line 170 of file tensor.cpp.

◆ rows()

template<typename T >
size_t utils::Tensor< T >::rows ( ) const
inline

Get the number of rows.

Returns
Number of rows
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 290 of file tensor.hpp.

◆ shape()

template<typename T >
std::tuple< size_t, size_t > utils::Tensor< T >::shape ( ) const
inline

Get the shape of the matric in one step.

Returns
Shape (rows, cols) in tuple
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 308 of file tensor.hpp.

◆ size()

template<typename T >
size_t utils::Tensor< T >::size ( ) const
inline

Get the total number of elements.

Returns
Total size (rows * cols)
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 302 of file tensor.hpp.

◆ squeeze()

template<typename T >
Tensor< T > utils::Tensor< T >::squeeze ( int  axis = -1) const

Squeeze dimensions of size 1.

Parameters
axisOptional axis to squeeze (if -1, squeeze all dimensions of size 1)
Returns
Squeezed tensor
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 189 of file tensor.cpp.

◆ transpose() [1/2]

template<typename T >
Tensor< T > utils::Tensor< T >::transpose ( ) const

Compute the transpose of the tensor (for 2D tensors)

Returns
Transposed tensor
Exceptions
std::invalid_argumentif tensor is not 2D
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 128 of file tensor.cpp.

◆ transpose() [2/2]

template<typename T >
Tensor< T > utils::Tensor< T >::transpose ( const std::vector< size_t > &  axes) const

Transpose along specified axes.

Parameters
axesAxes to transpose
Returns
Transposed tensor

Definition at line 139 of file tensor.cpp.

◆ unsqueeze()

template<typename T >
Tensor< T > utils::Tensor< T >::unsqueeze ( size_t  axis) const

Add a dimension of size 1.

Parameters
axisAxis where to add the dimension
Returns
Tensor with added dimension
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 220 of file tensor.cpp.

◆ view()

template<typename T >
Tensor< T > utils::Tensor< T >::view ( const std::vector< size_t > &  new_shape) const

Create a view of the tensor with new shape.

Parameters
new_shapeNew shape for the view
Returns
Tensor view with new shape
Exceptions
std::invalid_argumentif total size doesn't match
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 175 of file tensor.cpp.

◆ zeros() [1/2]

template<typename T >
Tensor< T > utils::Tensor< T >::zeros ( const std::vector< size_t > &  shape)
static

Create a zero tensor.

Parameters
shapeShape of the tensor
Returns
Zero tensor
Examples
/home/runner/work/deep-learning-algo-impls/deep-learning-algo-impls/include/utils/tensor.hpp.

Definition at line 267 of file tensor.cpp.

◆ zeros() [2/2]

template<typename T >
Tensor< T > utils::Tensor< T >::zeros ( size_t  rows,
size_t  cols 
)
static

Create a zero matrix (backward compatibility)

Parameters
rowsNumber of rows
colsNumber of columns
Returns
Zero tensor with 2D shape

Definition at line 272 of file tensor.cpp.

Friends And Related Symbol Documentation

◆ dot

template<typename T >
template<typename U >
Tensor< U > dot ( const Tensor< U > &  a,
const Tensor< U > &  b 
)
friend

◆ mean

template<typename T >
template<typename U >
U mean ( const Tensor< U > &  tensor)
friend

◆ operator<<

template<typename T >
template<typename U >
std::ostream & operator<< ( std::ostream &  os,
const Tensor< U > &  tensor 
)
friend

◆ sum

template<typename T >
template<typename U >
U sum ( const Tensor< U > &  tensor)
friend

The documentation for this class was generated from the following files: