30 #ifndef SHARK_UNSUPERVISED_RBM_SINGLECHAINAPPROXIMATOR_H 31 #define SHARK_UNSUPERVISED_RBM_SINGLECHAINAPPROXIMATOR_H 34 #include "Impl/DataEvaluator.h" 43 template<
class MarkovChainType>
46 typedef typename MarkovChainType::RBM
RBM;
49 : mpe_rbm(rbm),m_chain(rbm),m_k(1)
50 ,m_samples(0),m_batchSize(500)
51 ,m_numBatches(0),m_regularizer(0){
58 m_chain.setBatchSize(1);
63 {
return "SingleChainApproximator"; }
89 MarkovChainType
const&
chain()
const{
95 m_chain.initializeChain(m_data);
99 return mpe_rbm->parameterVector();
103 return mpe_rbm->numberOfParameters();
107 m_regularizer = regularizer;
108 m_regularizationStrength = factor;
112 mpe_rbm->setParameterVector(parameter);
115 RealVector empiricalAverage = detail::evaluateData(m_data,*mpe_rbm,m_numBatches);
121 std::size_t samplesToDraw = m_samples > 0 ? m_samples: m_data.
numberOfElements();
123 std::size_t batches = samplesToDraw / m_batchSize;
124 if(samplesToDraw - batches*m_batchSize != 0){
133 for(std::size_t batch = 0; batch != batches; ++batch){
135 std::size_t currentBatchSize = std::min(samplesToDraw-batch*m_batchSize, m_batchSize);
136 typename MarkovChainType::SampleBatch gradientBatch(currentBatchSize, mpe_rbm->numberOfVN(),mpe_rbm->numberOfHN());
138 for(std::size_t i = 0; i != currentBatchSize; ++i){
143 modelAverage.addVH(gradientBatch.hidden, gradientBatch.visible);
146 derivative.resize(mpe_rbm->numberOfParameters());
147 noalias(derivative) = modelAverage.result() - empiricalAverage;
152 noalias(derivative) += m_regularizationStrength*regularizerDerivative;
155 return std::numeric_limits<double>::quiet_NaN();
160 mutable MarkovChainType m_chain;
164 unsigned int m_samples;
165 std::size_t m_batchSize;
166 std::size_t m_numBatches;
169 double m_regularizationStrength;