MachineIntelligenceCore:Algorithms
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
mic::types::Tensor< T > Class Template Reference

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_)
 
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_)
 

Detailed Description

template<typename T>
class mic::types::Tensor< T >

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.

Author
tkornuta
Template Parameters
Ttemplate parameter denoting data type stored in tensor.

Definition at line 49 of file Matrix.hpp.

Constructor & Destructor Documentation

template<typename T>
mic::types::Tensor< T >::Tensor ( )
inline

Default constructor (empty).

Definition at line 69 of file Tensor.hpp.

template<typename T>
mic::types::Tensor< T >::Tensor ( std::initializer_list< size_t >  dims_)
inline

Constructor - sets the tensor dimension and assigns memory.

Parameters
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().

Here is the call graph for this function:

template<typename T>
mic::types::Tensor< T >::Tensor ( std::vector< size_t >  dims_)
inline

Constructor - sets the tensor dimension and assigns memory.

Parameters
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().

Here is the call graph for this function:

template<typename T>
mic::types::Tensor< T >::Tensor ( const Tensor< T > &  t)
inline

Copying constructor - copies the values of the given tensor, including tensor dimensions and data.

Parameters
tThe 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.

template<typename T>
mic::types::Tensor< T >::Tensor ( const mic::types::Matrix< T > &  mat_)
inline

Copying constructor - copies the values of the given 2D matrix.

Parameters
tThe 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.

template<typename T>
mic::types::Tensor< T >::~Tensor ( )
inline

Destructor. Frees memory (if it was assigned).

Definition at line 182 of file Tensor.hpp.

References mic::types::Tensor< T >::data_ptr.

Member Function Documentation

template<typename T>
Tensor<T> mic::types::Tensor< T >::block ( std::vector< std::vector< size_t > >  ranges_)
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.

Parameters
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).
Returns
The created subtensor

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().

Here is the call graph for this function:

template<typename T>
void mic::types::Tensor< T >::concatenate ( const Tensor< T > &  obj_)
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.

Parameters
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().

template<typename T>
void mic::types::Tensor< T >::concatenate ( std::vector< mic::types::Tensor< T > >  tensors_)
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.

Parameters
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.

template<typename T>
void mic::types::Tensor< T >::conservativeResize ( std::vector< size_t >  dims_)
inline

Resizes the tensor - sets new tensor dimensions. Important note: the total number of elements must remain unchanged.

Parameters
dims_New dimensions.

Definition at line 200 of file Tensor.hpp.

References mic::types::Tensor< T >::dimensions, and mic::types::Tensor< T >::elements.

template<typename T>
T* mic::types::Tensor< T >::data ( )
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().

template<typename T>
size_t mic::types::Tensor< T >::dim ( size_t  k)
inline

Returns k-th dimension.

Definition at line 315 of file Tensor.hpp.

References mic::types::Tensor< T >::dimensions.

template<typename T>
std::vector<size_t> mic::types::Tensor< T >::dims ( )
inline
template<typename T>
void mic::types::Tensor< T >::elementwiseFunction ( T(*)(T)  func)
inline

Applies the function to all tensor elements.

Parameters
funcThe 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.

template<typename T>
void mic::types::Tensor< T >::elementwiseFunctionScalar ( T(*)(T, T)  func,
scalar 
)
inline

Applies the function to all tensor elements. Additionally passes scalar to the function as second argument.

Parameters
funcThe function to be applied. This must be a function with exactly two arguments.
scalarScalar 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.

template<typename T>
void mic::types::Tensor< T >::enumerate ( )
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.

Referenced by main(), and TEST().

template<typename T>
void mic::types::Tensor< T >::flatten ( )
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().

template<typename T>
size_t mic::types::Tensor< T >::getIndex ( std::vector< size_t >  coordinates_)
inline

Returns the index of an element in nD matrix. For dimensions higher than 3 it uses recursive function.

Parameters
coordinates_nD vector of element coordinates ({ }, vector<size_t> etc.).
Returns
Index of the element.

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()().

Here is the call graph for this function:

template<typename T>
template<class Archive >
void mic::types::Tensor< T >::load ( Archive &  ar,
const unsigned int  version 
)
inlineprivate

Serialization load - loads the tensor object to archive.

Parameters
arUsed archive.
versionVersion 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.

template<typename T>
void mic::types::Tensor< T >::normRandReal ( float  mean = 0,
float  stddev = 1 
)
inline

Set values of all matrix elements to random with a normal distribution.

Parameters
meanMean
stddevVariance

Definition at line 286 of file Tensor.hpp.

References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::elements.

template<typename T>
void mic::types::Tensor< T >::ones ( )
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.

template<typename T>
T& mic::types::Tensor< T >::operator() ( size_t  index_)
inline

Operator used for setting the value of a given element of nD tensor.

Parameters
index_Index (a single value).
Returns
The value of the element.

Definition at line 483 of file Tensor.hpp.

References mic::types::Tensor< T >::data_ptr.

template<typename T>
T& mic::types::Tensor< T >::operator() ( std::vector< size_t >  coordinates_)
inline

Operator used for setting the value of a given element of nD tensor.

Parameters
coordinates_nD vector of element coordinates ({ }, vector<size_t> etc.).
Returns
The value of the element.

Definition at line 492 of file Tensor.hpp.

References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::getIndex().

Here is the call graph for this function:

template<typename T>
const T& mic::types::Tensor< T >::operator() ( size_t  index_) const
inline

Operator returning the value of a given element of nD tensor.

Parameters
index_Index (a single value).
Returns
The value of the element.

Definition at line 501 of file Tensor.hpp.

References mic::types::Tensor< T >::data_ptr.

template<typename T>
const T& mic::types::Tensor< T >::operator() ( std::vector< size_t >  coordinates_) const
inline

Operator returning the value of a given element of nD tensor.

Parameters
coordinates_nD vector of element coordinates ({ }, vector<size_t> etc.).
Returns
The value of the element.

Definition at line 510 of file Tensor.hpp.

References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::getIndex().

Here is the call graph for this function:

template<typename T>
mic::types::Tensor<T> mic::types::Tensor< T >::operator+ ( mic::types::Tensor< T >  obj_)
inline

Operator returning new tensor being the sum of two tensors.

Parameters
obj_The second tensor (on the right side of the +).
Returns
New, resulting tensor being the sum.

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().

Here is the call graph for this function:

template<typename T>
mic::types::Tensor<T> mic::types::Tensor< T >::operator- ( mic::types::Tensor< T >  obj_)
inline

Operator returning new tensor being the difference of two tensors.

Parameters
obj_The second tensor (on the right side of the -).
Returns
New, resulting tensor being the tensor difference.

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().

Here is the call graph for this function:

template<typename T>
const Tensor<T>& mic::types::Tensor< T >::operator= ( const Tensor< T > &  t)
inline

Assign operator - copies the values of the given tensor, including tensor dimensions and data.

Parameters
tThe 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.

template<typename T>
void mic::types::Tensor< T >::rand ( min = 0,
max = 1 
)
inline

Set values of all matrix elements to random numbers from range <min, max> - uniform distribution.

Parameters
minMin value.
maxMax value.
Returns
Random real value.

Definition at line 387 of file Tensor.hpp.

References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::elements.

template<typename T>
void mic::types::Tensor< T >::randn ( mean = 0,
stddev = 1 
)
inline

Set values of all matrix elements to random with a normal distribution.

Parameters
meanMean
stddevVariance

Definition at line 368 of file Tensor.hpp.

References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::elements.

Referenced by TEST().

template<typename T>
void mic::types::Tensor< T >::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_ 
)
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!

Parameters
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().

Here is the call graph for this function:

template<typename T>
size_t mic::types::Tensor< T >::recursiveCalculateSourceIndex ( size_t  dim_,
std::vector< size_t >  is_,
size_t  offset_ 
)
inlineprivate

A recursive function responsible for the calculation of position (index) in source (i.e. this) data table.

Parameters
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]).
Returns
Index in source (this) data table.

Definition at line 788 of file Tensor.hpp.

References mic::types::Tensor< T >::dimensions.

Referenced by mic::types::Tensor< T >::recursiveBlockCopy().

template<typename T>
size_t mic::types::Tensor< T >::recursiveCalculateTargetIndex ( size_t  dim_,
std::vector< size_t >  js_,
std::vector< size_t >  dims_ 
)
inlineprivate

A recursive function responsible for the calculation of position (index) in target (i.e. new) data table.

Parameters
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.
Returns
Index in target (new) data table.

Definition at line 761 of file Tensor.hpp.

Referenced by mic::types::Tensor< T >::recursiveBlockCopy().

template<typename T>
size_t mic::types::Tensor< T >::recursiveIndex ( size_t  dim_,
std::vector< size_t >  coordinates_ 
)
inlineprivate

Recursive method computing the index of element in nD matrix.

Parameters
dim_Dimension considered at the moment.
coordinates_nD vector of coordinates.
Returns
Index of the element.

Definition at line 705 of file Tensor.hpp.

References mic::types::Tensor< T >::dimensions.

Referenced by mic::types::Tensor< T >::getIndex().

template<typename T>
void mic::types::Tensor< T >::resize ( std::vector< size_t >  dims_)
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.

Parameters
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().

Here is the call graph for this function:

template<typename T>
template<class Archive >
void mic::types::Tensor< T >::save ( Archive &  ar,
const unsigned int  version 
) const
inlineprivate

Serialization save - saves the tensor object to archive.

Parameters
arUsed archive.
versionVersion 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.

template<typename T>
void mic::types::Tensor< T >::setValue ( value_)
inline

Sets values of all element to the value given as parameter.

Parameters
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.

template<typename T>
size_t mic::types::Tensor< T >::size ( )
inline
template<typename T>
T mic::types::Tensor< T >::sum ( )
inline

Functions sums the tensor elements.

Returns
Sum of tensor elements.

Definition at line 449 of file Tensor.hpp.

References mic::types::Tensor< T >::data_ptr, and mic::types::Tensor< T >::elements.

template<typename T>
void mic::types::Tensor< T >::zeros ( )
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().

Friends And Related Function Documentation

template<typename T>
friend class boost::serialization::access
friend

Definition at line 808 of file Tensor.hpp.

template<typename T>
std::ostream& operator<< ( std::ostream &  os_,
const Tensor< T > &  obj_ 
)
friend

Stream operator enabling to print tensor dimensions and all values.

Parameters
os_Ostream object.
obj_Tensor object.

Definition at line 463 of file Tensor.hpp.

Member Data Documentation


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