MachineIntelligenceCore:Algorithms
|
Template class representing an nD (n-Dimensional) tensor. Tensor is row-major, i.e. first dimension is height (rows), second is width (cols), third is depth (channels) etc. More...
#include <Matrix.hpp>
Public Member Functions | |
Tensor () | |
Tensor (std::initializer_list< size_t > dims_) | |
Tensor (std::vector< size_t > dims_) | |
Tensor (const Tensor< T > &t) | |
Tensor (const mic::types::Matrix< T > &mat_) | |
const Tensor< T > & | operator= (const Tensor< T > &t) |
~Tensor () | |
void | flatten () |
void | conservativeResize (std::vector< size_t > dims_) |
void | resize (std::vector< size_t > dims_) |
void | elementwiseFunction (T(*func)(T)) |
void | elementwiseFunctionScalar (T(*func)(T, T), T scalar) |
void | normRandReal (float mean=0, float stddev=1) |
T * | data () |
std::vector< size_t > | dims () |
size_t | dim (size_t k) |
size_t | size () |
void | zeros () |
void | ones () |
void | enumerate () |
void | setValue (T value_) |
void | randn (T mean=0, T stddev=1) |
void | rand (T min=0, T max=1) |
mic::types::Tensor< T > | operator+ (mic::types::Tensor< T > obj_) |
mic::types::Tensor< T > | operator- (mic::types::Tensor< T > obj_) |
T | sum () |
T & | operator() (size_t index_) |
T & | operator() (std::vector< size_t > coordinates_) |
const T & | operator() (size_t index_) const |
const T & | operator() (std::vector< size_t > coordinates_) const |
size_t | getIndex (std::vector< size_t > coordinates_) |
Tensor< T > | block (std::vector< std::vector< size_t > > ranges_) |
void | concatenate (const Tensor &obj_) |
void | concatenate (std::vector< mic::types::Tensor< T > > tensors_) |
Private Member Functions | |
size_t | recursiveIndex (size_t dim_, std::vector< size_t > coordinates_) |
void | recursiveBlockCopy (size_t dim_, std::vector< std::vector< size_t > > ranges_, std::vector< size_t > is_, std::vector< size_t > js_, std::vector< size_t > new_dims_, T *tgt_data_ptr_) |
size_t | recursiveCalculateTargetIndex (size_t dim_, std::vector< size_t > js_, std::vector< size_t > dims_) |
size_t | recursiveCalculateSourceIndex (size_t dim_, std::vector< size_t > is_, size_t offset_) |
template<class Archive > | |
void | save (Archive &ar, const unsigned int version) const |
template<class Archive > | |
void | load (Archive &ar, const unsigned int version) |
Private Attributes | |
size_t | elements |
std::vector< size_t > | dimensions |
T * | data_ptr |
Friends | |
class | boost::serialization::access |
std::ostream & | operator<< (std::ostream &os_, const Tensor &obj_) |
Template class representing an nD (n-Dimensional) tensor. Tensor is row-major, i.e. first dimension is height (rows), second is width (cols), third is depth (channels) etc.
T | template parameter denoting data type stored in tensor. |
Definition at line 49 of file Matrix.hpp.
|
inline |
Default constructor (empty).
Definition at line 69 of file Tensor.hpp.
|
inline |
Constructor - sets the tensor dimension and assigns memory.
dims_ | Tensor dimensions - initilizer list ({ }). |
Definition at line 77 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, mic::types::Tensor< T >::dimensions, mic::types::Tensor< T >::elements, and mic::types::Tensor< T >::zeros().
|
inline |
Constructor - sets the tensor dimension and assigns memory.
dims_ | Tensor dimensions ({ }, vector<size_t> etc.). |
Definition at line 101 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, mic::types::Tensor< T >::dimensions, mic::types::Tensor< T >::elements, and mic::types::Tensor< T >::zeros().
|
inline |
Copying constructor - copies the values of the given tensor, including tensor dimensions and data.
t | The original tensor to be copied. |
Definition at line 125 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, mic::types::Tensor< T >::dimensions, and mic::types::Tensor< T >::elements.
|
inline |
Copying constructor - copies the values of the given 2D matrix.
t | The original matrix to be copied. |
Definition at line 142 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, mic::types::Tensor< T >::dimensions, and mic::types::Tensor< T >::elements.
|
inline |
Destructor. Frees memory (if it was assigned).
Definition at line 182 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr.
|
inline |
Returns the (sub)tensor, a new tensor with data being a subtraction from the original tensor taking into account the ranges given as pairs (lower, higher). For dimensions higher than 3 it uses recursive block copy.
lower_ | Ranges given as pairs (lower, higher) e <0,size-1> for each dimension. If given range consists of a single value, then it is treated as (lower=higher). |
Definition at line 550 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, mic::types::Tensor< T >::dimensions, mic::types::Tensor< T >::recursiveBlockCopy(), and mic::types::Tensor< T >::size().
Referenced by main().
|
inline |
Concatenates two tensors - attaches the tensor passed as argument "to the back". Note: both tensors must have exactly the same dimensions except the 0th dimension.
obj_ | Tensor to be attached. |
Definition at line 617 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, mic::types::Tensor< T >::dimensions, and mic::types::Tensor< T >::elements.
Referenced by main().
|
inline |
Concatenates a set of tensors - attaches the tensors passed as argument "to the back" of a given tensor. Note: both tensors must have exactly the same dimensions except the 0th dimension.
objs_ | A list of tensors to be attached. |
Definition at line 646 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, mic::types::Tensor< T >::dimensions, and mic::types::Tensor< T >::elements.
|
inline |
Resizes the tensor - sets new tensor dimensions. Important note: the total number of elements must remain unchanged.
dims_ | New dimensions. |
Definition at line 200 of file Tensor.hpp.
References mic::types::Tensor< T >::dimensions, and mic::types::Tensor< T >::elements.
|
inline |
Returns pointer to data.
Definition at line 301 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr.
Referenced by mic::types::Matrix< T >::Matrix().
|
inline |
Returns k-th dimension.
Definition at line 315 of file Tensor.hpp.
References mic::types::Tensor< T >::dimensions.
|
inline |
Returns dimensions.
Definition at line 308 of file Tensor.hpp.
References mic::types::Tensor< T >::dimensions.
Referenced by mic::types::Tensor< T >::getIndex(), mic::types::Matrix< T >::Matrix(), mic::types::Tensor< T >::operator+(), and mic::types::Tensor< T >::operator-().
|
inline |
Applies the function to all tensor elements.
func | The function to be applied. This must be a function with a single argument. |
Definition at line 261 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::elements.
|
inline |
Applies the function to all tensor elements. Additionally passes scalar to the function as second argument.
func | The function to be applied. This must be a function with exactly two arguments. |
scalar | Scalar passed as second function argument. |
Definition at line 273 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::elements.
|
inline |
Enumerates - sets values of elements to their indices.
Definition at line 346 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::elements.
|
inline |
Flattens the tensor - sets dimensions to [ n ].
Definition at line 190 of file Tensor.hpp.
References mic::types::Tensor< T >::dimensions, and mic::types::Tensor< T >::elements.
Referenced by main().
|
inline |
Returns the index of an element in nD matrix. For dimensions higher than 3 it uses recursive function.
coordinates_ | nD vector of element coordinates ({ }, vector<size_t> etc.). |
Definition at line 520 of file Tensor.hpp.
References mic::types::Tensor< T >::dimensions, mic::types::Tensor< T >::dims(), mic::types::Tensor< T >::recursiveIndex(), and mic::types::Tensor< T >::size().
Referenced by mic::types::Tensor< T >::operator()().
|
inlineprivate |
Serialization load - loads the tensor object to archive.
ar | Used archive. |
version | Version of the tensor class (not used currently). |
Definition at line 828 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, mic::types::Tensor< T >::dimensions, and mic::types::Tensor< T >::elements.
|
inline |
Set values of all matrix elements to random with a normal distribution.
mean | Mean |
stddev | Variance |
Definition at line 286 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::elements.
|
inline |
Sets all element values to one.
Definition at line 337 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::elements.
|
inline |
Operator used for setting the value of a given element of nD tensor.
index_ | Index (a single value). |
Definition at line 483 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr.
|
inline |
Operator used for setting the value of a given element of nD tensor.
coordinates_ | nD vector of element coordinates ({ }, vector<size_t> etc.). |
Definition at line 492 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::getIndex().
|
inline |
Operator returning the value of a given element of nD tensor.
index_ | Index (a single value). |
Definition at line 501 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr.
|
inline |
Operator returning the value of a given element of nD tensor.
coordinates_ | nD vector of element coordinates ({ }, vector<size_t> etc.). |
Definition at line 510 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::getIndex().
|
inline |
Operator returning new tensor being the sum of two tensors.
obj_ | The second tensor (on the right side of the +). |
Definition at line 406 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, mic::types::Tensor< T >::dimensions, mic::types::Tensor< T >::dims(), mic::types::Tensor< T >::elements, and mic::types::Tensor< T >::size().
|
inline |
Operator returning new tensor being the difference of two tensors.
obj_ | The second tensor (on the right side of the -). |
Definition at line 428 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, mic::types::Tensor< T >::dimensions, mic::types::Tensor< T >::dims(), mic::types::Tensor< T >::elements, and mic::types::Tensor< T >::size().
|
inline |
Assign operator - copies the values of the given tensor, including tensor dimensions and data.
t | The original tensor to be copied. |
Definition at line 158 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, mic::types::Tensor< T >::dimensions, and mic::types::Tensor< T >::elements.
|
inline |
Set values of all matrix elements to random numbers from range <min, max> - uniform distribution.
min | Min value. |
max | Max value. |
Definition at line 387 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::elements.
|
inline |
Set values of all matrix elements to random with a normal distribution.
mean | Mean |
stddev | Variance |
Definition at line 368 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::elements.
Referenced by TEST().
|
inlineprivate |
A recursive function copying the blocks of data from source (current) tensor to the new one. 0th dimension (of the new tensor) is treated as the size of a block. The function calls itself (recursively) in order to generate a vector of indices used subsequently for calculation of positions in both source and target data tables. It uses two helper functions (also recursive) for computation of those positions. WARNING: Tensor dimensions must be > 1!
dim_ | The currently analyzed dimension of tensor. |
ranges_ | nD vector of pairs of ranges. |
is_ | nD vector of indices in the source tensor. |
js_ | nD vector of indices in the new/target tensor. |
new_dims_ | nD vector of dimensions of the new tensor. |
tgt_data_ptr_ | Pointer to the memory block of the new tensor. |
Definition at line 726 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, mic::types::Tensor< T >::recursiveCalculateSourceIndex(), and mic::types::Tensor< T >::recursiveCalculateTargetIndex().
Referenced by mic::types::Tensor< T >::block().
|
inlineprivate |
A recursive function responsible for the calculation of position (index) in source (i.e. this) data table.
dim_ | The currently analyzed dimension of tensor. |
is_ | nD vector of indices in the source tensor. |
offset_ | Offset added aat the end(always const = range[0][0]). |
Definition at line 788 of file Tensor.hpp.
References mic::types::Tensor< T >::dimensions.
Referenced by mic::types::Tensor< T >::recursiveBlockCopy().
|
inlineprivate |
A recursive function responsible for the calculation of position (index) in target (i.e. new) data table.
dim_ | The currently analyzed dimension of tensor. |
js_ | nD vector of indices in the new/target tensor. |
dims_ | nD vector of dimensions of the (new) tensor. |
Definition at line 761 of file Tensor.hpp.
Referenced by mic::types::Tensor< T >::recursiveBlockCopy().
|
inlineprivate |
Recursive method computing the index of element in nD matrix.
dim_ | Dimension considered at the moment. |
coordinates_ | nD vector of coordinates. |
Definition at line 705 of file Tensor.hpp.
References mic::types::Tensor< T >::dimensions.
Referenced by mic::types::Tensor< T >::getIndex().
|
inline |
Resizes the tensor - sets new tensor dimensions. Analogically to Eigen, the resize() method is a no-operation if the actual matrix size doesn't change. Otherwise, i.e. if the total number of elements must change, a new memory block will be allocated and the method will copy as much elements from the old memory block as possible. If you want a conservative variant of resize() which does not change the coefficients, use conservativeResize(), If the new block will be bigger than the old one the values of "new elements" will be set to zeros.
dims_ | New dimensions. |
Definition at line 224 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, mic::types::Tensor< T >::dimensions, mic::types::Tensor< T >::elements, and mic::types::Tensor< T >::zeros().
|
inlineprivate |
Serialization save - saves the tensor object to archive.
ar | Used archive. |
version | Version of the tensor class (not used currently). |
Definition at line 816 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, mic::types::Tensor< T >::dimensions, and mic::types::Tensor< T >::elements.
|
inline |
Sets values of all element to the value given as parameter.
value_ | The value to be set. |
Definition at line 356 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::elements.
|
inline |
Returns size - number of elements.
Definition at line 323 of file Tensor.hpp.
References mic::types::Tensor< T >::elements.
Referenced by mic::types::Tensor< T >::block(), mic::types::Tensor< T >::getIndex(), mic::types::Matrix< T >::Matrix(), mic::types::Tensor< T >::operator+(), and mic::types::Tensor< T >::operator-().
|
inline |
Functions sums the tensor elements.
Definition at line 449 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::elements.
|
inline |
Sets all element values to zero.
Definition at line 330 of file Tensor.hpp.
References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::elements.
Referenced by mic::types::Tensor< T >::resize(), and mic::types::Tensor< T >::Tensor().
|
friend |
Definition at line 808 of file Tensor.hpp.
|
friend |
Stream operator enabling to print tensor dimensions and all values.
os_ | Ostream object. |
obj_ | Tensor object. |
Definition at line 463 of file Tensor.hpp.
|
private |
Table of elements of data types.
Definition at line 697 of file Tensor.hpp.
Referenced by mic::types::Tensor< T >::block(), mic::types::Tensor< T >::concatenate(), mic::types::Tensor< T >::data(), mic::types::Tensor< T >::elementwiseFunction(), mic::types::Tensor< T >::elementwiseFunctionScalar(), mic::types::Tensor< T >::enumerate(), mic::types::Tensor< T >::load(), mic::types::Tensor< T >::normRandReal(), mic::types::Tensor< T >::ones(), mic::types::Tensor< T >::operator()(), mic::types::Tensor< T >::operator+(), mic::types::Tensor< T >::operator-(), mic::types::Tensor< T >::operator=(), mic::types::Tensor< T >::rand(), mic::types::Tensor< T >::randn(), mic::types::Tensor< T >::recursiveBlockCopy(), mic::types::Tensor< T >::resize(), mic::types::Tensor< T >::save(), mic::types::Tensor< T >::setValue(), mic::types::Tensor< T >::sum(), mic::types::Tensor< T >::Tensor(), mic::types::Tensor< T >::zeros(), and mic::types::Tensor< T >::~Tensor().
|
private |
Dimensions.
Definition at line 692 of file Tensor.hpp.
Referenced by mic::types::Tensor< T >::block(), mic::types::Tensor< T >::concatenate(), mic::types::Tensor< T >::conservativeResize(), mic::types::Tensor< T >::dim(), mic::types::Tensor< T >::dims(), mic::types::Tensor< T >::flatten(), mic::types::Tensor< T >::getIndex(), mic::types::Tensor< T >::load(), mic::types::Tensor< T >::operator+(), mic::types::Tensor< T >::operator-(), mic::types::Tensor< T >::operator=(), mic::types::Tensor< T >::recursiveCalculateSourceIndex(), mic::types::Tensor< T >::recursiveIndex(), mic::types::Tensor< T >::resize(), mic::types::Tensor< T >::save(), mic::types::Tensor< T >::Tensor(), and TEST().
|
private |
Number of elements.
Definition at line 687 of file Tensor.hpp.
Referenced by mic::types::Tensor< T >::concatenate(), mic::types::Tensor< T >::conservativeResize(), mic::types::Tensor< T >::elementwiseFunction(), mic::types::Tensor< T >::elementwiseFunctionScalar(), mic::types::Tensor< T >::enumerate(), mic::types::Tensor< T >::flatten(), mic::types::Tensor< T >::load(), mic::types::Tensor< T >::normRandReal(), mic::types::Tensor< T >::ones(), mic::types::Tensor< T >::operator+(), mic::types::Tensor< T >::operator-(), mic::types::Tensor< T >::operator=(), mic::types::Tensor< T >::rand(), mic::types::Tensor< T >::randn(), mic::types::Tensor< T >::resize(), mic::types::Tensor< T >::save(), mic::types::Tensor< T >::setValue(), mic::types::Tensor< T >::size(), mic::types::Tensor< T >::sum(), mic::types::Tensor< T >::Tensor(), TEST(), and mic::types::Tensor< T >::zeros().