Rbm Class Reference

#include <nnet-rbm.h>

Inheritance diagram for Rbm:
Collaboration diagram for Rbm:

Public Member Functions

 Rbm (int32 dim_in, int32 dim_out)
 
 ~Rbm ()
 
ComponentCopy () const
 Copy component (deep copy),. More...
 
ComponentType GetType () const
 Get Type Identification of the component,. More...
 
void InitData (std::istream &is)
 Virtual interface for initialization and I/O,. More...
 
void ReadData (std::istream &is, bool binary)
 Reads the component content. More...
 
void WriteData (std::ostream &os, bool binary) const
 Writes the component content. More...
 
void PropagateFnc (const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out)
 Abstract interface for propagation/backpropagation. More...
 
void Reconstruct (const CuMatrixBase< BaseFloat > &hid_state, CuMatrix< BaseFloat > *vis_probs)
 
void RbmUpdate (const CuMatrixBase< BaseFloat > &pos_vis, const CuMatrixBase< BaseFloat > &pos_hid, const CuMatrixBase< BaseFloat > &neg_vis, const CuMatrixBase< BaseFloat > &neg_hid)
 
RbmNodeType VisType () const
 
RbmNodeType HidType () const
 
void WriteAsNnet (std::ostream &os, bool binary) const
 
- Public Member Functions inherited from RbmBase
 RbmBase (int32 dim_in, int32 dim_out)
 
void SetRbmTrainOptions (const RbmTrainOptions &opts)
 Set training hyper-parameters to the network and its UpdatableComponent(s) More...
 
const RbmTrainOptionsGetRbmTrainOptions () const
 Get training hyper-parameters from the network. More...
 
- Public Member Functions inherited from Component
 Component (int32 input_dim, int32 output_dim)
 Generic interface of a component,. More...
 
virtual ~Component ()
 
virtual bool IsUpdatable () const
 Check if componeny has 'Updatable' interface (trainable components),. More...
 
virtual bool IsMultistream () const
 Check if component has 'Recurrent' interface (trainable and recurrent),. More...
 
int32 InputDim () const
 Get the dimension of the input,. More...
 
int32 OutputDim () const
 Get the dimension of the output,. More...
 
void Propagate (const CuMatrixBase< BaseFloat > &in, CuMatrix< BaseFloat > *out)
 Perform forward-pass propagation 'in' -> 'out',. More...
 
void Backpropagate (const CuMatrixBase< BaseFloat > &in, const CuMatrixBase< BaseFloat > &out, const CuMatrixBase< BaseFloat > &out_diff, CuMatrix< BaseFloat > *in_diff)
 Perform backward-pass propagation 'out_diff' -> 'in_diff'. More...
 
void Write (std::ostream &os, bool binary) const
 Write the component to a stream,. More...
 
virtual std::string Info () const
 Print some additional info (after <ComponentName> and the dims),. More...
 
virtual std::string InfoGradient () const
 Print some additional info about gradient (after <...> and dims),. More...
 

Protected Attributes

CuMatrix< BaseFloatvis_hid_
 Matrix with neuron weights. More...
 
CuVector< BaseFloatvis_bias_
 Vector with biases. More...
 
CuVector< BaseFloathid_bias_
 Vector with biases. More...
 
CuMatrix< BaseFloatvis_hid_corr_
 Matrix for linearity updates. More...
 
CuVector< BaseFloatvis_bias_corr_
 Vector for bias updates. More...
 
CuVector< BaseFloathid_bias_corr_
 Vector for bias updates. More...
 
RbmNodeType vis_type_
 
RbmNodeType hid_type_
 
- Protected Attributes inherited from RbmBase
RbmTrainOptions rbm_opts_
 
- Protected Attributes inherited from Component
int32 input_dim_
 Data members,. More...
 
int32 output_dim_
 Dimension of the output of the Component,. More...
 

Additional Inherited Members

- Public Types inherited from RbmBase
enum  RbmNodeType { Bernoulli, Gaussian }
 
- Public Types inherited from Component
enum  ComponentType {
  kUnknown = 0x0, kUpdatableComponent = 0x0100, kAffineTransform, kLinearTransform,
  kConvolutionalComponent, kLstmProjected, kBlstmProjected, kRecurrentComponent,
  kActivationFunction = 0x0200, kSoftmax, kHiddenSoftmax, kBlockSoftmax,
  kSigmoid, kTanh, kParametricRelu, kDropout,
  kLengthNormComponent, kTranform = 0x0400, kRbm, kSplice,
  kCopy, kTranspose, kBlockLinearity, kAddShift,
  kRescale, kKlHmm = 0x0800, kSentenceAveragingComponent, kSimpleSentenceAveragingComponent,
  kAveragePoolingComponent, kMaxPoolingComponent, kFramePoolingComponent, kParallelComponent,
  kMultiBasisComponent
}
 Component type identification mechanism,. More...
 
- Static Public Member Functions inherited from Component
static const char * TypeToMarker (ComponentType t)
 Converts component type to marker,. More...
 
static ComponentType MarkerToType (const std::string &s)
 Converts marker to component type (case insensitive),. More...
 
static ComponentInit (const std::string &conf_line)
 Initialize component from a line in config file,. More...
 
static ComponentRead (std::istream &is, bool binary)
 Read the component from a stream (static method),. More...
 
- Static Public Attributes inherited from Component
static const struct key_value kMarkerMap []
 The table with pairs of Component types and markers (defined in nnet-component.cc),. More...
 

Detailed Description

Definition at line 96 of file nnet-rbm.h.

Constructor & Destructor Documentation

◆ Rbm()

Rbm ( int32  dim_in,
int32  dim_out 
)
inline

Definition at line 98 of file nnet-rbm.h.

98  :
99  RbmBase(dim_in, dim_out)
100  { }
RbmBase(int32 dim_in, int32 dim_out)
Definition: nnet-rbm.h:42

◆ ~Rbm()

~Rbm ( )
inline

Definition at line 102 of file nnet-rbm.h.

103  { }

Member Function Documentation

◆ Copy()

Component* Copy ( ) const
inlinevirtual

Copy component (deep copy),.

Implements Component.

Definition at line 105 of file nnet-rbm.h.

105  {
106  return new Rbm(*this);
107  }
Rbm(int32 dim_in, int32 dim_out)
Definition: nnet-rbm.h:98

◆ GetType()

ComponentType GetType ( ) const
inlinevirtual

Get Type Identification of the component,.

Implements Component.

Definition at line 109 of file nnet-rbm.h.

References Component::kRbm.

109  {
110  return kRbm;
111  }

◆ HidType()

RbmNodeType HidType ( ) const
inlinevirtual

Implements RbmBase.

Definition at line 393 of file nnet-rbm.h.

393  {
394  return hid_type_;
395  }
RbmNodeType hid_type_
Definition: nnet-rbm.h:425

◆ InitData()

void InitData ( std::istream &  is)
inlinevirtual

Virtual interface for initialization and I/O,.

Initialize internal data of a component

Reimplemented from Component.

Definition at line 113 of file nnet-rbm.h.

References RbmBase::Bernoulli, rnnlm::d, RbmBase::Gaussian, Nnet::GetComponent(), Nnet::InputDim(), Component::InputDim(), KALDI_ASSERT, KALDI_ERR, KALDI_LOG, kaldi::Log(), Component::OutputDim(), kaldi::nnet1::RandGauss(), kaldi::nnet1::RandUniform(), Nnet::Read(), kaldi::ReadBasicType(), and kaldi::ReadToken().

113  {
114  // define options,
115  std::string vis_type;
116  std::string hid_type;
117  float vis_bias_mean = 0.0, vis_bias_range = 0.0,
118  hid_bias_mean = 0.0, hid_bias_range = 0.0,
119  param_stddev = 0.1;
120  std::string vis_bias_cmvn_file; // initialize biases to logit(p_active)
121  // parse config,
122  std::string token;
123  while (is >> std::ws, !is.eof()) {
124  ReadToken(is, false, &token);
125  if (token == "<VisibleType>") ReadToken(is, false, &vis_type);
126  else if (token == "<HiddenType>") ReadToken(is, false, &hid_type);
127  else if (token == "<VisibleBiasMean>") ReadBasicType(is, false, &vis_bias_mean);
128  else if (token == "<VisibleBiasRange>") ReadBasicType(is, false, &vis_bias_range);
129  else if (token == "<HiddenBiasMean>") ReadBasicType(is, false, &hid_bias_mean);
130  else if (token == "<HiddenBiasRange>") ReadBasicType(is, false, &hid_bias_range);
131  else if (token == "<ParamStddev>") ReadBasicType(is, false, &param_stddev);
132  else if (token == "<VisibleBiasCmvnFilename>") ReadToken(is, false, &vis_bias_cmvn_file);
133  else KALDI_ERR << "Unknown token " << token << " Typo in config?";
134  }
135 
136  // Translate the 'node' types,
137  if (vis_type == "bern" || vis_type == "Bernoulli") vis_type_ = RbmBase::Bernoulli;
138  else if (vis_type == "gauss" || vis_type == "Gaussian") vis_type_ = RbmBase::Gaussian;
139  else KALDI_ERR << "Wrong <VisibleType>" << vis_type;
140  //
141  if (hid_type == "bern" || hid_type == "Bernoulli") hid_type_ = RbmBase::Bernoulli;
142  else if (hid_type == "gauss" || hid_type == "Gaussian") hid_type_ = RbmBase::Gaussian;
143  else KALDI_ERR << "Wrong <HiddenType>" << hid_type;
144 
145  //
146  // Initialize trainable parameters,
147  //
148  // visible-hidden connections,
149  vis_hid_.Resize(OutputDim(), InputDim());
150  RandGauss(0.0, param_stddev, &vis_hid_);
151  // hidden-bias,
152  hid_bias_.Resize(OutputDim());
153  RandUniform(hid_bias_mean, hid_bias_range, &hid_bias_);
154  // visible-bias,
155  if (vis_bias_cmvn_file == "") {
156  vis_bias_.Resize(InputDim());
157  RandUniform(vis_bias_mean, vis_bias_range, &vis_bias_);
158  } else {
159  KALDI_LOG << "Initializing from <VisibleBiasCmvnFilename> "
160  << vis_bias_cmvn_file;
161  // Reading Nnet with 'global-cmvn' components,
162  Nnet cmvn;
163  cmvn.Read(vis_bias_cmvn_file);
164  KALDI_ASSERT(InputDim() == cmvn.InputDim());
165  // The parameters from <AddShift> correspond to 'negative' mean values,
166  Vector<BaseFloat> p(cmvn.InputDim());
167  dynamic_cast<AddShift&>(cmvn.GetComponent(0)).GetParams(&p);
168  p.Scale(-1.0); // 'un-do' negation of mean values,
169  p.ApplyFloor(0.0001);
170  p.ApplyCeiling(0.9999);
171  // Getting the logit,
172  Vector<BaseFloat> logit_p(p.Dim());
173  for (int32 d = 0; d < p.Dim(); d++) {
174  logit_p(d) = Log(p(d)) - Log(1.0 - p(d));
175  }
176  vis_bias_ = logit_p;
177  KALDI_ASSERT(vis_bias_.Dim() == InputDim());
178  }
179  }
CuVector< BaseFloat > hid_bias_
Vector with biases.
Definition: nnet-rbm.h:418
RbmNodeType hid_type_
Definition: nnet-rbm.h:425
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
RbmNodeType vis_type_
Definition: nnet-rbm.h:424
CuMatrix< BaseFloat > vis_hid_
Matrix with neuron weights.
Definition: nnet-rbm.h:416
void RandUniform(BaseFloat mu, BaseFloat range, CuMatrixBase< Real > *mat, struct RandomState *state=NULL)
Fill CuMatrix with random numbers (Uniform distribution): mu = the mean value, range = the &#39;width&#39; of...
Definition: nnet-utils.h:188
kaldi::int32 int32
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
Definition: io-funcs.cc:154
double Log(double x)
Definition: kaldi-math.h:100
int32 InputDim() const
Get the dimension of the input,.
#define KALDI_ERR
Definition: kaldi-error.h:147
void RandGauss(BaseFloat mu, BaseFloat sigma, CuMatrixBase< Real > *mat, struct RandomState *state=NULL)
Fill CuMatrix with random numbers (Gaussian distribution): mu = the mean value, sigma = standard devi...
Definition: nnet-utils.h:164
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
CuVector< BaseFloat > vis_bias_
Vector with biases.
Definition: nnet-rbm.h:417
int32 OutputDim() const
Get the dimension of the output,.
#define KALDI_LOG
Definition: kaldi-error.h:153

◆ PropagateFnc()

void PropagateFnc ( const CuMatrixBase< BaseFloat > &  in,
CuMatrixBase< BaseFloat > *  out 
)
inlinevirtual

Abstract interface for propagation/backpropagation.

Forward pass transformation (to be implemented by descending class...)

Implements Component.

Definition at line 226 of file nnet-rbm.h.

References CuMatrixBase< Real >::AddMatMat(), CuMatrixBase< Real >::AddVecToRows(), RbmBase::Bernoulli, kaldi::kNoTrans, kaldi::kTrans, and CuMatrixBase< Real >::Sigmoid().

227  {
228  // pre-fill with bias
229  out->AddVecToRows(1.0, hid_bias_, 0.0);
230  // multiply by weights^t
231  out->AddMatMat(1.0, in, kNoTrans, vis_hid_, kTrans, 1.0);
232  // optionally apply sigmoid
233  if (hid_type_ == RbmBase::Bernoulli) {
234  out->Sigmoid(*out);
235  }
236  }
CuVector< BaseFloat > hid_bias_
Vector with biases.
Definition: nnet-rbm.h:418
RbmNodeType hid_type_
Definition: nnet-rbm.h:425
CuMatrix< BaseFloat > vis_hid_
Matrix with neuron weights.
Definition: nnet-rbm.h:416

◆ RbmUpdate()

void RbmUpdate ( const CuMatrixBase< BaseFloat > &  pos_vis,
const CuMatrixBase< BaseFloat > &  pos_hid,
const CuMatrixBase< BaseFloat > &  neg_vis,
const CuMatrixBase< BaseFloat > &  neg_hid 
)
inlinevirtual

Implements RbmBase.

Definition at line 262 of file nnet-rbm.h.

References kaldi::nnet1::CheckNanInf(), kaldi::nnet1::ComputeStdDev(), RbmBase::Gaussian, Component::input_dim_, KALDI_ASSERT, KALDI_WARN, kaldi::kNoTrans, kaldi::kSetZero, kaldi::kTrans, RbmTrainOptions::l2_penalty, RbmTrainOptions::learn_rate, RbmTrainOptions::momentum, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), Component::output_dim_, and RbmBase::rbm_opts_.

265  {
266  // dims
267  KALDI_ASSERT(pos_vis.NumRows() == pos_hid.NumRows() &&
268  pos_vis.NumRows() == neg_vis.NumRows() &&
269  pos_vis.NumRows() == neg_hid.NumRows() &&
270  pos_vis.NumCols() == neg_vis.NumCols() &&
271  pos_hid.NumCols() == neg_hid.NumCols() &&
272  pos_vis.NumCols() == input_dim_ &&
273  pos_hid.NumCols() == output_dim_);
274 
275  // lazy initialization of buffers
276  if ( vis_hid_corr_.NumRows() != vis_hid_.NumRows() ||
277  vis_hid_corr_.NumCols() != vis_hid_.NumCols() ||
278  vis_bias_corr_.Dim() != vis_bias_.Dim() ||
279  hid_bias_corr_.Dim() != hid_bias_.Dim() ) {
280  vis_hid_corr_.Resize(vis_hid_.NumRows(), vis_hid_.NumCols(), kSetZero);
281  vis_bias_corr_.Resize(vis_bias_.Dim(), kSetZero);
282  hid_bias_corr_.Resize(hid_bias_.Dim(), kSetZero);
283  }
284 
285  // ANTI-WEIGHT-EXPLOSION PROTECTION (Gaussian-Bernoulli RBM)
286  //
287  // in the following section we detect that the weights in
288  // Gaussian-Bernoulli RBM are almost exploding. The weight
289  // explosion is caused by large variance of the reconstructed data,
290  // which causes a feed-back loop that keeps increasing the weights.
291  //
292  // To avoid explosion, the standard-deviation of the visible-data
293  // and reconstructed-data should be about the same.
294  // The model is particularly sensitive at the very
295  // beginning of the CD-1 training.
296  //
297  // We compute the standard deviations on
298  // * 'A' : input mini-batch
299  // * 'B' : reconstruction.
300  // When 'B > 2*A', we stabilize the training in this way:
301  // 1. we scale down the weights and biases by 'A/B',
302  // 2. we shrink learning rate by 0.9x,
303  // 3. we reset the momentum buffer,
304  //
305  // A warning message is put to the log. In later stage
306  // the learning-rate returns back to its original value.
307  //
308  // To avoid the issue, we make sure that the weight-matrix
309  // is sensibly initialized.
310  //
311  if (vis_type_ == RbmBase::Gaussian) {
312  // check the data have no nan/inf:
313  CheckNanInf(pos_vis, "pos_vis");
314  CheckNanInf(pos_hid, "pos_hid");
315  CheckNanInf(neg_vis, "neg_vis");
316  CheckNanInf(neg_hid, "pos_hid");
317 
318  // get standard deviations of pos_vis and neg_vis:
319  BaseFloat pos_vis_std = ComputeStdDev(pos_vis);
320  BaseFloat neg_vis_std = ComputeStdDev(neg_vis);
321 
322  // monitor the standard deviation mismatch : data vs. reconstruction
323  if (pos_vis_std * 2 < neg_vis_std) {
324  // 1) scale-down the weights and biases
325  BaseFloat scale = pos_vis_std / neg_vis_std;
326  vis_hid_.Scale(scale);
327  vis_bias_.Scale(scale);
328  hid_bias_.Scale(scale);
329  // 2) reduce the learning rate
330  rbm_opts_.learn_rate *= 0.9;
331  // 3) reset the momentum buffers
332  vis_hid_corr_.SetZero();
333  vis_bias_corr_.SetZero();
334  hid_bias_corr_.SetZero();
335 
336  KALDI_WARN << "Mismatch between pos_vis and neg_vis variances, "
337  << "danger of weight explosion."
338  << " a) Reducing weights with scale " << scale
339  << " b) Lowering learning rate to " << rbm_opts_.learn_rate
340  << " [pos_vis_std:" << pos_vis_std
341  << ",neg_vis_std:" << neg_vis_std << "]";
342  return; /* i.e. don't update now, the update would be too BIG */
343  }
344  }
345  //
346  // End of weight-explosion check
347 
348 
349  // We use these training hyper-parameters
350  //
351  const BaseFloat lr = rbm_opts_.learn_rate;
352  const BaseFloat mmt = rbm_opts_.momentum;
353  const BaseFloat l2 = rbm_opts_.l2_penalty;
354 
355  // UPDATE vishid matrix
356  //
357  // vishidinc = momentum*vishidinc + ...
358  // epsilonw*( (posprods-negprods)/numcases - weightcost*vishid)
359  //
360  // vishidinc[t] = -(epsilonw/numcases)*negprods + momentum*vishidinc[t-1]
361  // +(epsilonw/numcases)*posprods
362  // -(epsilonw*weightcost)*vishid[t-1]
363  //
364  BaseFloat N = static_cast<BaseFloat>(pos_vis.NumRows());
365  vis_hid_corr_.AddMatMat(-lr/N, neg_hid, kTrans, neg_vis, kNoTrans, mmt);
366  vis_hid_corr_.AddMatMat(+lr/N, pos_hid, kTrans, pos_vis, kNoTrans, 1.0);
367  vis_hid_corr_.AddMat(-lr*l2, vis_hid_);
368  vis_hid_.AddMat(1.0, vis_hid_corr_);
369 
370  // UPDATE visbias vector
371  //
372  // visbiasinc = momentum*visbiasinc +
373  // (epsilonvb/numcases)*(posvisact-negvisact);
374  //
375  vis_bias_corr_.AddRowSumMat(-lr/N, neg_vis, mmt);
376  vis_bias_corr_.AddRowSumMat(+lr/N, pos_vis, 1.0);
377  vis_bias_.AddVec(1.0, vis_bias_corr_, 1.0);
378 
379  // UPDATE hidbias vector
380  //
381  // hidbiasinc = momentum*hidbiasinc +
382  // (epsilonhb/numcases)*(poshidact-neghidact);
383  //
384  hid_bias_corr_.AddRowSumMat(-lr/N, neg_hid, mmt);
385  hid_bias_corr_.AddRowSumMat(+lr/N, pos_hid, 1.0);
386  hid_bias_.AddVec(1.0, hid_bias_corr_, 1.0);
387  }
CuVector< BaseFloat > hid_bias_
Vector with biases.
Definition: nnet-rbm.h:418
CuVector< BaseFloat > hid_bias_corr_
Vector for bias updates.
Definition: nnet-rbm.h:422
int32 input_dim_
Data members,.
RbmTrainOptions rbm_opts_
Definition: nnet-rbm.h:76
RbmNodeType vis_type_
Definition: nnet-rbm.h:424
CuMatrix< BaseFloat > vis_hid_
Matrix with neuron weights.
Definition: nnet-rbm.h:416
float BaseFloat
Definition: kaldi-types.h:29
Real ComputeStdDev(const CuMatrixBase< Real > &mat)
Get the standard deviation of values in the matrix.
Definition: nnet-utils.h:142
#define KALDI_WARN
Definition: kaldi-error.h:150
int32 output_dim_
Dimension of the output of the Component,.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
CuVector< BaseFloat > vis_bias_
Vector with biases.
Definition: nnet-rbm.h:417
CuVector< BaseFloat > vis_bias_corr_
Vector for bias updates.
Definition: nnet-rbm.h:421
CuMatrix< BaseFloat > vis_hid_corr_
Matrix for linearity updates.
Definition: nnet-rbm.h:420
void CheckNanInf(const CuMatrixBase< Real > &mat, const char *msg="")
Check that matrix contains no nan or inf.
Definition: nnet-utils.h:132

◆ ReadData()

void ReadData ( std::istream &  is,
bool  binary 
)
inlinevirtual

Reads the component content.

Reimplemented from Component.

Definition at line 182 of file nnet-rbm.h.

References RbmBase::Bernoulli, RbmBase::Gaussian, Component::input_dim_, KALDI_ASSERT, Component::output_dim_, and kaldi::ReadToken().

182  {
183  std::string vis_node_type, hid_node_type;
184  ReadToken(is, binary, &vis_node_type);
185  ReadToken(is, binary, &hid_node_type);
186 
187  if (vis_node_type == "bern") {
189  } else if (vis_node_type == "gauss") {
191  }
192  if (hid_node_type == "bern") {
194  } else if (hid_node_type == "gauss") {
196  }
197 
198  vis_hid_.Read(is, binary);
199  vis_bias_.Read(is, binary);
200  hid_bias_.Read(is, binary);
201 
202  KALDI_ASSERT(vis_hid_.NumRows() == output_dim_);
203  KALDI_ASSERT(vis_hid_.NumCols() == input_dim_);
206  }
CuVector< BaseFloat > hid_bias_
Vector with biases.
Definition: nnet-rbm.h:418
int32 input_dim_
Data members,.
RbmNodeType hid_type_
Definition: nnet-rbm.h:425
RbmNodeType vis_type_
Definition: nnet-rbm.h:424
CuMatrix< BaseFloat > vis_hid_
Matrix with neuron weights.
Definition: nnet-rbm.h:416
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
Definition: io-funcs.cc:154
int32 output_dim_
Dimension of the output of the Component,.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
CuVector< BaseFloat > vis_bias_
Vector with biases.
Definition: nnet-rbm.h:417

◆ Reconstruct()

void Reconstruct ( const CuMatrixBase< BaseFloat > &  hid_state,
CuMatrix< BaseFloat > *  vis_probs 
)
inlinevirtual

Implements RbmBase.

Definition at line 239 of file nnet-rbm.h.

References CuMatrixBase< Real >::AddMatMat(), CuMatrixBase< Real >::AddVecToRows(), RbmBase::Bernoulli, Component::input_dim_, KALDI_ERR, kaldi::kNoTrans, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), Component::output_dim_, CuMatrix< Real >::Resize(), and CuMatrixBase< Real >::Sigmoid().

240  {
241  // check the dim
242  if (output_dim_ != hid_state.NumCols()) {
243  KALDI_ERR << "Nonmatching dims, component:" << output_dim_
244  << " data:" << hid_state.NumCols();
245  }
246  // optionally allocate buffer
247  if (input_dim_ != vis_probs->NumCols() ||
248  hid_state.NumRows() != vis_probs->NumRows()) {
249  vis_probs->Resize(hid_state.NumRows(), input_dim_);
250  }
251 
252  // pre-fill with bias
253  vis_probs->AddVecToRows(1.0, vis_bias_, 0.0);
254  // multiply by weights
255  vis_probs->AddMatMat(1.0, hid_state, kNoTrans, vis_hid_, kNoTrans, 1.0);
256  // optionally apply sigmoid
257  if (vis_type_ == RbmBase::Bernoulli) {
258  vis_probs->Sigmoid(*vis_probs);
259  }
260  }
int32 input_dim_
Data members,.
RbmNodeType vis_type_
Definition: nnet-rbm.h:424
CuMatrix< BaseFloat > vis_hid_
Matrix with neuron weights.
Definition: nnet-rbm.h:416
#define KALDI_ERR
Definition: kaldi-error.h:147
int32 output_dim_
Dimension of the output of the Component,.
CuVector< BaseFloat > vis_bias_
Vector with biases.
Definition: nnet-rbm.h:417

◆ VisType()

RbmNodeType VisType ( ) const
inlinevirtual

Implements RbmBase.

Definition at line 389 of file nnet-rbm.h.

389  {
390  return vis_type_;
391  }
RbmNodeType vis_type_
Definition: nnet-rbm.h:424

◆ WriteAsNnet()

void WriteAsNnet ( std::ostream &  os,
bool  binary 
) const
inlinevirtual

Implements RbmBase.

Definition at line 397 of file nnet-rbm.h.

References RbmBase::Bernoulli, RbmBase::HidType(), Component::InputDim(), Component::kAffineTransform, Component::kSigmoid, Component::OutputDim(), Component::TypeToMarker(), kaldi::WriteBasicType(), and kaldi::WriteToken().

397  {
398  // header,
400  WriteBasicType(os, binary, OutputDim());
401  WriteBasicType(os, binary, InputDim());
402  if (!binary) os << "\n";
403  // data,
404  vis_hid_.Write(os, binary);
405  hid_bias_.Write(os, binary);
406  // sigmoid activation,
407  if (HidType() == Bernoulli) {
409  WriteBasicType(os, binary, OutputDim());
410  WriteBasicType(os, binary, OutputDim());
411  }
412  if (!binary) os << "\n";
413  }
CuVector< BaseFloat > hid_bias_
Vector with biases.
Definition: nnet-rbm.h:418
CuMatrix< BaseFloat > vis_hid_
Matrix with neuron weights.
Definition: nnet-rbm.h:416
static const char * TypeToMarker(ComponentType t)
Converts component type to marker,.
RbmNodeType HidType() const
Definition: nnet-rbm.h:393
int32 InputDim() const
Get the dimension of the input,.
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34
int32 OutputDim() const
Get the dimension of the output,.

◆ WriteData()

void WriteData ( std::ostream &  os,
bool  binary 
) const
inlinevirtual

Writes the component content.

Reimplemented from Component.

Definition at line 208 of file nnet-rbm.h.

References RbmBase::Bernoulli, RbmBase::Gaussian, KALDI_ERR, and kaldi::WriteToken().

208  {
209  switch (vis_type_) {
210  case Bernoulli : WriteToken(os,binary, "bern"); break;
211  case Gaussian : WriteToken(os,binary, "gauss"); break;
212  default : KALDI_ERR << "Unknown type " << vis_type_;
213  }
214  switch (hid_type_) {
215  case Bernoulli : WriteToken(os,binary, "bern"); break;
216  case Gaussian : WriteToken(os,binary, "gauss"); break;
217  default : KALDI_ERR << "Unknown type " << hid_type_;
218  }
219  vis_hid_.Write(os, binary);
220  vis_bias_.Write(os, binary);
221  hid_bias_.Write(os, binary);
222  }
CuVector< BaseFloat > hid_bias_
Vector with biases.
Definition: nnet-rbm.h:418
RbmNodeType hid_type_
Definition: nnet-rbm.h:425
RbmNodeType vis_type_
Definition: nnet-rbm.h:424
CuMatrix< BaseFloat > vis_hid_
Matrix with neuron weights.
Definition: nnet-rbm.h:416
#define KALDI_ERR
Definition: kaldi-error.h:147
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
CuVector< BaseFloat > vis_bias_
Vector with biases.
Definition: nnet-rbm.h:417

Member Data Documentation

◆ hid_bias_

CuVector<BaseFloat> hid_bias_
protected

Vector with biases.

Definition at line 418 of file nnet-rbm.h.

◆ hid_bias_corr_

CuVector<BaseFloat> hid_bias_corr_
protected

Vector for bias updates.

Definition at line 422 of file nnet-rbm.h.

◆ hid_type_

RbmNodeType hid_type_
protected

Definition at line 425 of file nnet-rbm.h.

◆ vis_bias_

CuVector<BaseFloat> vis_bias_
protected

Vector with biases.

Definition at line 417 of file nnet-rbm.h.

◆ vis_bias_corr_

CuVector<BaseFloat> vis_bias_corr_
protected

Vector for bias updates.

Definition at line 421 of file nnet-rbm.h.

◆ vis_hid_

CuMatrix<BaseFloat> vis_hid_
protected

Matrix with neuron weights.

Definition at line 416 of file nnet-rbm.h.

◆ vis_hid_corr_

CuMatrix<BaseFloat> vis_hid_corr_
protected

Matrix for linearity updates.

Definition at line 420 of file nnet-rbm.h.

◆ vis_type_

RbmNodeType vis_type_
protected

Definition at line 424 of file nnet-rbm.h.


The documentation for this class was generated from the following file: