A class for holding an array of matrices. More...
#include <DoubleMatrixArray.h>
Public Member Functions | |
DoubleMatrixArray () | |
Construct an empty DoubleMatrixArray object. | |
DoubleMatrixArray (int rows, int cols, int len) | |
Construct DoubleMatrixArray of given size, filled with zeroes. More... | |
DoubleMatrixArray (std::ifstream &matricesFile) | |
Construct a new DoubleMatrixArray, initialized from text stream. More... | |
DoubleMatrixArray (int len, const std::vector< std::vector< double >> &fixedMatrix) | |
Construct a new DoubleMatrixArray with a given matrix duplicated a number of times. More... | |
DoubleMatrixArray (int len, const std::vector< std::vector< float >> &fixedMatrix) | |
Construct a new DoubleMatrixArray with a given matrix duplicated a number of times. More... | |
DoubleMatrixArray (int len, const DoubleMatrix &fixedMatrix) | |
Construct a new DoubleMatrixArray with a given matrix duplicated a number of times. More... | |
DoubleMatrixArray (const boost::numeric::ublas::tensor< double > &tensor) | |
Construct a new DoubleMatrixArray from a given tensor. More... | |
void init (int rows, int cols, int len) | |
Initializes to a given size, filled with zeroes. More... | |
void init (int len, const std::vector< std::vector< double >> &fixedMatrix) | |
Initializes to a given matrix duplicated a number of times. More... | |
void init (int len, const std::vector< std::vector< float >> &fixedMatrix) | |
Initializes to a given matrix duplicated a number of times. More... | |
void init (int len, const DoubleMatrix &fixedMatrix) | |
Initializes to a given matrix duplicated a number of times. More... | |
void init (const boost::numeric::ublas::tensor< double > &tensor) | |
Initializes to a given tensor. More... | |
void initFromH5File (int len, const std::string &h5File, const std::string &path) | |
Initialize to values loaded from h5 file. More... | |
std::streamoff save (std::ostream &stream) const | |
Save to binary stream. More... | |
std::streamoff load (std::istream &stream) | |
Load from binary stream. More... | |
void loadMatrix (int matrixIndex, const std::string &h5File, const std::string &path) | |
Loads a single matrix to a specific position along the third dimension. More... | |
boost::numeric::ublas::tensor< double > getTensor () const | |
Returns the content of this object as a 3d tensor. | |
DoubleMatrixArray getConvolution (const DoubleMatrixArray &filter, int strideRows, int strideCol) const | |
Returns the result of convolution between this object and given filters. More... | |
int size () const | |
Returns the size of third dimension. | |
const DoubleMatrix & getMat (int i) const | |
Returns the i'th matrix, or i'th slice along third dimension. More... | |
DoubleMatrix & getMat (int i) | |
Returns the i'th matrix, or i'th slice along third dimension. More... | |
int rows () const | |
Returns number of rows, or size of first dimension. | |
int cols () const | |
Returns number of columns, or size of second dimension. | |
DoubleMatrixArray getMatrixMultiply (const DoubleMatrixArray &other) const | |
Returns the result of multiplying this with other according to the rules of matrixmultiplication. More... | |
DoubleMatrixArray getTranspose () const | |
Returns a DoubleMatrixArray with every matrix transposed. | |
DoubleMatrixArray getSubMatrix (int row1, int col1, int row2, int col2) const | |
Returns a DoubleMatrixArray with every matrix sliced. More... | |
DoubleMatrixArray getSubMatrix (int row, int col) const | |
Returns a DoubleMatrixArray with a (1x1) slice of every matrix. More... | |
DoubleMatrixArray getColumn (int col) const | |
Returns a DoubleMatrixArray with a column slice of every matrix. More... | |
DoubleMatrixArray getMeanAlongRows () const | |
Returns a DoubleMatrixArray with the rows reduced to a single row by calculating the mean of each column. | |
DoubleMatrixArray getMeanAlongCols () const | |
Returns a DoubleMatrixArray with the columns reduced to a single column by calculating the mean of each row. | |
DoubleMatrixArray getMeanInDepth () const | |
Returns a DoubleMatrixArray reduced to a single matrix containing the mean of all matrices. | |
DoubleMatrixArray getSumAlongRows () const | |
Returns a DoubleMatrixArray with the rows reduced to a single row by calculating the sum of each column. | |
DoubleMatrixArray getSumAlongCols () const | |
Returns a DoubleMatrixArray with the cols reduced to a single col by calculating the sum of each row. | |
DoubleMatrixArray getSumInDepth () const | |
Returns a DoubleMatrixArray reduced to a single matrix containing the sum of all matrices. | |
DoubleMatrixArray getMultiplyByScalar (double scalar) const | |
Returns a DoubleMatrixArray resulting from multiplying this object with a scalar. More... | |
void flattenIntoVec (std::vector< double > &vec) const | |
Appends all elements to given vector. More... | |
void transpose () | |
Transposes each matrix in this double matrix array. | |
void square () | |
Elementwise square all elements. | |
void add (const DoubleMatrixArray &other) | |
void addAt (double val, int row, int col) | |
void addAt (const DoubleMatrixArray &other, int startRow, int startCol) | |
void sub (const DoubleMatrixArray &other) | |
void elementMultiply (const DoubleMatrixArray &other) | |
void elementMultiplyAt (const DoubleMatrixArray &other, int startRow, int startCol) | |
void multiplyByScalar (double scalar) | |
void innerSum () | |
void meanAlongRows () | |
void meanAlongCols () | |
void meanInDepth () | |
void sumAlongRows () | |
void sumAlongCols () | |
void sumInDepth () | |
void initRandom () | |
void initRandomSame () | |
void initConst (double v) | |
void initHotel () | |
void debugPrint (std::ostream &out, const std::string &title, int verboseLevel=1) const | |
double testEquals (const std::string &title, const DoubleMatrixArray &other, double eps=1e-6) const | |
double testEqualsZero (const std::string &title, const std::vector< int > &zs, double eps=1e-6) const | |
void testSameAlongDepth (const std::string &title, double eps=1e-6) const | |
Tests matrices in all depths are the same. | |
void testSameSize (const std::string &title, const DoubleMatrixArray &other) const | |
void testSameDims (const std::string &title, const DoubleMatrixArray &other) const | |
bool checkIfEqual (const DoubleMatrixArray &other, double eps=1e-6) const | |
bool checkIfSameSize (const DoubleMatrixArray &other) const | |
bool checkIfSameDims (const DoubleMatrixArray &other) const | |
void pushBackMatrix (const DoubleMatrix &mat) | |
DoubleMatrixArray depthSlice (int start, int end) const | |
Returns a slice along the depth dimension. More... | |
void appendCols (const DoubleMatrixArray &other) | |
Appends given matrix to the right of this matrix. | |
void appendRows (const DoubleMatrixArray &other) | |
Appends given matrix to the bottom of this matrix. | |
void clear () | |
void setInAllDepth (int i, int j, double val) | |
Runs getMat(k).set(i,j,val) for all k. | |
std::vector< double > getInAllDepth (int i, int j, double scale=1.0) const | |
Returns the values at (i,j,k) for all k, scaled by scale. | |
Detailed Description
A class for holding an array of matrices.
It used for handling a batch of matrices, and can perform SIMD (single instruction multiple data) ops on all of them. For example, matrix multiplication with another DoubleMatrixArray means multiplying the corresponding i'th matrix in each array. It can also be described as a 3d tensor.
Constructor & Destructor Documentation
◆ DoubleMatrixArray() [1/6]
helayers::DoubleMatrixArray::DoubleMatrixArray | ( | int | rows, |
int | cols, | ||
int | len | ||
) |
Construct DoubleMatrixArray of given size, filled with zeroes.
- Parameters
-
rows Number of rows cols Number of columns len Length of third dimension
◆ DoubleMatrixArray() [2/6]
helayers::DoubleMatrixArray::DoubleMatrixArray | ( | std::ifstream & | matricesFile | ) |
Construct a new DoubleMatrixArray, initialized from text stream.
The file is expected to have the following format: [[mat_{1,1,1}, . . . , mat_{1,n,1}] [mat_{2,1,1}, . . . , mat_{2,n,1}] ... [mat_{m,1,1}, . . . , mat_{m,n,1}], ... [mat_{1,1,k}, . . . , mat_{1,n,k}] [mat_{2,1,k}, . . . , mat_{2,n,k}] ... [mat_{m,1,k}, . . . , mat_{m,n,k}]]
- Parameters
-
matricesFile Stream to read from
◆ DoubleMatrixArray() [3/6]
helayers::DoubleMatrixArray::DoubleMatrixArray | ( | int | len, |
const std::vector< std::vector< double >> & | fixedMatrix | ||
) |
Construct a new DoubleMatrixArray with a given matrix duplicated a number of times.
- Parameters
-
len Size of the third dimension. fixedMatrix The matrix to be duplicated.
◆ DoubleMatrixArray() [4/6]
helayers::DoubleMatrixArray::DoubleMatrixArray | ( | int | len, |
const std::vector< std::vector< float >> & | fixedMatrix | ||
) |
Construct a new DoubleMatrixArray with a given matrix duplicated a number of times.
- Parameters
-
len Size of the third dimension. fixedMatrix The matrix to be duplicated.
◆ DoubleMatrixArray() [5/6]
helayers::DoubleMatrixArray::DoubleMatrixArray | ( | int | len, |
const DoubleMatrix & | fixedMatrix | ||
) |
Construct a new DoubleMatrixArray with a given matrix duplicated a number of times.
- Parameters
-
len Size of the third dimension. fixedMatrix The matrix to be duplicated.
◆ DoubleMatrixArray() [6/6]
helayers::DoubleMatrixArray::DoubleMatrixArray | ( | const boost::numeric::ublas::tensor< double > & | tensor | ) |
Construct a new DoubleMatrixArray from a given tensor.
- Parameters
-
tensor A 3d tensor containing the values.
Member Function Documentation
◆ depthSlice()
DoubleMatrixArray helayers::DoubleMatrixArray::depthSlice | ( | int | start, |
int | end | ||
) | const |
Returns a slice along the depth dimension.
In the range [start,end)
◆ flattenIntoVec()
void helayers::DoubleMatrixArray::flattenIntoVec | ( | std::vector< double > & | vec | ) | const |
Appends all elements to given vector.
Dimension order: Cols, Rows, Depth (cols runs first)
- Parameters
-
vec Output vector
◆ getColumn()
DoubleMatrixArray helayers::DoubleMatrixArray::getColumn | ( | int | col | ) | const |
Returns a DoubleMatrixArray with a column slice of every matrix.
- Parameters
-
col Column index
◆ getConvolution()
DoubleMatrixArray helayers::DoubleMatrixArray::getConvolution | ( | const DoubleMatrixArray & | filter, |
int | strideRows, | ||
int | strideCol | ||
) | const |
Returns the result of convolution between this object and given filters.
Filters are expected to have the same 3d dimension size as this object. The convolution is computed in SIMD fashion for each matrix and filter.
- Parameters
-
filter Filter to compute convolution with. strideRows Convolution stride along rows strideCol Convolution stride along column
◆ getMat() [1/2]
|
inline |
Returns the i'th matrix, or i'th slice along third dimension.
- Parameters
-
i Index along third dimension
◆ getMat() [2/2]
|
inline |
Returns the i'th matrix, or i'th slice along third dimension.
- Parameters
-
i Index along third dimension
◆ getMatrixMultiply()
DoubleMatrixArray helayers::DoubleMatrixArray::getMatrixMultiply | ( | const DoubleMatrixArray & | other | ) | const |
Returns the result of multiplying this with other according to the rules of matrixmultiplication.
Both arrays should have the same depth. Returns an object res such that res.getMat(i)=this.getMat(i)*other.getMat(i)
- Parameters
-
other other object to multiply with
◆ getMultiplyByScalar()
DoubleMatrixArray helayers::DoubleMatrixArray::getMultiplyByScalar | ( | double | scalar | ) | const |
Returns a DoubleMatrixArray resulting from multiplying this object with a scalar.
- Parameters
-
scalar Scalar to multiyply with
◆ getSubMatrix() [1/2]
DoubleMatrixArray helayers::DoubleMatrixArray::getSubMatrix | ( | int | row, |
int | col | ||
) | const |
Returns a DoubleMatrixArray with a (1x1) slice of every matrix.
- Parameters
-
row Row index col Column index
◆ getSubMatrix() [2/2]
DoubleMatrixArray helayers::DoubleMatrixArray::getSubMatrix | ( | int | row1, |
int | col1, | ||
int | row2, | ||
int | col2 | ||
) | const |
Returns a DoubleMatrixArray with every matrix sliced.
- Parameters
-
row1 First row index col1 First column index row2 Last row index (exclusive) col2 Last column index (exclusive)
◆ init() [1/5]
void helayers::DoubleMatrixArray::init | ( | const boost::numeric::ublas::tensor< double > & | tensor | ) |
Initializes to a given tensor.
- Parameters
-
tensor A 3d tensor containing the values.
◆ init() [2/5]
void helayers::DoubleMatrixArray::init | ( | int | len, |
const DoubleMatrix & | fixedMatrix | ||
) |
Initializes to a given matrix duplicated a number of times.
Overwrites existing state.
- Parameters
-
len Size of the third dimension. fixedMatrix The matrix to be duplicated.
◆ init() [3/5]
void helayers::DoubleMatrixArray::init | ( | int | len, |
const std::vector< std::vector< double >> & | fixedMatrix | ||
) |
Initializes to a given matrix duplicated a number of times.
Overwrites existing state.
- Parameters
-
len Size of the third dimension. fixedMatrix The matrix to be duplicated.
◆ init() [4/5]
void helayers::DoubleMatrixArray::init | ( | int | len, |
const std::vector< std::vector< float >> & | fixedMatrix | ||
) |
Initializes to a given matrix duplicated a number of times.
Overwrites existing state.
- Parameters
-
len Size of the third dimension. fixedMatrix The matrix to be duplicated.
◆ init() [5/5]
void helayers::DoubleMatrixArray::init | ( | int | rows, |
int | cols, | ||
int | len | ||
) |
Initializes to a given size, filled with zeroes.
Overwrites existing state.
- Parameters
-
rows Number of rows cols Number of columns len Length of third dimension
◆ initFromH5File()
void helayers::DoubleMatrixArray::initFromH5File | ( | int | len, |
const std::string & | h5File, | ||
const std::string & | path | ||
) |
Initialize to values loaded from h5 file.
Loads a single matrix from h5 file, and duplicates it along third dimension.
- Parameters
-
len Number of duplicates along third dimension h5File h5 file to load from path path inside h5 file
◆ load()
streamoff helayers::DoubleMatrixArray::load | ( | std::istream & | stream | ) |
Load from binary stream.
- Parameters
-
stream stream to load from
◆ loadMatrix()
void helayers::DoubleMatrixArray::loadMatrix | ( | int | matrixIndex, |
const std::string & | h5File, | ||
const std::string & | path | ||
) |
Loads a single matrix to a specific position along the third dimension.
- Parameters
-
matrixIndex The position along the third dimension to load to h5File h5 file to load from path path inside h5 file
◆ save()
streamoff helayers::DoubleMatrixArray::save | ( | std::ostream & | stream | ) | const |
Save to binary stream.
- Parameters
-
stream stream to save to.
The documentation for this class was generated from the following files:
- /opt/IBM/FHE-distro/ML-HElib/src/helayers/simple_nn/DoubleMatrixArray.h
- /opt/IBM/FHE-distro/ML-HElib/src/helayers/simple_nn/DoubleMatrixArray.cpp