Deep Learning Algorithm Implementations 1.0.0
C++ implementations of fundamental deep learning algorithms
|
#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. | |
T | 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 > | |
U | sum (const Tensor< U > &tensor) |
template<typename U > | |
U | mean (const Tensor< U > &tensor) |
Definition at line 48 of file tensor.hpp.
|
inline |
Default constructor creating an empty tensor.
Definition at line 58 of file tensor.hpp.
|
explicit |
Constructor creating a tensor with specified shape.
shape | Shape of the tensor |
Definition at line 13 of file tensor.cpp.
utils::Tensor< T >::Tensor | ( | size_t | rows, |
size_t | cols | ||
) |
Constructor creating a 2D tensor (matrix) with specified dimensions.
rows | Number of rows |
cols | Number of columns |
Definition at line 21 of file tensor.cpp.
utils::Tensor< T >::Tensor | ( | const std::vector< size_t > & | shape, |
T | value | ||
) |
Constructor creating a tensor filled with a specific value.
shape | Shape of the tensor |
value | Value to fill the tensor with |
Definition at line 17 of file tensor.cpp.
utils::Tensor< T >::Tensor | ( | size_t | rows, |
size_t | cols, | ||
T | value | ||
) |
Constructor creating a 2D tensor (matrix) filled with a specific value.
rows | Number of rows |
cols | Number of columns |
value | Value to fill the tensor with |
Definition at line 25 of file tensor.cpp.
utils::Tensor< T >::Tensor | ( | std::initializer_list< std::initializer_list< T > > | list | ) |
Constructor from initializer list (2D matrix)
list | Nested initializer list representing matrix data |
Definition at line 37 of file tensor.cpp.
|
explicit |
Constructor from xtensor array.
data | xtensor array data |
Definition at line 29 of file tensor.cpp.
T & utils::Tensor< T >::at | ( | size_t | row, |
size_t | col | ||
) |
Access 2D tensor element at specified position (mutable) - backward compatibility.
row | Row index (0-based) |
col | Column index (0-based) |
std::out_of_range | if indices are invalid |
Definition at line 55 of file tensor.cpp.
const T & utils::Tensor< T >::at | ( | size_t | row, |
size_t | col | ||
) | const |
Access 2D tensor element at specified position (const) - backward compatibility.
row | Row index (0-based) |
col | Column index (0-based) |
std::out_of_range | if indices are invalid |
Definition at line 60 of file tensor.cpp.
|
inline |
Get the number of columns.
Definition at line 296 of file tensor.hpp.
|
inline |
Get the underlying xtensor array.
Definition at line 409 of file tensor.hpp.
|
inline |
Get the underlying xtensor array (const)
Definition at line 415 of file tensor.hpp.
T utils::Tensor< T >::determinant | ( | ) | const |
Calculate the determinant of the matrix (for 2D square tensors)
std::invalid_argument | if tensor is not 2D square |
Definition at line 233 of file tensor.cpp.
auto utils::Tensor< T >::eigenvalues | ( | ) | const |
Calculate eigenvalues of the matrix (for 2D square tensors)
std::invalid_argument | if tensor is not 2D square |
Definition at line 256 of file tensor.cpp.
|
static |
Create a tensor from an existing xt::xarray.
array | The xt::xarray to wrap |
Definition at line 348 of file tensor.cpp.
|
static |
Create a tensor filled with a specific value.
shape | Shape of the tensor |
value | Value to fill the tensor with |
Definition at line 289 of file tensor.cpp.
|
static |
Create an identity matrix (2D tensor)
size | Size of the square identity matrix |
Definition at line 294 of file tensor.cpp.
Tensor< T > utils::Tensor< T >::inverse | ( | ) | const |
Calculate the inverse of the matrix (for 2D square tensors)
std::invalid_argument | if tensor is not 2D square or singular |
Definition at line 243 of file tensor.cpp.
Tensor< T > utils::Tensor< T >::matmul | ( | const Tensor< T > & | other | ) | const |
Matrix multiplication operator (for 2D tensors)
other | Tensor to multiply with |
std::invalid_argument | if tensor dimensions are incompatible for matrix multiplication |
Definition at line 111 of file tensor.cpp.
|
static |
Create a tensor filled with ones.
shape | Shape of the tensor |
Definition at line 277 of file tensor.cpp.
|
static |
Create a matrix filled with ones (backward compatibility)
rows | Number of rows |
cols | Number of columns |
Definition at line 284 of file tensor.cpp.
T & utils::Tensor< T >::operator() | ( | Args... | indices | ) |
Access tensor element at specified position (mutable)
indices | Variable number of indices for n-dimensional access |
std::out_of_range | if indices are invalid |
Definition at line 432 of file tensor.hpp.
const T & utils::Tensor< T >::operator() | ( | Args... | indices | ) | const |
Access tensor element at specified position (const)
indices | Variable number of indices for n-dimensional access |
std::out_of_range | if indices are invalid |
Definition at line 438 of file tensor.hpp.
Tensor< T > utils::Tensor< T >::operator* | ( | const Tensor< T > & | other | ) | const |
Tensor element-wise multiplication operator.
other | Tensor to multiply with |
std::invalid_argument | if tensor shapes don't match |
Definition at line 93 of file tensor.cpp.
Tensor< T > utils::Tensor< T >::operator* | ( | T | scalar | ) | const |
Scalar multiplication operator.
scalar | Scalar value to multiply with |
Definition at line 104 of file tensor.cpp.
Tensor< T > utils::Tensor< T >::operator+ | ( | const Tensor< T > & | other | ) | const |
Tensor element-wise addition operator.
other | Tensor to add |
std::invalid_argument | if tensor shapes don't match |
Definition at line 71 of file tensor.cpp.
Tensor< T > utils::Tensor< T >::operator- | ( | const Tensor< T > & | other | ) | const |
Tensor element-wise subtraction operator.
other | Tensor to subtract |
std::invalid_argument | if tensor shapes don't match |
Definition at line 82 of file tensor.cpp.
|
static |
Create a random tensor with values between 0 and 1.
shape | Shape of the tensor |
Definition at line 302 of file tensor.cpp.
|
static |
Create a random tensor with values between min and max.
shape | Shape of the tensor |
min | Minimum random value |
max | Maximum random value |
Definition at line 313 of file tensor.cpp.
|
static |
Create a matrix with random values between 0 and 1 (backward compatibility)
rows | Number of rows |
cols | Number of columns |
Definition at line 324 of file tensor.cpp.
|
static |
Create a matrix with random values (backward compatibility)
rows | Number of rows |
cols | Number of columns |
min | Minimum random value |
max | Maximum random value |
Definition at line 336 of file tensor.cpp.
Tensor< T > utils::Tensor< T >::reshape | ( | const std::vector< size_t > & | new_shape | ) | const |
Reshape the tensor to new dimensions.
new_shape | New shape for the tensor |
std::invalid_argument | if total size doesn't match |
Definition at line 156 of file tensor.cpp.
Tensor< T > utils::Tensor< T >::reshape | ( | size_t | new_rows, |
size_t | new_cols | ||
) | const |
Reshape the tensor to new 2D dimensions (backward compatibility)
new_rows | New number of rows |
new_cols | New number of columns |
std::invalid_argument | if total size doesn't match |
Definition at line 170 of file tensor.cpp.
|
inline |
Get the number of rows.
Definition at line 290 of file tensor.hpp.
|
inline |
Get the shape of the matric in one step.
Definition at line 308 of file tensor.hpp.
|
inline |
Get the total number of elements.
Definition at line 302 of file tensor.hpp.
Tensor< T > utils::Tensor< T >::squeeze | ( | int | axis = -1 | ) | const |
Squeeze dimensions of size 1.
axis | Optional axis to squeeze (if -1, squeeze all dimensions of size 1) |
Definition at line 189 of file tensor.cpp.
Tensor< T > utils::Tensor< T >::transpose | ( | ) | const |
Compute the transpose of the tensor (for 2D tensors)
std::invalid_argument | if tensor is not 2D |
Definition at line 128 of file tensor.cpp.
Tensor< T > utils::Tensor< T >::transpose | ( | const std::vector< size_t > & | axes | ) | const |
Transpose along specified axes.
axes | Axes to transpose |
Definition at line 139 of file tensor.cpp.
Tensor< T > utils::Tensor< T >::unsqueeze | ( | size_t | axis | ) | const |
Add a dimension of size 1.
axis | Axis where to add the dimension |
Definition at line 220 of file tensor.cpp.
Tensor< T > utils::Tensor< T >::view | ( | const std::vector< size_t > & | new_shape | ) | const |
Create a view of the tensor with new shape.
new_shape | New shape for the view |
std::invalid_argument | if total size doesn't match |
Definition at line 175 of file tensor.cpp.
|
static |
Create a zero tensor.
shape | Shape of the tensor |
Definition at line 267 of file tensor.cpp.
|
static |
Create a zero matrix (backward compatibility)
rows | Number of rows |
cols | Number of columns |
Definition at line 272 of file tensor.cpp.
|
friend |