mlpack  master
Public Member Functions | Private Member Functions | Private Attributes | List of all members
mlpack::math::ColumnsToBlocks Class Reference

Transform the columns of the given matrix into a block format. More...

Public Member Functions

 ColumnsToBlocks (size_t rows, size_t cols, size_t blockHeight=0, size_t blockWidth=0)
 Constructor a ColumnsToBlocks object with the given parameters. More...
 
void BlockHeight (const size_t value)
 Set the height of each block; see the constructor for more details. More...
 
size_t BlockHeight () const
 Get the block height. More...
 
void BlockWidth (size_t value)
 Set the width of each block; see the constructor for more details. More...
 
size_t BlockWidth () const
 Get the block width. More...
 
void BufSize (const size_t value)
 Modify the buffer size (the size of the margin around each column of the input). More...
 
size_t BufSize () const
 Get the buffer size. More...
 
void BufValue (const double value)
 Modify the value used for buffer cells; the default is -1. More...
 
double BufValue () const
 Get the value used for buffer cells. More...
 
void Cols (const size_t value)
 Set the number of blocks per column. More...
 
size_t Cols () const
 Return the number of blocks per column. More...
 
void MaxRange (const double value)
 Set the maximum of the range the input will be scaled to, if scaling is enabled (see Scale()). More...
 
double MaxRange () const
 Get the maximum of the range the input will be scaled to, if scaling is enabled (see Scale()). More...
 
void MinRange (const double value)
 Set the minimum of the range the input will be scaled to, if scaling is enabled (see Scale()). More...
 
double MinRange () const
 Get the minimum of the range the input will be scaled to, if scaling is enabled (see Scale()). More...
 
void Rows (const size_t value)
 Set the number of blocks per row. More...
 
size_t Rows () const
 Modify the number of blocks per row. More...
 
void Scale (const bool value)
 Set whether or not scaling is enabled (see also MaxRange() and MinRange()). More...
 
bool Scale () const
 Get whether or not scaling is enabled (see also MaxRange() and MinRange()). More...
 
void Transform (const arma::mat &maximalInputs, arma::mat &output)
 Transform the columns of the input matrix into blocks. More...
 

Private Member Functions

bool IsPerfectSquare (size_t value) const
 Determine whether or not the number is a perfect square. More...
 

Private Attributes

size_t blockHeight
 The height of each block. More...
 
size_t blockWidth
 The width of each block. More...
 
size_t bufSize
 The size of the buffer around each block. More...
 
double bufValue
 The value of the buffer around each block. More...
 
size_t cols
 The number of blocks in each column. More...
 
double maxRange
 The maximum of the range to be scaled to (if scaling is enabled). More...
 
double minRange
 The minimum of the range to be scaled to (if scaling is enabled). More...
 
size_t rows
 The number of blocks in each row. More...
 
bool scale
 Whether or not scaling is enabled. More...
 

Detailed Description

Transform the columns of the given matrix into a block format.

This could be useful with the mlpack::nn::MaximalInputs() function, if your training samples are images. Roughly speaking, given a matrix

[[A] [B] [C] [D]]

then the ColumnsToBlocks class can transform this to something like

[[m m m m m] [m A m B m] [m m m m m] [m C m D m] [m m m m m]]

where A through D are vectors and may themselves be reshaped by ColumnsToBlocks.

An example usage of the ColumnsToBlocks class with the output of MaximalInputs() is given below; this assumes that the images are square, and will return a matrix with a one-element margin, with each maximal input (that is, each column of the maximalInput matrix) as a square block in the output matrix. 5 rows and columns of blocks will be in the output matrix.

// We assume we have a sparse autoencoder 'encoder'.
arma::mat maximalInput; // Store the features learned by sparse autoencoder
mlpack::nn::MaximalInputs(encoder.Parameters(), maximalInput);
arma::mat outputs;
const bool scale = true;
ColumnsToBlocks ctb(5, 5);
arma::mat output;
ctb.Transform(maximalInput, output);
// You can save the output as a pgm, this may help you visualize the training
// results.
output.save(fileName, arma::pgm_binary);

Another example of usage is given below, on a sample matrix.

// This matrix has two columns.
arma::mat input;
input << -1.0000 << 0.1429 << arma::endr
<< -0.7143 << 0.4286 << arma::endr
<< -0.4286 << 0.7143 << arma::endr
<< -0.1429 << 1.0000 << arma::endr;
arma::mat output;
ColumnsToBlocks ctb(1, 2);
ctb.Transform(input, output);
// The columns of the input will be reshaped as a square which is
// surrounded by padding value -1 (this value could be changed with the
// BufValue() method):
// -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
// -1.0000 -1.0000 -0.4286 -1.0000 0.1429 0.7143 -1.0000
// -1.0000 -0.7143 -0.1429 -1.0000 0.4286 1.0000 -1.0000
// -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
// Now, let's change some parameters; let's have each input column output not
// as a square, but as a 4x1 vector.
ctb.BlockWidth(1);
ctb.BlockHeight(4);
ctb.Transform(input, output);
// The output here will be similar, but each maximal input is 4x1:
// -1.0000 -1.0000 -1.0000 -1.0000 -1.0000
// -1.0000 -1.0000 -1.0000 0.1429 -1.0000
// -1.0000 -0.7143 -1.0000 0.4286 -1.0000
// -1.0000 -0.4286 -1.0000 0.7143 -1.0000
// -1.0000 -0.1429 -1.0000 1.0000 -1.0000
// -1.0000 -1.0000 -1.0000 -1.0000 -1.0000

The ColumnsToBlocks class can also, depending on the parameters, scale the input to a given range (useful for exporting to PGM, for instance), and also set the buffer size and value. See the Scale(), MinRange(), MaxRange(), BufSize(), and BufValue() methods for more details.

Definition at line 106 of file columns_to_blocks.hpp.

Constructor & Destructor Documentation

mlpack::math::ColumnsToBlocks::ColumnsToBlocks ( size_t  rows,
size_t  cols,
size_t  blockHeight = 0,
size_t  blockWidth = 0 
)

Constructor a ColumnsToBlocks object with the given parameters.

The rows and cols parameters control the number of blocks per row and column of the output matrix, respectively, and the blockHeight and blockWidth parameters control the size of the individual blocks. If blockHeight and blockWidth are specified, then (blockHeight * blockWidth) must be equal to the number of rows in the input matrix when Transform() is called. If blockHeight and blockWidth are not specified, then the square root of the number of rows of the input matrix will be taken when Transform() is called and that will be used as the block width and height.

Note that the ColumnsToBlocks object can also scale the inputs to a given range; see Scale(), MinRange(), and MaxRange(), and the buffer (margin) size can also be set with BufSize(), and the value used for the buffer can be set with BufValue().

Parameters
rowsNumber of blocks in each column of the output matrix.
colsNumber of blocks in each row of the output matrix.
blockHeightHeight of each block.
blockWidthWidth of each block.
Warning
blockHeight * blockWidth must be equal to maximalInputs.n_rows.

Member Function Documentation

void mlpack::math::ColumnsToBlocks::BlockHeight ( const size_t  value)
inline

Set the height of each block; see the constructor for more details.

Definition at line 149 of file columns_to_blocks.hpp.

References blockHeight.

size_t mlpack::math::ColumnsToBlocks::BlockHeight ( ) const
inline

Get the block height.

Definition at line 151 of file columns_to_blocks.hpp.

References blockHeight.

void mlpack::math::ColumnsToBlocks::BlockWidth ( size_t  value)
inline

Set the width of each block; see the constructor for more details.

Definition at line 154 of file columns_to_blocks.hpp.

References blockWidth.

size_t mlpack::math::ColumnsToBlocks::BlockWidth ( ) const
inline

Get the block width.

Definition at line 156 of file columns_to_blocks.hpp.

References blockWidth.

void mlpack::math::ColumnsToBlocks::BufSize ( const size_t  value)
inline

Modify the buffer size (the size of the margin around each column of the input).

The default value is 1.

Definition at line 160 of file columns_to_blocks.hpp.

References bufSize.

size_t mlpack::math::ColumnsToBlocks::BufSize ( ) const
inline

Get the buffer size.

Definition at line 162 of file columns_to_blocks.hpp.

References bufSize.

void mlpack::math::ColumnsToBlocks::BufValue ( const double  value)
inline

Modify the value used for buffer cells; the default is -1.

Definition at line 165 of file columns_to_blocks.hpp.

References bufValue.

double mlpack::math::ColumnsToBlocks::BufValue ( ) const
inline

Get the value used for buffer cells.

Definition at line 167 of file columns_to_blocks.hpp.

References bufValue.

void mlpack::math::ColumnsToBlocks::Cols ( const size_t  value)
inline

Set the number of blocks per column.

Definition at line 196 of file columns_to_blocks.hpp.

size_t mlpack::math::ColumnsToBlocks::Cols ( ) const
inline

Return the number of blocks per column.

Definition at line 198 of file columns_to_blocks.hpp.

References cols, and IsPerfectSquare().

bool mlpack::math::ColumnsToBlocks::IsPerfectSquare ( size_t  value) const
private

Determine whether or not the number is a perfect square.

Referenced by Cols().

void mlpack::math::ColumnsToBlocks::MaxRange ( const double  value)
inline

Set the maximum of the range the input will be scaled to, if scaling is enabled (see Scale()).

Definition at line 171 of file columns_to_blocks.hpp.

References maxRange.

double mlpack::math::ColumnsToBlocks::MaxRange ( ) const
inline

Get the maximum of the range the input will be scaled to, if scaling is enabled (see Scale()).

Definition at line 174 of file columns_to_blocks.hpp.

References maxRange.

void mlpack::math::ColumnsToBlocks::MinRange ( const double  value)
inline

Set the minimum of the range the input will be scaled to, if scaling is enabled (see Scale()).

Definition at line 178 of file columns_to_blocks.hpp.

References minRange.

double mlpack::math::ColumnsToBlocks::MinRange ( ) const
inline

Get the minimum of the range the input will be scaled to, if scaling is enabled (see Scale()).

Definition at line 181 of file columns_to_blocks.hpp.

References minRange.

void mlpack::math::ColumnsToBlocks::Rows ( const size_t  value)
inline

Set the number of blocks per row.

Definition at line 191 of file columns_to_blocks.hpp.

size_t mlpack::math::ColumnsToBlocks::Rows ( ) const
inline

Modify the number of blocks per row.

Definition at line 193 of file columns_to_blocks.hpp.

References rows.

void mlpack::math::ColumnsToBlocks::Scale ( const bool  value)
inline

Set whether or not scaling is enabled (see also MaxRange() and MinRange()).

Definition at line 185 of file columns_to_blocks.hpp.

References scale.

bool mlpack::math::ColumnsToBlocks::Scale ( ) const
inline

Get whether or not scaling is enabled (see also MaxRange() and MinRange()).

Definition at line 188 of file columns_to_blocks.hpp.

References scale.

void mlpack::math::ColumnsToBlocks::Transform ( const arma::mat &  maximalInputs,
arma::mat &  output 
)

Transform the columns of the input matrix into blocks.

If blockHeight and blockWidth were not specified in the constructor (and BlockHeight() and BlockWidth() were not called), then the number of rows in the input matrix must be a perfect square.

Parameters
inputInput matrix to transform.
outputMatrix to store transformed output in.

Member Data Documentation

size_t mlpack::math::ColumnsToBlocks::blockHeight
private

The height of each block.

Definition at line 205 of file columns_to_blocks.hpp.

Referenced by BlockHeight().

size_t mlpack::math::ColumnsToBlocks::blockWidth
private

The width of each block.

Definition at line 207 of file columns_to_blocks.hpp.

Referenced by BlockWidth().

size_t mlpack::math::ColumnsToBlocks::bufSize
private

The size of the buffer around each block.

Definition at line 209 of file columns_to_blocks.hpp.

Referenced by BufSize().

double mlpack::math::ColumnsToBlocks::bufValue
private

The value of the buffer around each block.

Definition at line 211 of file columns_to_blocks.hpp.

Referenced by BufValue().

size_t mlpack::math::ColumnsToBlocks::cols
private

The number of blocks in each column.

Definition at line 221 of file columns_to_blocks.hpp.

Referenced by Cols().

double mlpack::math::ColumnsToBlocks::maxRange
private

The maximum of the range to be scaled to (if scaling is enabled).

Definition at line 215 of file columns_to_blocks.hpp.

Referenced by MaxRange().

double mlpack::math::ColumnsToBlocks::minRange
private

The minimum of the range to be scaled to (if scaling is enabled).

Definition at line 213 of file columns_to_blocks.hpp.

Referenced by MinRange().

size_t mlpack::math::ColumnsToBlocks::rows
private

The number of blocks in each row.

Definition at line 219 of file columns_to_blocks.hpp.

Referenced by Rows().

bool mlpack::math::ColumnsToBlocks::scale
private

Whether or not scaling is enabled.

Definition at line 217 of file columns_to_blocks.hpp.

Referenced by Scale().


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