The implementation of the RBM module. More...
Public Types | |
typedef DataType::elem_type | ElemType |
using | NetworkType = RBM< InitializationRuleType, DataType, PolicyType > |
Public Member Functions | |
RBM (arma::Mat< ElemType > predictors, InitializationRuleType initializeRule, const size_t visibleSize, const size_t hiddenSize, const size_t batchSize=1, const size_t numSteps=1, const size_t negSteps=1, const size_t poolSize=2, const ElemType slabPenalty=8, const ElemType radius=1, const bool persistence=false) | |
Initialize all the parameters of the network using initializeRule. More... | |
double | Evaluate (const arma::Mat< ElemType > ¶meters, const size_t i, const size_t batchSize) |
Evaluate the RBM network with the given parameters. More... | |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, BinaryRBM >::value, double >::type | FreeEnergy (const arma::Mat< ElemType > &input) |
This function calculates the free energy of the BinaryRBM. More... | |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, SpikeSlabRBM >::value, double >::type | FreeEnergy (const arma::Mat< ElemType > &input) |
This function calculates the free energy of the SpikeSlabRBM. More... | |
void | Gibbs (const arma::Mat< ElemType > &input, arma::Mat< ElemType > &output, const size_t steps=SIZE_MAX) |
This function does the k-step Gibbs Sampling. More... | |
void | Gradient (const arma::Mat< ElemType > ¶meters, const size_t i, arma::Mat< ElemType > &gradient, const size_t batchSize) |
Calculates the gradients for the RBM network. More... | |
DataType const & | HiddenBias () const |
Return the hidden bias of the network. More... | |
DataType & | HiddenBias () |
Modify the hidden bias of the network. More... | |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, BinaryRBM >::value, void >::type | HiddenMean (const InputType &input, DataType &output) |
The function calculates the mean for the hidden layer. More... | |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, SpikeSlabRBM >::value, void >::type | HiddenMean (const InputType &input, DataType &output) |
The function calculates the mean of the Normal distribution of P(s|v, h). More... | |
size_t const & | HiddenSize () const |
Get the hidden size. More... | |
size_t | NumFunctions () const |
Return the number of separable functions (the number of predictor points). More... | |
size_t | NumSteps () const |
Return the number of steps of Gibbs Sampling. More... | |
const arma::Mat< ElemType > & | Parameters () const |
Return the parameters of the network. More... | |
arma::Mat< ElemType > & | Parameters () |
Modify the parameters of the network. More... | |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, BinaryRBM >::value, void >::type | Phase (const InputType &input, DataType &gradient) |
Calculates the gradient of the RBM network on the provided input. More... | |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, SpikeSlabRBM >::value, void >::type | Phase (const InputType &input, DataType &gradient) |
Calculates the gradient of the RBM network on the provided input. More... | |
size_t const & | PoolSize () const |
Get the pool size. More... | |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, BinaryRBM >::value, void >::type | Reset () |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, SpikeSlabRBM >::value, void >::type | Reset () |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, BinaryRBM >::value, void >::type | SampleHidden (const arma::Mat< ElemType > &input, arma::Mat< ElemType > &output) |
This function samples the hidden layer given the visible layer using Bernoulli function. More... | |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, SpikeSlabRBM >::value, void >::type | SampleHidden (const arma::Mat< ElemType > &input, arma::Mat< ElemType > &output) |
This function samples the slab outputs from the Normal distribution with mean given by: and variance: . More... | |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, SpikeSlabRBM >::value, void >::type | SampleSlab (InputType &slabMean, DataType &slab) |
The function samples from the Normal distribution of P(s|v, h), where the mean is given by: and variance is given by: . More... | |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, SpikeSlabRBM >::value, void >::type | SampleSpike (InputType &spikeMean, DataType &spike) |
The function samples the spike function using Bernoulli distribution. More... | |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, BinaryRBM >::value, void >::type | SampleVisible (arma::Mat< ElemType > &input, arma::Mat< ElemType > &output) |
This function samples the visible layer given the hidden layer using Bernoulli function. More... | |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, SpikeSlabRBM >::value, void >::type | SampleVisible (arma::Mat< ElemType > &input, arma::Mat< ElemType > &output) |
Sample Hidden function samples the slab outputs from the Normal distribution with mean given by: and variance: . More... | |
template < typename Archive > | |
void | serialize (Archive &ar, const uint32_t version) |
Serialize the model. More... | |
void | Shuffle () |
Shuffle the order of function visitation. More... | |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, SpikeSlabRBM >::value, void >::type | SlabMean (const DataType &visible, DataType &spike, DataType &slabMean) |
The function calculates the mean of Normal distribution of P(s|v, h), where the mean is given by: . More... | |
ElemType const & | SlabPenalty () const |
Get the regularizer associated with slab variables. More... | |
DataType const & | SpikeBias () const |
Get the regularizer associated with spike variables. More... | |
DataType & | SpikeBias () |
Modify the regularizer associated with spike variables. More... | |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, SpikeSlabRBM >::value, void >::type | SpikeMean (const InputType &visible, DataType &spikeMean) |
The function calculates the mean of the distribution P(h|v), where mean is given by: . More... | |
template<typename OptimizerType , typename... CallbackType> | |
double | Train (OptimizerType &optimizer, CallbackType &&... callbacks) |
Train the RBM on the given input data. More... | |
DataType const & | VisibleBias () const |
Return the visible bias of the network. More... | |
DataType & | VisibleBias () |
Modify the visible bias of the network. More... | |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, BinaryRBM >::value, void >::type | VisibleMean (InputType &input, DataType &output) |
The function calculates the mean for the visible layer. More... | |
template < typename Policy = PolicyType , typename InputType = DataType > | |
std::enable_if< std::is_same< Policy, SpikeSlabRBM >::value, void >::type | VisibleMean (InputType &input, DataType &output) |
The function calculates the mean of the Normal distribution of P(v|s, h). More... | |
DataType const & | VisiblePenalty () const |
Get the regularizer associated with visible variables. More... | |
DataType & | VisiblePenalty () |
Modify the regularizer associated with visible variables. More... | |
size_t const & | VisibleSize () const |
Get the visible size. More... | |
arma::Cube< ElemType > const & | Weight () const |
Get the weights of the network. More... | |
arma::Cube< ElemType > & | Weight () |
Modify the weights of the network. More... | |
The implementation of the RBM module.
A Restricted Boltzmann Machines (RBM) is a generative stochastic artificial neural network that can learn a probability distribution over its set of inputs. RBMs have found applications in dimensionality reduction, classification, collaborative filtering, feature learning and topic modelling. They can be trained in either supervised or unsupervised ways, depending on the task. They are a variant of Boltzmann machines, with the restriction that the neurons must form a bipartite graph.
InitializationRuleType | Rule used to initialize the network. |
DataType | The type of matrix to be used. |
PolicyType | The RBM variant to be used (BinaryRBM or SpikeSlabRBM). |
using NetworkType = RBM<InitializationRuleType, DataType, PolicyType> |
RBM | ( | arma::Mat< ElemType > | predictors, |
InitializationRuleType | initializeRule, | ||
const size_t | visibleSize, | ||
const size_t | hiddenSize, | ||
const size_t | batchSize = 1 , |
||
const size_t | numSteps = 1 , |
||
const size_t | negSteps = 1 , |
||
const size_t | poolSize = 2 , |
||
const ElemType | slabPenalty = 8 , |
||
const ElemType | radius = 1 , |
||
const bool | persistence = false |
||
) |
Initialize all the parameters of the network using initializeRule.
predictors | Training data to be used. |
initializeRule | InitializationRule object for initializing the network parameter. |
visibleSize | Number of visible neurons. |
hiddenSize | Number of hidden neurons. |
batchSize | Batch size to be used for training. |
numSteps | Number of Gibbs Sampling steps. |
negSteps | Number of negative samples to average negative gradient. |
poolSize | Number of hidden neurons to pool together. |
slabPenalty | Regulariser of slab variables. |
radius | Feasible regions for visible layer samples. |
persistence | Indicates whether to use Persistent CD or not. |
double Evaluate | ( | const arma::Mat< ElemType > & | parameters, |
const size_t | i, | ||
const size_t | batchSize | ||
) |
Evaluate the RBM network with the given parameters.
The function is needed for monitoring the progress of the network.
parameters | Matrix model parameters. |
i | Index of the data point. |
batchSize | Variable to store the present number of inputs. |
std::enable_if<std::is_same<Policy, BinaryRBM>::value, double>::type FreeEnergy | ( | const arma::Mat< ElemType > & | input | ) |
This function calculates the free energy of the BinaryRBM.
The free energy is given by: .
input | The visible neurons. |
std::enable_if<std::is_same<Policy, SpikeSlabRBM>::value, double>::type FreeEnergy | ( | const arma::Mat< ElemType > & | input | ) |
This function calculates the free energy of the SpikeSlabRBM.
The free energy is given by: -
input | The visible layer neurons. |
void Gibbs | ( | const arma::Mat< ElemType > & | input, |
arma::Mat< ElemType > & | output, | ||
const size_t | steps = SIZE_MAX |
||
) |
This function does the k-step Gibbs Sampling.
input | Input to the Gibbs function. |
output | Used for storing the negative sample. |
steps | Number of Gibbs Sampling steps taken. |
void Gradient | ( | const arma::Mat< ElemType > & | parameters, |
const size_t | i, | ||
arma::Mat< ElemType > & | gradient, | ||
const size_t | batchSize | ||
) |
Calculates the gradients for the RBM network.
parameters | The current parameters of the network. |
i | Index of the data point. |
gradient | Variable to store the present gradient. |
batchSize | Variable to store the present number of inputs. |
|
inline |
|
inline |
std::enable_if<std::is_same<Policy, BinaryRBM>::value, void>::type HiddenMean | ( | const InputType & | input, |
DataType & | output | ||
) |
The function calculates the mean for the hidden layer.
input | Visible neurons. |
output | Hidden neuron activations. |
std::enable_if<std::is_same<Policy, SpikeSlabRBM>::value, void>::type HiddenMean | ( | const InputType & | input, |
DataType & | output | ||
) |
The function calculates the mean of the Normal distribution of P(s|v, h).
The mean is given by: The variance is given by:
input | Visible layer neurons. |
output | Consists of both the spike samples and slab samples. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
std::enable_if<std::is_same<Policy, BinaryRBM>::value, void>::type Phase | ( | const InputType & | input, |
DataType & | gradient | ||
) |
std::enable_if<std::is_same<Policy, SpikeSlabRBM>::value, void>::type Phase | ( | const InputType & | input, |
DataType & | gradient | ||
) |
|
inline |
Get the pool size.
Definition at line 376 of file rbm.hpp.
References RBM< InitializationRuleType, DataType, PolicyType >::serialize().
std::enable_if<std::is_same<Policy, BinaryRBM>::value, void>::type Reset | ( | ) |
std::enable_if<std::is_same<Policy, SpikeSlabRBM>::value, void>::type Reset | ( | ) |
std::enable_if<std::is_same<Policy, BinaryRBM>::value, void>::type SampleHidden | ( | const arma::Mat< ElemType > & | input, |
arma::Mat< ElemType > & | output | ||
) |
This function samples the hidden layer given the visible layer using Bernoulli function.
input | Visible layer input. |
output | The sampled hidden layer. |
std::enable_if<std::is_same<Policy, SpikeSlabRBM>::value, void>::type SampleHidden | ( | const arma::Mat< ElemType > & | input, |
arma::Mat< ElemType > & | output | ||
) |
This function samples the slab outputs from the Normal distribution with mean given by: and variance: .
input | Consists of both visible and spike variables. |
output | Sampled slab neurons. |
std::enable_if<std::is_same<Policy, SpikeSlabRBM>::value, void>::type SampleSlab | ( | InputType & | slabMean, |
DataType & | slab | ||
) |
The function samples from the Normal distribution of P(s|v, h), where the mean is given by: and variance is given by: .
slabMean | Mean of the Normal distribution of the slab neurons. |
slab | Sampled slab variable from the Normal distribution. |
std::enable_if<std::is_same<Policy, SpikeSlabRBM>::value, void>::type SampleSpike | ( | InputType & | spikeMean, |
DataType & | spike | ||
) |
The function samples the spike function using Bernoulli distribution.
spikeMean | Indicates P(h|v). |
spike | Sampled binary spike variables. |
std::enable_if<std::is_same<Policy, BinaryRBM>::value, void>::type SampleVisible | ( | arma::Mat< ElemType > & | input, |
arma::Mat< ElemType > & | output | ||
) |
This function samples the visible layer given the hidden layer using Bernoulli function.
input | Hidden layer of the network. |
output | The sampled visible layer. |
std::enable_if<std::is_same<Policy, SpikeSlabRBM>::value, void>::type SampleVisible | ( | arma::Mat< ElemType > & | input, |
arma::Mat< ElemType > & | output | ||
) |
Sample Hidden function samples the slab outputs from the Normal distribution with mean given by: and variance: .
input | Hidden layer of the network. |
output | The sampled visible layer. |
void serialize | ( | Archive & | ar, |
const uint32_t | version | ||
) |
Serialize the model.
Referenced by RBM< InitializationRuleType, DataType, PolicyType >::PoolSize().
void Shuffle | ( | ) |
Shuffle the order of function visitation.
This may be called by the optimizer.
std::enable_if<std::is_same<Policy, SpikeSlabRBM>::value, void>::type SlabMean | ( | const DataType & | visible, |
DataType & | spike, | ||
DataType & | slabMean | ||
) |
The function calculates the mean of Normal distribution of P(s|v, h), where the mean is given by: .
visible | The visible layer neurons. |
spike | The spike variables from hidden layer. |
slabMean | The mean of the Normal distribution of slab neurons. |
|
inline |
|
inline |
|
inline |
std::enable_if<std::is_same<Policy, SpikeSlabRBM>::value, void>::type SpikeMean | ( | const InputType & | visible, |
DataType & | spikeMean | ||
) |
The function calculates the mean of the distribution P(h|v), where mean is given by: .
visible | The visible layer neurons. |
spikeMean | Indicates P(h|v). |
double Train | ( | OptimizerType & | optimizer, |
CallbackType &&... | callbacks | ||
) |
Train the RBM on the given input data.
This will use the existing model parameters as a starting point for the optimization. If this is not what you want, then you should access the parameters vector directly with Parameters() and modify it as desired.
OptimizerType | Type of optimizer to use to train the model. |
CallbackTypes | Types of Callback functions. |
optimizer | Optimizer type. |
callbacks | Callback Functions for ensmallen optimizer OptimizerType . See https://www.ensmallen.org/docs.html#callback-documentation. |
|
inline |
|
inline |
std::enable_if<std::is_same<Policy, BinaryRBM>::value, void>::type VisibleMean | ( | InputType & | input, |
DataType & | output | ||
) |
The function calculates the mean for the visible layer.
input | Hidden neurons from the hidden layer of the network. |
output | Visible neuron activations. |
std::enable_if<std::is_same<Policy, SpikeSlabRBM>::value, void>::type VisibleMean | ( | InputType & | input, |
DataType & | output | ||
) |
The function calculates the mean of the Normal distribution of P(v|s, h).
The mean is given by:
input | Consists of both the spike and slab variables. |
output | Mean of the of the Normal distribution. |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |