30 #ifndef SHARK_UNSUPERVISED_RBM_RBM_H 31 #define SHARK_UNSUPERVISED_RBM_RBM_H 35 #include <shark/Unsupervised/RBM/Impl/AverageEnergyGradient.h> 38 #include <boost/serialization/string.hpp> 42 template<
class VisibleLayerT,
class H
iddenLayerT,
class randomT>
58 RealMatrix m_weightMatrix;
61 HiddenType m_hiddenNeurons;
64 VisibleType m_visibleNeurons;
74 void evalForward(BatchInputType
const& state,BatchOutputType& output)
const{
76 typename HiddenType::StatisticsBatch statisticsBatch(batchSize,
numberOfHN());
81 hiddenNeurons().sufficientStatistics(inputBatch,statisticsBatch,blas::repeat(1.0,batchSize));
95 void evalBackward(BatchInputType
const& state,BatchOutputType& output)
const{
97 typename VisibleType::StatisticsBatch statisticsBatch(batchSize,
numberOfVN());
102 visibleNeurons().sufficientStatistics(inputBatch,statisticsBatch,blas::repeat(1.0,batchSize));
112 RBM(randomType&
rng):mpe_rng(&rng),m_forward(true),m_evalMean(true)
122 parameters += m_hiddenNeurons.numberOfParameters();
123 parameters += m_visibleNeurons.numberOfParameters();
129 return to_vector(m_weightMatrix)
130 | m_hiddenNeurons.parameterVector()
131 | m_visibleNeurons.parameterVector();
139 std::size_t endH = endW + m_hiddenNeurons.numberOfParameters();
140 std::size_t endV = endH + m_visibleNeurons.numberOfParameters();
141 noalias(to_vector(m_weightMatrix)) = subrange(newParameters,0,endW);
142 m_hiddenNeurons.setParameterVector(subrange(newParameters,endW,endH));
143 m_visibleNeurons.setParameterVector(subrange(newParameters,endH,endV));
151 m_weightMatrix.resize(hiddenNeurons,visibleNeurons);
152 m_weightMatrix.clear();
154 m_hiddenNeurons.resize(hiddenNeurons);
155 m_visibleNeurons.resize(visibleNeurons);
160 return m_hiddenNeurons;
164 return m_hiddenNeurons;
168 return m_visibleNeurons;
172 return m_visibleNeurons;
177 return m_weightMatrix;
181 return m_weightMatrix;
207 m_evalMean = evalMean;
227 return boost::shared_ptr<State>(
new EmptyState());
242 void eval(BatchInputType
const& patterns,BatchOutputType& outputs)
const{
244 evalForward(patterns,outputs);
247 evalBackward(patterns,outputs);
252 void eval(BatchInputType
const& patterns, BatchOutputType& outputs,
State& state)
const{
253 eval(patterns,outputs);
260 void inputHidden(RealMatrix& inputs, RealMatrix
const& visibleStates)
const{
261 SIZE_CHECK(visibleStates.size1() == inputs.size1());
262 SIZE_CHECK(inputs.size2() == m_hiddenNeurons.size());
263 SIZE_CHECK( visibleStates.size2() == m_visibleNeurons.size());
265 noalias(inputs) = prod(m_visibleNeurons.phi(visibleStates),trans(m_weightMatrix));
273 void inputVisible(RealMatrix& inputs, RealMatrix
const& hiddenStates)
const{
274 SIZE_CHECK(hiddenStates.size1() == inputs.size1());
275 SIZE_CHECK(inputs.size2() == m_visibleNeurons.size());
277 noalias(inputs) = prod(m_hiddenNeurons.phi(hiddenStates),m_weightMatrix);
285 return m_hiddenNeurons.size();
289 return m_visibleNeurons.size();
294 archive >> m_weightMatrix;
295 archive >> m_hiddenNeurons;
296 archive >> m_visibleNeurons;
302 std::stringstream stream(str);
308 archive << m_weightMatrix;
309 archive << m_hiddenNeurons;
310 archive << m_visibleNeurons;
312 std::stringstream stream;
314 std::string str = stream.str();