30 #ifndef SHARK_UNSUPERVISED_RBM_NEURONLAYERS_TRUNCATEDEXPONENTIALLAYER_H 31 #define SHARK_UNSUPERVISED_RBM_NEURONLAYERS_TRUNCATEDEXPONENTIALLAYER_H 43 template<
class VectorType>
44 struct TruncatedExponentialSufficientStatistics{
48 TruncatedExponentialSufficientStatistics(std::size_t numberOfNeurons)
49 :lambda(numberOfNeurons), expMinusLambda(numberOfNeurons){}
50 TruncatedExponentialSufficientStatistics(){}
58 template<
class VectorType>
59 struct Batch< detail::TruncatedExponentialSufficientStatistics<VectorType> >{
61 detail::TruncatedExponentialSufficientStatistics<VectorType>,
86 const RealVector&
bias()
const{
98 m_bias.resize(newSize);
103 return m_bias.size();
111 template<
class Input,
class BetaVector>
115 SIZE_CHECK(input.size1() == statistics.lambda.size1());
117 for(std::size_t i = 0; i != input.size1(); ++i){
120 noalias(statistics.expMinusLambda) = exp(-statistics.lambda);
139 template<
class Matrix,
class Rng>
140 void sample(StatisticsBatch
const& statistics, Matrix& state,
double alpha, Rng& rng)
const{
142 SIZE_CHECK(statistics.lambda.size1() == state.size1());
143 SIZE_CHECK(statistics.lambda.size2() == state.size2());
146 for(std::size_t i = 0; i != state.size1();++i){
147 for(std::size_t j = 0; j != state.size2();++j){
148 double integral = 1.0 - statistics.expMinusLambda(i,j);
150 state(i,j) = truncExp();
162 template<
class Matrix>
163 Matrix
const&
phi(Matrix
const& state)
const{
176 std::size_t batchSize=statistics.lambda.size1();
179 for(std::size_t i = 0; i != batchSize;++i){
180 for(std::size_t j = 0; j !=
size();++j){
181 double expML=statistics.expMinusLambda(i,j);
182 mean(i,j) = 1.0/statistics.lambda(i,j)-expML/(1.0 - expML);
189 RealMatrix
mean(StatisticsBatch
const& statistics)
const{
190 return expectedPhiValue(statistics);
198 template<
class Matrix,
class BetaVector>
205 RealVector energies = beta *
prod(state,m_bias);
218 template<
class Input>
222 for(std::size_t i = 0; i !=
size(); ++i){
223 double a = (inputs(i)+m_bias(i))*beta;
230 lnResult += a+std::log(1.0 - std::exp(-a));
232 lnResult += std::log( 1.0 - std::exp(a));
233 lnResult -= std::log(std::abs(a));
245 template<
class Vector,
class SampleBatch>
248 sum_rows(samples.statistics.probability,derivative);
257 template<
class Vector,
class SampleBatch>
270 m_bias = newParameters;