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>
114 SIZE_CHECK(statistics.lambda.size2() == size());
115 SIZE_CHECK(input.size1() == statistics.lambda.size1());
117 for(std::size_t i = 0; i != input.size1(); ++i){
118 noalias(row(statistics.lambda,i)) = -(row(input,i)+m_bias)*
beta(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{
141 SIZE_CHECK(statistics.lambda.size2() == size());
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 state(i,j) =
random::truncExp(rng,statistics.lambda(i,j),1.0,1.0 - statistics.expMinusLambda(i,j));
160 template<
class Matrix>
161 Matrix
const&
phi(Matrix
const& state)
const{
172 SIZE_CHECK(statistics.lambda.size2() == size());
173 SIZE_CHECK(statistics.expMinusLambda.size2() == size());
174 std::size_t
batchSize=statistics.lambda.size1();
175 RealMatrix
mean(batchSize,size());
177 for(std::size_t i = 0; i !=
batchSize;++i){
178 for(std::size_t j = 0; j != size();++j){
179 double expML=statistics.expMinusLambda(i,j);
180 mean(i,j) = 1.0/statistics.lambda(i,j)-expML/(1.0 - expML);
187 RealMatrix
mean(StatisticsBatch
const& statistics)
const{
188 return expectedPhiValue(statistics);
196 template<
class Matrix,
class BetaVector>
203 RealVector energies = beta * prod(state,m_bias);
216 template<
class Input>
220 for(std::size_t i = 0; i != size(); ++i){
221 double a = (inputs(i)+m_bias(i))*beta;
228 lnResult += a+std::log(1.0 - std::exp(-a));
230 lnResult += std::log( 1.0 - std::exp(a));
231 lnResult -= std::log(std::abs(a));
243 template<
class Vector,
class SampleBatch>
246 sum_rows(samples.statistics.probability,derivative);
255 template<
class Vector,
class SampleBatch>
258 sum_rows(samples.state,derivative);
268 m_bias = newParameters;