30 #ifndef SHARK_UNSUPERVISED_RBM_SAMPLING_TEMPEREDMARKOVCHAIN_H 31 #define SHARK_UNSUPERVISED_RBM_SAMPLING_TEMPEREDMARKOVCHAIN_H 37 #include "Impl/SampleTypes.h" 44 template<
class Operator>
47 typedef typename Operator::HiddenSample HiddenSample;
48 typedef typename Operator::VisibleSample VisibleSample;
58 typedef typename Operator::RBM
RBM;
70 SampleBatch m_temperedChains;
74 void metropolisSwap(reference low,
double betaLow, reference high,
double betaHigh){
76 double betaDiff = betaLow - betaHigh;
77 double energyDiff = low.energy - high.energy;
78 double baseRateDiff = inner_prod(low.visible.state,baseRate) - inner_prod(high.visible.state,baseRate);
79 double r = betaDiff * energyDiff + betaDiff*baseRateDiff;
81 double z =
random::uni(m_operator.rbm()->rng(),0,1);
82 if( r >= 0 || (z > 0 && std::log(z) < r) ){
102 std::size_t visibles=m_operator.rbm()->numberOfVN();
103 std::size_t hiddens=m_operator.rbm()->numberOfHN();
104 m_temperedChains =
SampleBatch(temperatures,visibles,hiddens);
105 m_betas.resize(temperatures);
114 for(std::size_t i = 0; i != temperatures; ++i){
115 double factor = temperatures - 1.0;
123 return m_betas.size();
138 double beta(std::size_t i)
const{
153 return m_temperedChains;
158 return m_temperedChains;
166 SHARK_RUNTIME_CHECK(m_temperedChains.size() != 0,
"You did not initialize the number of temperatures bevor initializing the chain!");
167 std::size_t visibles = m_operator.rbm()->numberOfVN();
168 RealMatrix sampleData(m_temperedChains.size(),visibles);
170 for(std::size_t i = 0; i != m_temperedChains.size(); ++i){
181 SHARK_RUNTIME_CHECK(m_temperedChains.size() != 0,
"You did not initialize the number of temperatures bevor initializing the chain!");
183 m_operator.createSample(m_temperedChains.hidden,m_temperedChains.visible,sampleData,m_betas);
185 m_temperedChains.energy = m_operator.calculateEnergy(
186 m_temperedChains.hidden,
187 m_temperedChains.visible
192 for(std::size_t i = 0; i != k; ++i){
194 m_operator.stepVH(m_temperedChains.hidden, m_temperedChains.visible,1,m_betas);
197 m_temperedChains.energy = m_operator.calculateEnergy(
198 m_temperedChains.hidden,
199 m_temperedChains.visible
203 std::size_t elems = m_temperedChains.size();
204 for(std::size_t i = 0; i < elems-1; i+=2){
206 reference(m_temperedChains,i),m_betas(i),
207 reference(m_temperedChains,i+1),m_betas(i+1)
211 for(std::size_t i = 1; i < elems-1; i+=2){
213 reference(m_temperedChains,i),m_betas(i),
214 reference(m_temperedChains,i+1),m_betas(i+1)
217 m_operator.rbm()->hiddenNeurons().sufficientStatistics(
218 m_temperedChains.hidden.input,m_temperedChains.hidden.statistics, m_betas