nnlib
GPU-accelerated, C/C++ neural network library.
Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | List of all members
Tensor Class Reference

Class to represent multidimensional arrays. More...

#include <tensor.h>

Public Member Functions

 Tensor ()
 Initialize an empty tensor.
 
 Tensor (const Tensor &other)
 The copy constructor. More...
 
 Tensor (std::vector< size_t > shape)
 Tensor constructor with shape given directly by a vector. More...
 
 Tensor (std::vector< size_t > shape, DataLocation location)
 
template<typename... Args>
 Tensor (Args... args)
 Construct a tensor based on the passed shape. More...
 
Tensoroperator= (const Tensor &other)
 The assignment operator. More...
 
std::shared_ptr< Tensorcopy () const
 
void move (DataLocation target)
 Move the tensor to the designated destination. More...
 
void useGrad ()
 
void backward ()
 
template<typename... Args>
float & operator() (Args... args)
 Method to access an element of the tensor at a specific method. More...
 
 ~Tensor ()
 The destructor.
 

Static Public Member Functions

static Tensor construct1d (const std::vector< float > &data)
 Static method to easily initialize a 1D tensor with given data. More...
 
static Tensor construct2d (const std::vector< std::vector< float >> &data)
 Static method to easily initialize a 2D tensor with given data. More...
 

Public Attributes

std::vector< size_t > shape
 Store the shape of the tensor.
 
size_t size
 Store the total size of the tensor. More...
 
DataLocation location
 The location of the tensor. More...
 
float * data
 The data stored by the tensor.
 
Session session
 Session object containing information about current session.
 
bool requiresGrad
 
std::shared_ptr< BackwardFunctiongradFunction
 
std::shared_ptr< Tensorgrad
 

Private Member Functions

void computeSize ()
 Compute the total size of the tensor based on its shape.
 
size_t findEffectiveAddress (const std::vector< size_t > &index, size_t depth) const
 Finds the address of an element in the flattened data array given its index in non-flattened tensor. More...
 
void verifyIndex (const std::vector< size_t > &index) const
 Verify that an index of an element is within the shape of the tensor. More...
 

Detailed Description

Class to represent multidimensional arrays.

Constructor & Destructor Documentation

◆ Tensor() [1/3]

Tensor::Tensor ( const Tensor other)

The copy constructor.

Parameters
otherThe tensor based on which this one should be initialized.

◆ Tensor() [2/3]

Tensor::Tensor ( std::vector< size_t >  shape)
explicit

Tensor constructor with shape given directly by a vector.

Parameters
shapeThe shape of the vector to create.

◆ Tensor() [3/3]

template<typename... Args>
Tensor::Tensor ( Args...  args)
inlineexplicit

Construct a tensor based on the passed shape.

For example Tensor(2, 2, 3) will initialize a 3D tensor with two 2x3 matrices.

Parameters
argsThe shapes of consecutive dimensions.

Member Function Documentation

◆ construct1d()

Tensor Tensor::construct1d ( const std::vector< float > &  data)
static

Static method to easily initialize a 1D tensor with given data.

Parameters
dataThe data based on which a tensor should be constructed.
Returns
The constructed tensor.

◆ construct2d()

Tensor Tensor::construct2d ( const std::vector< std::vector< float >> &  data)
static

Static method to easily initialize a 2D tensor with given data.

Parameters
dataThe data based on which a tensor should be constructed.
Returns
The constructed tensor.

◆ findEffectiveAddress()

size_t Tensor::findEffectiveAddress ( const std::vector< size_t > &  index,
size_t  depth 
) const
private

Finds the address of an element in the flattened data array given its index in non-flattened tensor.

Since the method works recursively, it also takes the depth parameter, which provides information about which dimension is currently taken into account when computing the index.

Parameters
indexThe multidimensional index of the element to access.
depthThe dimension that is currently considered in the recursive call.
Returns
The address of the element in the flattened data array.

◆ move()

void Tensor::move ( DataLocation  target)

Move the tensor to the designated destination.

This involves copying the data to the new location and releasing memory from the old location.

Parameters
targetThe destination to move the tensor to.

◆ operator()()

template<typename... Args>
float& Tensor::operator() ( Args...  args)
inline

Method to access an element of the tensor at a specific method.

This method should not be used in performance critical operations. In these cases a direct access to Tensor::data will be more appropriate (as it becomes easier for the compiler to optimize such code).

Parameters
argsThe index of the element to access.
Returns
A reference to the requested element.

◆ operator=()

Tensor & Tensor::operator= ( const Tensor other)

The assignment operator.

This releases all the current memory and copies all the information of the other tensor.

Parameters
otherThe other tensor to assign this one to.
Returns
Assigned tensor. Always returns *this.

◆ verifyIndex()

void Tensor::verifyIndex ( const std::vector< size_t > &  index) const
private

Verify that an index of an element is within the shape of the tensor.

Parameters
indexThe index of the element that is being accessed.

Member Data Documentation

◆ location

DataLocation Tensor::location

The location of the tensor.

Can be either HOST or DEVICE. See DataLocation for more details.

◆ size

size_t Tensor::size

Store the total size of the tensor.

The size is equal to the number of elements that can be contained in the tensor. It is computed in the Tensor::computeSize() method.


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