shark::GibbsOperator< RBMType > Class Template Reference

Implements Block Gibbs Sampling related transition operators for various temperatures. More...

#include <shark/Unsupervised/RBM/Sampling/GibbsOperator.h>

Public Types

typedef RBMType RBM
 
typedef detail::GibbsSample< typename RBMType::HiddenType::SufficientStatistics > HiddenSample
 the type of a concrete sample. More...
 
typedef detail::GibbsSample< typename RBMType::VisibleType::SufficientStatistics > VisibleSample
 the type of a concrete sample. More...
 
typedef Batch< HiddenSample >::type HiddenSampleBatch
 Represents the state of a batch of hidden samples and additional information required by the gradient. More...
 
typedef Batch< VisibleSample >::type VisibleSampleBatch
 Represents the state of the visible units and additional information required by the gradient. More...
 

Public Member Functions

 GibbsOperator (RBM *rbm, double alphaVisible=0, double alphaHidden=0)
 Constructs the Operator using an allready defined Distribution to sample from. More...
 
RBMrbm () const
 Returns the internal RBM. More...
 
template<class BetaVector >
void precomputeHidden (HiddenSampleBatch &hiddenBatch, VisibleSampleBatch &visibleBatch, BetaVector const &beta) const
 Calculates internal data needed for sampling the hidden units as well as requested information for the gradient. More...
 
template<class BetaVector >
void precomputeVisible (HiddenSampleBatch &hiddenBatch, VisibleSampleBatch &visibleBatch, BetaVector const &beta) const
 calculates internal data needed for sampling the visible units as well as requested information for the gradient More...
 
void sampleHidden (HiddenSampleBatch &sampleBatch) const
 Samples a new batch of states of the hidden units using their precomputed statistics. More...
 
void sampleVisible (VisibleSampleBatch &sampleBatch) const
 Samples a new batch of states of the visible units using their precomputed statistics. More...
 
template<class BetaVector >
void stepVH (HiddenSampleBatch &hiddenBatch, VisibleSampleBatch &visibleBatch, std::size_t numberOfSteps, BetaVector const &beta)
 Applies the Gibbs operator a number of times to a given sample. More...
 
template<class States , class BetaVector >
void createSample (HiddenSampleBatch &hiddenBatch, VisibleSampleBatch &visibleBatch, States const &states, BetaVector const &beta) const
 Creates hidden/visible sample pairs from the states of the visible neurons, i.e. sets the visible units to the given states and samples hidden states based on the states of the visible units. This can directly be used to calculate the gradient. More...
 
template<class States >
void createSample (HiddenSampleBatch &hiddenBatch, VisibleSampleBatch &visibleBatch, States const &states) const
 Creates hidden/visible sample pairs from the states of the visible neurons, i.e. sets the visible units to the given states and samples hidden states based on the states of the visible units. This can directly be used to calculate the gradient. More...
 
RealVector calculateEnergy (HiddenSampleBatch const &hiddenBatch, VisibleSampleBatch const &visibleBatch) const
 Calculates the Energy of a sample of the visible and hidden neurons created by this chain. More...
 
void setAlpha (double newAlphaVisible, double newAlphaHidden)
 

Detailed Description

template<class RBMType>
class shark::GibbsOperator< RBMType >

Implements Block Gibbs Sampling related transition operators for various temperatures.

The operator generates transitions from the current state of the neurons of an RBM to a new one and thus can be used to produce a Markov chain. The Gibbs Operator works by computing the conditional distribution of the hidden given the visible p(h|v) (or vice versa) and than samples the new hidden (or visible) state from it.

As an interesting twist, this operator can also be used to implement Flip-The-State sampling using two values alpha_visible and alpha_hidden both being between 0 and 1 (inclusively). for alpha_visible=alpha_hidden=0, pure gibbs sampling is performed. if for one of the layers, the value is not 0 a mixture of gibbs and flip-the-state sampling is performed. 1 equals to pure flip-the state sampling. The trick of this sampler is that it takes the previous state into account while sampling. If the current state has a low probability, the sampler jumps deterministically in another state with higher probability. This is counterbalanced by having a higher chance to jump away from this state.

Definition at line 52 of file GibbsOperator.h.

Member Typedef Documentation

◆ HiddenSample

template<class RBMType >
typedef detail::GibbsSample< typename RBMType::HiddenType::SufficientStatistics > shark::GibbsOperator< RBMType >::HiddenSample

the type of a concrete sample.

The operator holds a 'sample' of the visible and hidden neurons. Such a sample does not only contain the states of the neurons but all other information needed to approximate the gradient

Definition at line 63 of file GibbsOperator.h.

◆ HiddenSampleBatch

template<class RBMType >
typedef Batch<HiddenSample>::type shark::GibbsOperator< RBMType >::HiddenSampleBatch

Represents the state of a batch of hidden samples and additional information required by the gradient.

Aside from the hidden state, this structure can also hold the actual values of the input, the phi-function and the sufficient statistics

Definition at line 73 of file GibbsOperator.h.

◆ RBM

template<class RBMType >
typedef RBMType shark::GibbsOperator< RBMType >::RBM

Definition at line 54 of file GibbsOperator.h.

◆ VisibleSample

template<class RBMType >
typedef detail::GibbsSample< typename RBMType::VisibleType::SufficientStatistics > shark::GibbsOperator< RBMType >::VisibleSample

the type of a concrete sample.

Definition at line 67 of file GibbsOperator.h.

◆ VisibleSampleBatch

template<class RBMType >
typedef Batch<VisibleSample>::type shark::GibbsOperator< RBMType >::VisibleSampleBatch

Represents the state of the visible units and additional information required by the gradient.

Aside from the visible state, this structure can also hold the actual values of the hidden input, the phi-function and the sufficient statistics

Definition at line 79 of file GibbsOperator.h.

Constructor & Destructor Documentation

◆ GibbsOperator()

template<class RBMType >
shark::GibbsOperator< RBMType >::GibbsOperator ( RBM rbm,
double  alphaVisible = 0,
double  alphaHidden = 0 
)
inline

Constructs the Operator using an allready defined Distribution to sample from.

Definition at line 82 of file GibbsOperator.h.

References shark::GibbsOperator< RBMType >::setAlpha().

Member Function Documentation

◆ calculateEnergy()

template<class RBMType >
RealVector shark::GibbsOperator< RBMType >::calculateEnergy ( HiddenSampleBatch const &  hiddenBatch,
VisibleSampleBatch const &  visibleBatch 
) const
inline

Calculates the Energy of a sample of the visible and hidden neurons created by this chain.

Parameters
hiddenBatchthe batch of samples of the hidden neurons
visibleBatchthe batch of samples of the visible neurons (holding also the precomputed input of the visibles)
Returns
the value of the energy function

Definition at line 182 of file GibbsOperator.h.

◆ createSample() [1/2]

template<class RBMType >
template<class States , class BetaVector >
void shark::GibbsOperator< RBMType >::createSample ( HiddenSampleBatch hiddenBatch,
VisibleSampleBatch visibleBatch,
States const &  states,
BetaVector const &  beta 
) const
inline

Creates hidden/visible sample pairs from the states of the visible neurons, i.e. sets the visible units to the given states and samples hidden states based on the states of the visible units. This can directly be used to calculate the gradient.

Parameters
hiddenBatchthe batch of hidden samples to be created
visibleBatchthe batch of visible samples to be created
statesthe states of the visible neurons in the sample
betathe vector of inverse temperatures

Definition at line 157 of file GibbsOperator.h.

References shark::batchSize(), shark::GibbsOperator< RBMType >::precomputeHidden(), shark::GibbsOperator< RBMType >::sampleHidden(), and SIZE_CHECK.

Referenced by shark::GibbsOperator< RBMType >::createSample(), and shark::ExactGradient< RBMType >::evalDerivative().

◆ createSample() [2/2]

template<class RBMType >
template<class States >
void shark::GibbsOperator< RBMType >::createSample ( HiddenSampleBatch hiddenBatch,
VisibleSampleBatch visibleBatch,
States const &  states 
) const
inline

Creates hidden/visible sample pairs from the states of the visible neurons, i.e. sets the visible units to the given states and samples hidden states based on the states of the visible units. This can directly be used to calculate the gradient.

Parameters
hiddenBatchthe batch of hidden samples to be created
visibleBatchthe batch of visible samples to be created
statesthe states of the visible neurons in the sample

Definition at line 173 of file GibbsOperator.h.

References shark::GibbsOperator< RBMType >::createSample().

◆ precomputeHidden()

template<class RBMType >
template<class BetaVector >
void shark::GibbsOperator< RBMType >::precomputeHidden ( HiddenSampleBatch hiddenBatch,
VisibleSampleBatch visibleBatch,
BetaVector const &  beta 
) const
inline

Calculates internal data needed for sampling the hidden units as well as requested information for the gradient.

This function calculates the conditional probability distribution p(h|v) with inverse temperature beta for the whole batch of samples Be aware that a change of temperature may occur between sampleVisible and precomputeHidden.

Parameters
hiddenBatchthe batch of hidden samples to be created
visibleBatchthe batch of visible samples to be created
betathe vector of inverse temperatures

Definition at line 101 of file GibbsOperator.h.

References SIZE_CHECK.

Referenced by shark::GibbsOperator< RBMType >::createSample(), and shark::GibbsOperator< RBMType >::stepVH().

◆ precomputeVisible()

template<class RBMType >
template<class BetaVector >
void shark::GibbsOperator< RBMType >::precomputeVisible ( HiddenSampleBatch hiddenBatch,
VisibleSampleBatch visibleBatch,
BetaVector const &  beta 
) const
inline

calculates internal data needed for sampling the visible units as well as requested information for the gradient

This function calculates the conditional probability distribution p(v|h) with inverse temperature beta for a whole batch of inputs. Be aware that a change of temperature may occur between sampleHidden and precomputeVisible.

Definition at line 114 of file GibbsOperator.h.

References SIZE_CHECK.

Referenced by shark::GibbsOperator< RBMType >::stepVH().

◆ rbm()

template<class RBMType >
RBM* shark::GibbsOperator< RBMType >::rbm ( ) const
inline

Returns the internal RBM.

Definition at line 88 of file GibbsOperator.h.

◆ sampleHidden()

template<class RBMType >
void shark::GibbsOperator< RBMType >::sampleHidden ( HiddenSampleBatch sampleBatch) const
inline

Samples a new batch of states of the hidden units using their precomputed statistics.

Definition at line 123 of file GibbsOperator.h.

Referenced by shark::GibbsOperator< RBMType >::createSample(), and shark::GibbsOperator< RBMType >::stepVH().

◆ sampleVisible()

template<class RBMType >
void shark::GibbsOperator< RBMType >::sampleVisible ( VisibleSampleBatch sampleBatch) const
inline

Samples a new batch of states of the visible units using their precomputed statistics.

Definition at line 130 of file GibbsOperator.h.

Referenced by shark::GibbsOperator< RBMType >::stepVH().

◆ setAlpha()

template<class RBMType >
void shark::GibbsOperator< RBMType >::setAlpha ( double  newAlphaVisible,
double  newAlphaHidden 
)
inline

Definition at line 190 of file GibbsOperator.h.

References SHARK_RUNTIME_CHECK.

Referenced by shark::GibbsOperator< RBMType >::GibbsOperator().

◆ stepVH()

template<class RBMType >
template<class BetaVector >
void shark::GibbsOperator< RBMType >::stepVH ( HiddenSampleBatch hiddenBatch,
VisibleSampleBatch visibleBatch,
std::size_t  numberOfSteps,
BetaVector const &  beta 
)
inline

Applies the Gibbs operator a number of times to a given sample.

Performs one complete step for a sample by sampling first the hidden, than the visible and computing the probability of a hidden given the visible unit That is, Given a State (v,h), computes p(v|h),draws v and then computes p(h|v) and draws h . this is repeated several times

Definition at line 140 of file GibbsOperator.h.

References shark::GibbsOperator< RBMType >::precomputeHidden(), shark::GibbsOperator< RBMType >::precomputeVisible(), shark::GibbsOperator< RBMType >::sampleHidden(), and shark::GibbsOperator< RBMType >::sampleVisible().


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