30 #ifndef SHARK_UNSUPERVISED_RBM_NEURONLAYERS_BINARYLAYER_H 31 #define SHARK_UNSUPERVISED_RBM_NEURONLAYERS_BINARYLAYER_H 51 RealVector m_baseRate;
62 const RealVector&
bias()
const{
92 m_bias.resize(newSize);
93 m_baseRate.resize(newSize);
108 template<
class Input,
class BetaVector>
112 SIZE_CHECK(input.size1() == statistics.size1());
114 for(std::size_t i = 0; i != input.size1(); ++i){
115 noalias(row(statistics,i)) =
sigmoid((row(input,i)+m_bias)*
beta(i)+(1.0-
beta(i))*m_baseRate);
130 template<
class Matrix,
class Rng>
131 void sample(StatisticsBatch
const& statistics, Matrix& state,
double alpha, Rng& rng)
const{
133 SIZE_CHECK(statistics.size1() == state.size1());
134 SIZE_CHECK(statistics.size2() == state.size2());
138 for(std::size_t s = 0; s != state.size1();++s){
139 for(std::size_t i = 0; i != state.size2();++i){
145 for(
size_t s = 0; s != state.size1(); ++s){
146 for (
size_t i = 0; i != state.size2(); i++) {
147 double prob = statistics(s,i);
148 if (state(s,i) == 0) {
150 prob = (1. - alpha) * prob + alpha * prob / (1. - prob);
152 prob = (1. - alpha) * prob + alpha;
156 prob = (1. - alpha) * prob + alpha * (1. - (1. - prob) / prob);
158 prob = (1. - alpha) * prob;
174 template<
class Matrix>
175 RealVector
logProbability(StatisticsBatch
const& statistics, Matrix
const& state)
const{
177 SIZE_CHECK(statistics.size1() == state.size1());
178 SIZE_CHECK(statistics.size2() == state.size2());
180 RealVector logProbabilities(state.size1(),1.0);
181 for(std::size_t s = 0; s != state.size1();++s){
182 for(std::size_t i = 0; i != state.size2();++i){
183 logProbabilities(s) += (state(s,i) > 0.0)? std::log(statistics(s,i)) : std::log(1-statistics(s,i));
186 return logProbabilities;
195 template<
class Matrix>
196 Matrix
const&
phi(Matrix
const& state)
const{
213 RealMatrix
const&
mean(StatisticsBatch
const& statistics)
const{
223 template<
class Matrix,
class BetaVector>
229 RealVector energies = prod(state,m_bias);
230 RealVector baseRateEnergies = prod(state,m_baseRate);
231 noalias(energies) = beta*energies +(1-
beta)*baseRateEnergies;
249 template<
class Input>
252 long double logFactorization = 0;
253 for(std::size_t i = 0; i != inputs.size(); ++i){
254 double arg = (inputs(i)+m_bias(i))*beta+(1-beta)*m_baseRate(i);
258 return logFactorization;
268 template<
class Vector,
class SampleBatch>
271 sum_rows(samples.statistics,derivative);
281 template<
class Vector,
class SampleBatch,
class WeightVector>
284 noalias(derivative) += prod(weights,samples.statistics);
293 template<
class Vector,
class SampleBatch>
296 sum_rows(samples.state,derivative);
305 template<
class Vector,
class SampleBatch,
class WeightVector>
306 void parameterDerivative(Vector& derivative, SampleBatch
const& samples, WeightVector
const& weights)
const{
308 noalias(derivative) += prod(weights,samples.state);
318 m_bias = newParameters;
331 m_baseRate = RealVector(m_bias.size(),0);