30 #ifndef SHARK_UNSUPERVISED_RBM_GRADIENTAPPROXIMATIONS_MULTICHAINAPPROXIMATOR_H 31 #define SHARK_UNSUPERVISED_RBM_GRADIENTAPPROXIMATIONS_MULTICHAINAPPROXIMATOR_H 34 #include "Impl/DataEvaluator.h" 43 template<
class MarkovChainType>
46 typedef typename MarkovChainType::RBM
RBM;
49 : mpe_rbm(rbm),m_chainOperator(rbm),m_k(1),m_samples(0),m_numBatches(0),m_regularizer(0){
60 {
return "MultiChainApproximator"; }
70 if(!MarkovChainType::computesBatch)
75 return m_chainOperator;
77 MarkovChainType
const&
chain()
const{
78 return m_chainOperator;
107 std::size_t batches = m_samples / m_batchSize;
108 if(m_samples - batches*m_batchSize != 0){
111 m_chains.resize(batches);
114 for(std::size_t i = 0; i != batches;++i){
115 swap(m_chains[i],m_chainOperator.samples());
116 std::size_t currentBatchSize = std::min(m_samples-i*m_batchSize, m_batchSize);
117 m_chainOperator.setBatchSize(currentBatchSize);
118 m_chainOperator.initializeChain(m_data);
119 swap(m_chains[i],m_chainOperator.samples());
124 return mpe_rbm->parameterVector();
128 return mpe_rbm->numberOfParameters();
132 m_regularizer = regularizer;
133 m_regularizationStrength = factor;
137 mpe_rbm->setParameterVector(parameter);
140 RealVector empiricalAverage = detail::evaluateData(m_data,*mpe_rbm,m_numBatches);
144 for(std::size_t i = 0; i != m_chains.size();++i){
145 swap(m_chains[i],m_chainOperator.samples());
146 m_chainOperator.step(m_k);
147 modelAverage.addVH(m_chainOperator.samples().hidden, m_chainOperator.samples().visible);
148 swap(m_chains[i],m_chainOperator.samples());
151 derivative.resize(mpe_rbm->numberOfParameters());
152 noalias(derivative) = modelAverage.result() - empiricalAverage;
157 noalias(derivative) += m_regularizationStrength*regularizerDerivative;
160 return std::numeric_limits<double>::quiet_NaN();
164 mutable MarkovChainType m_chainOperator;
165 mutable std::vector<typename MarkovChainType::SampleBatch> m_chains;
169 std::size_t m_samples;
170 std::size_t m_batchSize;
171 std::size_t m_numBatches;
174 double m_regularizationStrength;