CompositeComponent Class Reference

CompositeComponent is a component representing a sequence of [simple] components. More...

#include <nnet-simple-component.h>

Inheritance diagram for CompositeComponent:
Collaboration diagram for CompositeComponent:

Public Member Functions

virtual int32 InputDim () const
 Returns input-dimension of this component. More...
 
virtual int32 OutputDim () const
 Returns output-dimension of this component. More...
 
virtual std::string Info () const
 Returns some text-form information about this component, for diagnostics. More...
 
virtual void InitFromConfig (ConfigLine *cfl)
 Initialize, from a ConfigLine object. More...
 
virtual ComponentCopy () const
 Copies component (deep copy). More...
 
 CompositeComponent ()
 
void Init (const std::vector< Component *> &components, int32 max_rows_process)
 
virtual std::string Type () const
 Returns a string such as "SigmoidComponent", describing the type of the object. More...
 
virtual int32 Properties () const
 Return bitmask of the component's properties. More...
 
virtual void * Propagate (const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
 Propagate function. More...
 
virtual void Backprop (const std::string &debug_info, const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &, const CuMatrixBase< BaseFloat > &out_deriv, void *memo, Component *to_update, CuMatrixBase< BaseFloat > *in_deriv) const
 Backprop function; depending on which of the arguments 'to_update' and 'in_deriv' are non-NULL, this can compute input-data derivatives and/or perform model update. More...
 
virtual void ZeroStats ()
 Components that provide an implementation of StoreStats should also provide an implementation of ZeroStats(), to set those stats to zero. More...
 
virtual void Read (std::istream &is, bool binary)
 Read function (used after we know the type of the Component); accepts input that is missing the token that describes the component type, in case it has already been consumed. More...
 
virtual void Write (std::ostream &os, bool binary) const
 Write component to stream. More...
 
virtual void SetUnderlyingLearningRate (BaseFloat lrate)
 Sets the learning rate of gradient descent- gets multiplied by learning_rate_factor_. More...
 
virtual void SetActualLearningRate (BaseFloat lrate)
 Sets the learning rate directly, bypassing learning_rate_factor_. More...
 
virtual void SetAsGradient ()
 Sets is_gradient_ to true and sets learning_rate_ to 1, ignoring learning_rate_factor_. More...
 
virtual void Scale (BaseFloat scale)
 This virtual function when called on – an UpdatableComponent scales the parameters by "scale" when called by an UpdatableComponent. More...
 
virtual void Add (BaseFloat alpha, const Component &other)
 This virtual function when called by – an UpdatableComponent adds the parameters of another updatable component, times some constant, to the current parameters. More...
 
virtual void PerturbParams (BaseFloat stddev)
 This function is to be used in testing. More...
 
virtual BaseFloat DotProduct (const UpdatableComponent &other) const
 Computes dot-product between parameters of two instances of a Component. More...
 
virtual int32 NumParameters () const
 The following new virtual function returns the total dimension of the parameters in this class. More...
 
virtual void Vectorize (VectorBase< BaseFloat > *params) const
 Turns the parameters into vector form. More...
 
virtual void UnVectorize (const VectorBase< BaseFloat > &params)
 Converts the parameters from vector form. More...
 
virtual void FreezeNaturalGradient (bool freeze)
 virtual More...
 
int32 NumComponents () const
 
const ComponentGetComponent (int32 i) const
 Gets the ith component in this component. More...
 
void SetComponent (int32 i, Component *component)
 Sets the ith component. More...
 
virtual ~CompositeComponent ()
 
- Public Member Functions inherited from UpdatableComponent
 UpdatableComponent (const UpdatableComponent &other)
 
 UpdatableComponent ()
 
virtual ~UpdatableComponent ()
 
virtual BaseFloat LearningRateFactor ()
 
virtual void SetLearningRateFactor (BaseFloat lrate_factor)
 
void SetUpdatableConfigs (const UpdatableComponent &other)
 
BaseFloat LearningRate () const
 Gets the learning rate to be used in gradient descent. More...
 
BaseFloat MaxChange () const
 Returns the per-component max-change value, which is interpreted as the maximum change (in l2 norm) in parameters that is allowed per minibatch for this component. More...
 
void SetMaxChange (BaseFloat max_change)
 
BaseFloat L2Regularization () const
 Returns the l2 regularization constant, which may be set in any updatable component (usually from the config file). More...
 
void SetL2Regularization (BaseFloat a)
 
- Public Member Functions inherited from Component
virtual void StoreStats (const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, void *memo)
 This function may store stats on average activation values, and for some component types, the average value of the derivative of the nonlinearity. More...
 
virtual void GetInputIndexes (const MiscComputationInfo &misc_info, const Index &output_index, std::vector< Index > *desired_indexes) const
 This function only does something interesting for non-simple Components. More...
 
virtual bool IsComputable (const MiscComputationInfo &misc_info, const Index &output_index, const IndexSet &input_index_set, std::vector< Index > *used_inputs) const
 This function only does something interesting for non-simple Components, and it exists to make it possible to manage optionally-required inputs. More...
 
virtual void ReorderIndexes (std::vector< Index > *input_indexes, std::vector< Index > *output_indexes) const
 This function only does something interesting for non-simple Components. More...
 
virtual ComponentPrecomputedIndexesPrecomputeIndexes (const MiscComputationInfo &misc_info, const std::vector< Index > &input_indexes, const std::vector< Index > &output_indexes, bool need_backprop) const
 This function must return NULL for simple Components. More...
 
virtual void DeleteMemo (void *memo) const
 This virtual function only needs to be overwritten by Components that return a non-NULL memo from their Propagate() function. More...
 
 Component ()
 
virtual ~Component ()
 

Private Member Functions

MatrixStrideType GetStrideType (int32 i) const
 
bool IsUpdatable () const
 

Private Attributes

int32 max_rows_process_
 
std::vector< Component * > components_
 

Additional Inherited Members

- Static Public Member Functions inherited from Component
static ComponentReadNew (std::istream &is, bool binary)
 Read component from stream (works out its type). Dies on error. More...
 
static ComponentNewComponentOfType (const std::string &type)
 Returns a new Component of the given type e.g. More...
 
- Protected Member Functions inherited from UpdatableComponent
void InitLearningRatesFromConfig (ConfigLine *cfl)
 
std::string ReadUpdatableCommon (std::istream &is, bool binary)
 
void WriteUpdatableCommon (std::ostream &is, bool binary) const
 
- Protected Attributes inherited from UpdatableComponent
BaseFloat learning_rate_
 learning rate (typically 0.0..0.01) More...
 
BaseFloat learning_rate_factor_
 learning rate factor (normally 1.0, but can be set to another < value so that when < you call SetLearningRate(), that value will be scaled by this factor. More...
 
BaseFloat l2_regularize_
 L2 regularization constant. More...
 
bool is_gradient_
 True if this component is to be treated as a gradient rather than as parameters. More...
 
BaseFloat max_change_
 configuration value for imposing max-change More...
 

Detailed Description

CompositeComponent is a component representing a sequence of [simple] components.

The config line would be something like the following (imagine this is all on one line):

component name=composite1 type=CompositeComponent max-rows-process=2048 num-components=3 \ component1='type=BlockAffineComponent input-dim=1000 output-dim=10000 num-blocks=100' \ component2='type=RectifiedLinearComponent dim=10000' \ component3='type=BlockAffineComponent input-dim=10000 output-dim=1000 num-blocks=100'

The reason you might want to use this component, instead of directly using the same sequence of components in the config file, is to save GPU memory (at the expense of more compute)– because doing it like this means we have to re-do parts of the forward pass in the backprop phase, but we avoid using much memory for very long (and you can make the memory usage very small by making max-rows-process small). We inherit from UpdatableComponent just in case one or more of the components in the sequence are updatable.

It is an error to nest a CompositeComponent inside a CompositeComponent. The same effect can be accomplished by specifying a smaller max-rows-process in a single CompositeComponent.

Definition at line 2475 of file nnet-simple-component.h.

Constructor & Destructor Documentation

◆ CompositeComponent()

CompositeComponent ( )
inline

Definition at line 2486 of file nnet-simple-component.h.

References PnormComponent::Init().

2486 { } // use Init() or InitFromConfig() to really initialize.

◆ ~CompositeComponent()

virtual ~CompositeComponent ( )
inlinevirtual

Definition at line 2554 of file nnet-simple-component.h.

References kaldi::DeletePointers().

void DeletePointers(std::vector< A *> *v)
Deletes any non-NULL pointers in the vector v, and sets the corresponding entries of v to NULL...
Definition: stl-utils.h:186
std::vector< Component * > components_

Member Function Documentation

◆ Add()

void Add ( BaseFloat  alpha,
const Component other 
)
virtual

This virtual function when called by – an UpdatableComponent adds the parameters of another updatable component, times some constant, to the current parameters.

– a NonlinearComponent (or another component that stores stats, like BatchNormComponent)– it relates to adding stats. Otherwise it will normally do nothing.

Reimplemented from Component.

Definition at line 5295 of file nnet-simple-component.cc.

References Component::Add(), CompositeComponent::components_, rnnlm::i, and KALDI_ASSERT.

5295  {
5296  const CompositeComponent *other = dynamic_cast<const CompositeComponent*>(
5297  &other_in);
5298  KALDI_ASSERT(other != NULL && other->components_.size() ==
5299  components_.size() && "Mismatching nnet topologies");
5300  for (size_t i = 0; i < components_.size(); i++)
5301  components_[i]->Add(alpha, *(other->components_[i]));
5302 }
virtual void Add(BaseFloat alpha, const Component &other)
This virtual function when called by – an UpdatableComponent adds the parameters of another updatabl...
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

◆ Backprop()

void Backprop ( const std::string &  debug_info,
const ComponentPrecomputedIndexes indexes,
const CuMatrixBase< BaseFloat > &  in_value,
const CuMatrixBase< BaseFloat > &  out_value,
const CuMatrixBase< BaseFloat > &  out_deriv,
void *  memo,
Component to_update,
CuMatrixBase< BaseFloat > *  in_deriv 
) const
virtual

Backprop function; depending on which of the arguments 'to_update' and 'in_deriv' are non-NULL, this can compute input-data derivatives and/or perform model update.

Parameters
[in]debug_infoThe component name, to be printed out in any warning messages.
[in]indexesA pointer to some information output by this class's PrecomputeIndexes function (will be NULL for simple components, i.e. those that don't do things like splicing).
[in]in_valueThe matrix that was given as input to the Propagate function. Will be ignored (and may be empty) if Properties()&kBackpropNeedsInput == 0.
[in]out_valueThe matrix that was output from the Propagate function. Will be ignored (and may be empty) if Properties()&kBackpropNeedsOutput == 0
[in]out_derivThe derivative at the output of this component.
[in]memoThis will normally be NULL, but for component types that set the flag kUsesMemo, this will be the return value of the Propagate() function that corresponds to this Backprop() function. Ownership of any pointers is not transferred to the Backprop function; DeleteMemo() will be called to delete it.
[out]to_updateIf model update is desired, the Component to be updated, else NULL. Does not have to be identical to this. If supplied, you can assume that to_update->Properties() & kUpdatableComponent is nonzero.
[out]in_derivThe derivative at the input of this component, if needed (else NULL). If Properties()&kBackpropInPlace, may be the same matrix as out_deriv. If Properties()&kBackpropAdds, this is added to by the Backprop routine, else it is set. The component code chooses which mode to work in, based on convenience.

Implements Component.

Definition at line 5150 of file nnet-simple-component.cc.

References MaxpoolingComponent::Backprop(), rnnlm::i, MaxpoolingComponent::InputDim(), KALDI_ASSERT, kaldi::nnet3::kBackpropAdds, kaldi::nnet3::kBackpropNeedsInput, kaldi::nnet3::kBackpropNeedsOutput, kaldi::nnet3::kPropagateAdds, kaldi::kSetZero, kaldi::nnet3::kStoresStats, kaldi::kUndefined, kaldi::nnet3::kUpdatableComponent, kaldi::nnet3::kUsesMemo, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), MaxpoolingComponent::OutputDim(), MaxpoolingComponent::Properties(), and Component::StoreStats().

5157  {
5158  KALDI_ASSERT(in_value.NumRows() == out_deriv.NumRows() &&
5159  in_value.NumCols() == InputDim() &&
5160  out_deriv.NumCols() == OutputDim());
5161  int32 num_rows = in_value.NumRows(),
5162  num_components = components_.size();
5163  if (max_rows_process_ > 0 && num_rows > max_rows_process_) {
5165  // recurse and process smaller parts of the data, to save memory.
5166  for (int32 row_offset = 0; row_offset < num_rows;
5167  row_offset += max_rows_process_) {
5168  bool have_output_value = (out_value.NumRows() != 0);
5169  int32 this_num_rows = std::min<int32>(max_rows_process_,
5170  num_rows - row_offset);
5171  // out_value_part will only be used if out_value is nonempty; otherwise we
5172  // make it a submatrix of 'out_deriv' to avoid errors in the constructor.
5173  const CuSubMatrix<BaseFloat> out_value_part(have_output_value ? out_value : out_deriv,
5174  row_offset, this_num_rows,
5175  0, out_deriv.NumCols());
5176  // in_deriv_value_part will only be used if in_deriv != NULL; otherwise we
5177  // make it a submatrix of 'in_value' to avoid errors in the constructor.
5178  CuSubMatrix<BaseFloat> in_deriv_part(in_deriv != NULL ? *in_deriv : in_value,
5179  row_offset, this_num_rows,
5180  0, in_value.NumCols());
5181  CuSubMatrix<BaseFloat> in_value_part(in_value, row_offset, this_num_rows,
5182  0, in_value.NumCols());
5183  const CuSubMatrix<BaseFloat> out_deriv_part(out_deriv,
5184  row_offset, this_num_rows,
5185  0, out_deriv.NumCols());
5186  CuMatrix<BaseFloat> empty_mat;
5187  this->Backprop(debug_info, NULL, in_value_part,
5188  (have_output_value ? static_cast<const CuMatrixBase<BaseFloat>&>(out_value_part) :
5189  static_cast<const CuMatrixBase<BaseFloat>&>(empty_mat)),
5190  out_deriv_part, NULL, to_update,
5191  in_deriv != NULL ? &in_deriv_part : NULL);
5192  }
5193  return;
5194  }
5195  // For now, assume all intermediate values and derivatives need to be
5196  // computed. in_value and out_deriv will always be supplied.
5197 
5198  // intermediate_outputs[i] contains the output of component i.
5199  std::vector<CuMatrix<BaseFloat> > intermediate_outputs(num_components);
5200  // intermediate_derivs[i] contains the deriative at the output of component i.
5201  std::vector<CuMatrix<BaseFloat> > intermediate_derivs(num_components - 1);
5202 
5203  KALDI_ASSERT(memo == NULL);
5204  // note: only a very few components use memos, but we need to support them.
5205  std::vector<void*> memos(num_components, NULL);
5206 
5207  int32 num_components_to_propagate = num_components;
5208  if (!(components_[num_components - 1]->Properties() & kUsesMemo)) {
5209  // we only need to propagate the very last component if it uses a memo.
5210  num_components_to_propagate--;
5211  if (num_components > 1) {
5212  // skip the last-but-one component's propagate if the last component's
5213  // backprop doesn't need the input and the last-but-one component's
5214  // backprop doesn't need the output. This is the lowest hanging fruit for
5215  // optimization; other propagates might also be skippable.
5216  int32 properties = components_[num_components - 2]->Properties(),
5217  next_properties = components_[num_components - 1]->Properties();
5218  if (!(properties & (kBackpropNeedsOutput || kUsesMemo)) &&
5219  !(next_properties & kBackpropNeedsInput)) {
5220  num_components_to_propagate--;
5221  }
5222  }
5223  }
5224 
5225 
5226  // Do the propagation again.
5227  for (int32 i = 0; i < num_components_to_propagate; i++) {
5228  MatrixResizeType resize_type =
5229  ((components_[i]->Properties() & kPropagateAdds) ?
5230  kSetZero : kUndefined);
5231  intermediate_outputs[i].Resize(num_rows, components_[i]->OutputDim(),
5232  resize_type, GetStrideType(i));
5233  memos[i] =
5234  components_[i]->Propagate(NULL,
5235  (i == 0 ? in_value : intermediate_outputs[i-1]),
5236  &(intermediate_outputs[i]));
5237  }
5238 
5239  for (int32 i = num_components - 1; i >= 0; i--) {
5240  const CuMatrixBase<BaseFloat> &this_in_value =
5241  (i == 0 ? in_value : intermediate_outputs[i-1]),
5242  &this_out_value =
5243  (i == num_components - 1 ? out_value : intermediate_outputs[i]);
5244 
5245  Component *component_to_update =
5246  (to_update == NULL ? NULL :
5247  dynamic_cast<CompositeComponent*>(to_update)->components_[i]);
5248 
5249  if (component_to_update != NULL &&
5251  component_to_update->StoreStats(this_in_value, this_out_value, memos[i]);
5252 
5253  if (i > 0) {
5254  MatrixResizeType resize_type =
5255  ((components_[i]->Properties() & kBackpropAdds) ?
5256  kSetZero : kUndefined);
5257  intermediate_derivs[i-1].Resize(num_rows, components_[i]->InputDim(),
5258  resize_type, GetStrideType(i - 1));
5259  }
5260  // skip the first component's backprop if it's not updatable and in_deriv is
5261  // not requested. Again, this is the lowest-hanging fruit to optimize.
5262  if (!(i == 0 && !(components_[0]->Properties() & kUpdatableComponent) &&
5263  in_deriv == NULL)) {
5264  components_[i]->Backprop(debug_info, NULL,
5265  this_in_value, this_out_value,
5266  (i + 1 == num_components ? out_deriv : intermediate_derivs[i]),
5267  memos[i], component_to_update,
5268  (i == 0 ? in_deriv : &(intermediate_derivs[i-1])));
5269  }
5270  if (memos[i] != NULL)
5271  components_[i]->DeleteMemo(memos[i]);
5272  }
5273 }
virtual int32 OutputDim() const
Returns output-dimension of this component.
MatrixResizeType
Definition: matrix-common.h:37
virtual void Backprop(const std::string &debug_info, const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &, const CuMatrixBase< BaseFloat > &out_deriv, void *memo, Component *to_update, CuMatrixBase< BaseFloat > *in_deriv) const
Backprop function; depending on which of the arguments &#39;to_update&#39; and &#39;in_deriv&#39; are non-NULL...
MatrixStrideType GetStrideType(int32 i) const
std::vector< Component * > components_
virtual int32 InputDim() const
Returns input-dimension of this component.
virtual int32 Properties() const
Return bitmask of the component&#39;s properties.
MatrixIndexT NumCols() const
Definition: cu-matrix.h:215
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:214

◆ Copy()

Component * Copy ( ) const
virtual

Copies component (deep copy).

Implements Component.

Definition at line 5439 of file nnet-simple-component.cc.

References MaxpoolingComponent::Copy(), rnnlm::i, and CompositeComponent::Init().

5439  {
5440  std::vector<Component*> components(components_.size());
5441  for (size_t i = 0; i < components_.size(); i++)
5442  components[i] = components_[i]->Copy();
5444  ans->Init(components, max_rows_process_);
5445  return ans;
5446 }
std::vector< Component * > components_
virtual Component * Copy() const
Copies component (deep copy).

◆ DotProduct()

BaseFloat DotProduct ( const UpdatableComponent other) const
virtual

Computes dot-product between parameters of two instances of a Component.

Can be used for computing parameter-norm of an UpdatableComponent.

Implements UpdatableComponent.

Definition at line 5406 of file nnet-simple-component.cc.

References CompositeComponent::components_, UpdatableComponent::DotProduct(), rnnlm::i, KALDI_ASSERT, kaldi::nnet3::kUpdatableComponent, and MaxpoolingComponent::Properties().

5407  {
5408  const CompositeComponent *other = dynamic_cast<const CompositeComponent*>(
5409  &other_in);
5410  KALDI_ASSERT(other != NULL && other->components_.size() ==
5411  components_.size() && "Mismatching nnet topologies");
5412  BaseFloat ans = 0.0;
5413  for (size_t i = 0.0; i < components_.size(); i++) {
5415  UpdatableComponent *uc =
5416  dynamic_cast<UpdatableComponent*>(components_[i]);
5417  const UpdatableComponent *uc_other =
5418  dynamic_cast<UpdatableComponent*>(other->components_[i]);
5419  KALDI_ASSERT(uc != NULL && uc_other != NULL);
5420  ans += uc->DotProduct(*uc_other);
5421  }
5422  }
5423  return ans;
5424 }
float BaseFloat
Definition: kaldi-types.h:29
std::vector< Component * > components_
virtual int32 Properties() const
Return bitmask of the component&#39;s properties.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

◆ FreezeNaturalGradient()

void FreezeNaturalGradient ( bool  freeze)
virtual

virtual

Reimplemented from UpdatableComponent.

Definition at line 5427 of file nnet-simple-component.cc.

References UpdatableComponent::FreezeNaturalGradient(), rnnlm::i, KALDI_ASSERT, kaldi::nnet3::kUpdatableComponent, and MaxpoolingComponent::Properties().

5427  {
5428  for (size_t i = 0; i < components_.size(); i++) {
5430  UpdatableComponent *uc =
5431  dynamic_cast<UpdatableComponent*>(components_[i]);
5432  KALDI_ASSERT(uc != NULL);
5433  uc->FreezeNaturalGradient(freeze);
5434  }
5435  }
5436 }
std::vector< Component * > components_
virtual int32 Properties() const
Return bitmask of the component&#39;s properties.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

◆ GetComponent()

const Component * GetComponent ( int32  i) const

Gets the ith component in this component.

The ordering is the same as in the config line. The caller does not own the received component.

Definition at line 5505 of file nnet-simple-component.cc.

References rnnlm::i, and KALDI_ASSERT.

Referenced by kaldi::nnet3::ConvertRepeatedToBlockAffine(), and kaldi::nnet3::UnitTestConvertRepeatedToBlockAffineComposite().

5505  {
5506  KALDI_ASSERT(static_cast<size_t>(i) < components_.size());
5507  return components_[i];
5508 }
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

◆ GetStrideType()

MatrixStrideType GetStrideType ( int32  i) const
inlineprivate

Definition at line 4998 of file nnet-simple-component.cc.

References kaldi::kDefaultStride, kaldi::nnet3::kInputContiguous, kaldi::nnet3::kOutputContiguous, kaldi::kStrideEqualNumCols, and MaxpoolingComponent::Properties().

4998  {
4999  int32 num_components = components_.size();
5000  if ((components_[i]->Properties() & kOutputContiguous) ||
5001  (i + 1 < num_components &&
5003  return kStrideEqualNumCols;
5004  else
5005  return kDefaultStride;
5006 }
std::vector< Component * > components_
virtual int32 Properties() const
Return bitmask of the component&#39;s properties.

◆ Info()

std::string Info ( ) const
virtual

Returns some text-form information about this component, for diagnostics.

Starts with the type of the component. E.g. "SigmoidComponent dim=900", although most components will have much more info.

Reimplemented from UpdatableComponent.

Definition at line 5277 of file nnet-simple-component.cc.

References rnnlm::i, MaxpoolingComponent::Info(), and MaxpoolingComponent::Type().

5277  {
5278  std::ostringstream stream;
5279  stream << Type() << " ";
5280  for (size_t i = 0; i < components_.size(); i++) {
5281  if (i > 0) stream << ", ";
5282  stream << "sub-component" << (i+1) << " = { "
5283  << components_[i]->Info() << " }";
5284  }
5285  return stream.str();
5286 }
virtual std::string Info() const
Returns some text-form information about this component, for diagnostics.
virtual std::string Type() const
Returns a string such as "SigmoidComponent", describing the type of the object.
std::vector< Component * > components_

◆ Init()

void Init ( const std::vector< Component *> &  components,
int32  max_rows_process 
)

Definition at line 5057 of file nnet-simple-component.cc.

References kaldi::DeletePointers(), rnnlm::i, MaxpoolingComponent::InputDim(), KALDI_ASSERT, kaldi::nnet3::kSimpleComponent, MaxpoolingComponent::OutputDim(), and MaxpoolingComponent::Properties().

Referenced by CompositeComponent::Copy().

5058  {
5059  DeletePointers(&components_); // clean up.
5060  components_ = components;
5061  KALDI_ASSERT(!components.empty());
5062  max_rows_process_ = max_rows_process;
5063 
5064  for (size_t i = 0; i < components_.size(); i++) {
5065  // make sure all constituent components are simple.
5067  if (i > 0) {
5068  // make sure all the internal dimensions match up.
5070  components_[i-1]->OutputDim());
5071  }
5072  }
5073 }
virtual int32 OutputDim() const
Returns output-dimension of this component.
void DeletePointers(std::vector< A *> *v)
Deletes any non-NULL pointers in the vector v, and sets the corresponding entries of v to NULL...
Definition: stl-utils.h:186
std::vector< Component * > components_
virtual int32 InputDim() const
Returns input-dimension of this component.
virtual int32 Properties() const
Return bitmask of the component&#39;s properties.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

◆ InitFromConfig()

void InitFromConfig ( ConfigLine cfl)
virtual

Initialize, from a ConfigLine object.

Parameters
[in]cflA ConfigLine containing any parameters that are needed for initialization. For example: "dim=100 param-stddev=0.1"

Implements Component.

Definition at line 5450 of file nnet-simple-component.cc.

References kaldi::DeletePointers(), ConfigLine::FirstToken(), ConfigLine::GetValue(), ConfigLine::HasUnusedValues(), rnnlm::i, Component::InitFromConfig(), KALDI_ERR, kaldi::nnet3::kRandomComponent, kaldi::nnet3::kSimpleComponent, Component::NewComponentOfType(), ConfigLine::ParseLine(), Component::Properties(), Component::Type(), ConfigLine::UnusedValues(), and ConfigLine::WholeLine().

5450  {
5451  int32 max_rows_process = 4096, num_components = -1;
5452  cfl->GetValue("max-rows-process", &max_rows_process);
5453  if (!cfl->GetValue("num-components", &num_components) ||
5454  num_components < 1)
5455  KALDI_ERR << "Expected num-components to be defined in "
5456  << "CompositeComponent config line '" << cfl->WholeLine() << "'";
5457  std::vector<Component*> components;
5458  for (int32 i = 1; i <= num_components; i++) {
5459  std::ostringstream name_stream;
5460  name_stream << "component" << i;
5461  std::string component_config;
5462  if (!cfl->GetValue(name_stream.str(), &component_config)) {
5463  DeletePointers(&components);
5464  KALDI_ERR << "Expected '" << name_stream.str() << "' to be defined in "
5465  << "CompositeComponent config line '" << cfl->WholeLine() << "'";
5466  }
5467  ConfigLine nested_line;
5468  // note: the nested line may not contain comments.
5469  std::string component_type;
5470  Component *this_component = NULL;
5471  if (!nested_line.ParseLine(component_config) ||
5472  !nested_line.GetValue("type", &component_type) ||
5473  !(this_component = NewComponentOfType(component_type)) ||
5474  nested_line.FirstToken() != "") {
5475  DeletePointers(&components);
5476  KALDI_ERR << "Could not parse config line for '" << name_stream.str()
5477  << "(or undefined or bad component type [type=xxx]), in "
5478  << "CompositeComponent config line '" << cfl->WholeLine() << "'";
5479  }
5480  if(this_component->Type() == "CompositeComponent") {
5481  DeletePointers(&components);
5482  delete this_component;
5483  // This is not allowed. If memory is too much with just one
5484  // CompositeComponent, try decreasing max-rows-process instead.
5485  KALDI_ERR << "Found CompositeComponent nested within CompositeComponent."
5486  << "Nested line: '" << nested_line.WholeLine() << "'\n"
5487  << "Toplevel CompositeComponent line '" << cfl->WholeLine()
5488  << "'";
5489  }
5490  this_component->InitFromConfig(&nested_line);
5491  int32 props = this_component->Properties();
5492  if ((props & kRandomComponent) != 0 ||
5493  (props & kSimpleComponent) == 0) {
5494  KALDI_ERR << "CompositeComponent contains disallowed component type: "
5495  << nested_line.WholeLine();
5496  }
5497  components.push_back(this_component);
5498  }
5499  if (cfl->HasUnusedValues())
5500  KALDI_ERR << "Could not process these elements in initializer: "
5501  << cfl->UnusedValues();
5502  this->Init(components, max_rows_process);
5503 }
void DeletePointers(std::vector< A *> *v)
Deletes any non-NULL pointers in the vector v, and sets the corresponding entries of v to NULL...
Definition: stl-utils.h:186
#define KALDI_ERR
Definition: kaldi-error.h:127
void Init(const std::vector< Component *> &components, int32 max_rows_process)
static Component * NewComponentOfType(const std::string &type)
Returns a new Component of the given type e.g.

◆ InputDim()

int32 InputDim ( ) const
virtual

Returns input-dimension of this component.

Implements Component.

Definition at line 4964 of file nnet-simple-component.cc.

References KALDI_ASSERT.

4964  {
4965  KALDI_ASSERT(!components_.empty());
4966  return components_.front()->InputDim();
4967 };
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

◆ IsUpdatable()

bool IsUpdatable ( ) const
private

Definition at line 4955 of file nnet-simple-component.cc.

References kaldi::nnet3::kUpdatableComponent.

4955  {
4956  for (std::vector<Component*>::const_iterator iter = components_.begin(),
4957  end = components_.end(); iter != end; ++iter)
4958  if (((*iter)->Properties() & kUpdatableComponent) != 0)
4959  return true;
4960  return false;
4961 }
std::vector< Component * > components_

◆ NumComponents()

int32 NumComponents ( ) const
inline

◆ NumParameters()

int32 NumParameters ( ) const
virtual

The following new virtual function returns the total dimension of the parameters in this class.

Reimplemented from UpdatableComponent.

Definition at line 5358 of file nnet-simple-component.cc.

References rnnlm::i, KALDI_ASSERT, kaldi::nnet3::kUpdatableComponent, UpdatableComponent::NumParameters(), and MaxpoolingComponent::Properties().

5358  {
5359  KALDI_ASSERT(this->IsUpdatable()); // or should not be called.
5360  int32 ans = 0;
5361  for (size_t i = 0; i < components_.size(); i++) {
5363  UpdatableComponent *uc =
5364  dynamic_cast<UpdatableComponent*>(components_[i]);
5365  ans += uc->NumParameters();
5366  }
5367  }
5368  return ans;
5369 }
std::vector< Component * > components_
virtual int32 Properties() const
Return bitmask of the component&#39;s properties.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

◆ OutputDim()

int32 OutputDim ( ) const
virtual

Returns output-dimension of this component.

Implements Component.

Definition at line 4970 of file nnet-simple-component.cc.

References KALDI_ASSERT.

4970  {
4971  KALDI_ASSERT(!components_.empty());
4972  return components_.back()->OutputDim();
4973 };
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

◆ PerturbParams()

void PerturbParams ( BaseFloat  stddev)
virtual

This function is to be used in testing.

It adds unit noise times "stddev" to the parameters of the component.

Implements UpdatableComponent.

Definition at line 5305 of file nnet-simple-component.cc.

References rnnlm::i, KALDI_ASSERT, kaldi::nnet3::kUpdatableComponent, UpdatableComponent::PerturbParams(), and MaxpoolingComponent::Properties().

5305  {
5306  KALDI_ASSERT(this->IsUpdatable()); // or should not be called.
5307  for (size_t i = 0; i < components_.size(); i++) {
5309  UpdatableComponent *uc =
5310  dynamic_cast<UpdatableComponent*>(components_[i]);
5311  uc->PerturbParams(stddev);
5312  }
5313  }
5314 }
std::vector< Component * > components_
virtual int32 Properties() const
Return bitmask of the component&#39;s properties.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

◆ Propagate()

void * Propagate ( const ComponentPrecomputedIndexes indexes,
const CuMatrixBase< BaseFloat > &  in,
CuMatrixBase< BaseFloat > *  out 
) const
virtual

Propagate function.

Parameters
[in]indexesA pointer to some information output by this class's PrecomputeIndexes function (will be NULL for simple components, i.e. those that don't do things like splicing).
[in]inThe input to this component. Num-columns == InputDim().
[out]outThe output of this component. Num-columns == OutputDim(). Note: output of this component will be added to the initial value of "out" if Properties()&kPropagateAdds != 0; otherwise the output will be set and the initial value ignored. Each Component chooses whether it is more convenient implementation-wise to add or set, and the calling code has to deal with it.
Returns
Normally returns NULL, but may return a non-NULL value for components which have the flag kUsesMemo set. This value will be passed into the corresponding Backprop routine.

Implements Component.

Definition at line 5010 of file nnet-simple-component.cc.

References rnnlm::i, MaxpoolingComponent::InputDim(), KALDI_ASSERT, kaldi::nnet3::kPropagateAdds, kaldi::kSetZero, kaldi::kUndefined, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), MaxpoolingComponent::OutputDim(), and MaxpoolingComponent::Propagate().

5013  {
5014  KALDI_ASSERT(in.NumRows() == out->NumRows() && in.NumCols() == InputDim() &&
5015  out->NumCols() == OutputDim());
5016  int32 num_rows = in.NumRows(),
5017  num_components = components_.size();
5018  if (max_rows_process_ > 0 && num_rows > max_rows_process_) {
5019  // recurse and process smaller parts of the data, to save memory.
5020  for (int32 row_offset = 0; row_offset < num_rows;
5021  row_offset += max_rows_process_) {
5022  int32 this_num_rows = std::min<int32>(max_rows_process_,
5023  num_rows - row_offset);
5024  const CuSubMatrix<BaseFloat> in_part(in, row_offset, this_num_rows,
5025  0, in.NumCols());
5026  CuSubMatrix<BaseFloat> out_part(*out, row_offset, this_num_rows,
5027  0, out->NumCols());
5028  this->Propagate(NULL, in_part, &out_part);
5029  }
5030  return NULL;
5031  }
5032  std::vector<CuMatrix<BaseFloat> > intermediate_outputs(num_components - 1);
5033  for (int32 i = 0; i < num_components; i++) {
5034  if (i + 1 < num_components) {
5035  MatrixResizeType resize_type =
5036  ((components_[i]->Properties() & kPropagateAdds) ?
5037  kSetZero : kUndefined);
5038  intermediate_outputs[i].Resize(num_rows, components_[i]->OutputDim(),
5039  resize_type, GetStrideType(i));
5040  }
5041  const CuMatrixBase<BaseFloat> &this_in = (i == 0 ? in :
5042  intermediate_outputs[i-1]);
5043  CuMatrixBase<BaseFloat> *this_out = (i + 1 == num_components ?
5044  out : &(intermediate_outputs[i]));
5045  void *memo = components_[i]->Propagate(NULL, this_in, this_out);
5046  // we'll re-do the forward propagation in the backprop, and we can
5047  // regenerate any memos there, so no need to keep them.
5048  if (memo != NULL)
5049  components_[i]->DeleteMemo(memo);
5050  if (i > 0)
5051  intermediate_outputs[i-1].Resize(0, 0);
5052  }
5053  return NULL;
5054 }
virtual int32 OutputDim() const
Returns output-dimension of this component.
MatrixResizeType
Definition: matrix-common.h:37
MatrixStrideType GetStrideType(int32 i) const
std::vector< Component * > components_
virtual int32 InputDim() const
Returns input-dimension of this component.
virtual void * Propagate(const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
Propagate function.
MatrixIndexT NumCols() const
Definition: cu-matrix.h:215
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:214

◆ Properties()

int32 Properties ( ) const
virtual

Return bitmask of the component's properties.

These properties depend only on the component's type. See enum ComponentProperties.

Implements Component.

Definition at line 4976 of file nnet-simple-component.cc.

References KALDI_ASSERT, kaldi::nnet3::kBackpropAdds, kaldi::nnet3::kBackpropNeedsInput, kaldi::nnet3::kBackpropNeedsOutput, kaldi::nnet3::kInputContiguous, kaldi::nnet3::kOutputContiguous, kaldi::nnet3::kPropagateAdds, kaldi::nnet3::kSimpleComponent, kaldi::nnet3::kStoresStats, and kaldi::nnet3::kUpdatableComponent.

4976  {
4977  KALDI_ASSERT(!components_.empty());
4978  int32 last_component_properties = components_.back()->Properties(),
4979  first_component_properties = components_.front()->Properties();
4980  // We always assume backprop needs the input, as this would be necessary to
4981  // get the activations at intermediate layers, if these were not needed in
4982  // backprop, there would be no reason to use a CompositeComponent.
4983  int32 ans = kSimpleComponent | kBackpropNeedsInput |
4984  (last_component_properties &
4986  (first_component_properties &
4988  (IsUpdatable() ? kUpdatableComponent : 0);
4989  // note, we don't return the kStoresStats property because that function is
4990  // not implemented; instead, for efficiency, we call StoreStats() on any
4991  // sub-components as part of the backprop phase.
4992  if (last_component_properties & kStoresStats)
4993  ans |= kBackpropNeedsOutput;
4994  return ans;
4995 };
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

◆ Read()

void Read ( std::istream &  is,
bool  binary 
)
virtual

Read function (used after we know the type of the Component); accepts input that is missing the token that describes the component type, in case it has already been consumed.

Implements Component.

Definition at line 5076 of file nnet-simple-component.cc.

References kaldi::nnet3::ExpectToken(), rnnlm::i, KALDI_ERR, kaldi::ReadBasicType(), Component::ReadNew(), and kaldi::ReadToken().

5076  {
5077  // Because we didn't previously write out the learning rate,
5078  // we need some temporary code.
5079  int32 max_rows_process;
5080  if (false) {
5081  ReadUpdatableCommon(is, binary);
5082  ExpectToken(is, binary, "<MaxRowsProcess>");
5083  ReadBasicType(is, binary, &max_rows_process);
5084  } else { // temporary code.
5085  std::string token;
5086  ReadToken(is, binary, &token);
5087  if (token == "<CompositeComponent>") {
5088  // if the first token is the opening tag, then
5089  // ignore it and get the next tag.
5090  ReadToken(is, binary, &token);
5091  }
5092  if (token == "<LearningRateFactor>") {
5093  ReadBasicType(is, binary, &learning_rate_factor_);
5094  ReadToken(is, binary, &token);
5095  } else {
5096  learning_rate_factor_ = 1.0;
5097  }
5098  if (token == "<IsGradient>") {
5099  ReadBasicType(is, binary, &is_gradient_);
5100  ReadToken(is, binary, &token);
5101  } else {
5102  is_gradient_ = false;
5103  }
5104  if (token == "<LearningRate>") {
5105  ReadBasicType(is, binary, &learning_rate_);
5106  ReadToken(is, binary, &token);
5107  }
5108  if (token != "<MaxRowsProcess>") {
5109  KALDI_ERR << "Expected token <MaxRowsProcess>, got "
5110  << token;
5111  }
5112  ReadBasicType(is, binary, &max_rows_process);
5113  }
5114  ExpectToken(is, binary, "<NumComponents>");
5115  int32 num_components;
5116  ReadBasicType(is, binary, &num_components); // Read dimension.
5117  if (num_components < 0 || num_components > 100000)
5118  KALDI_ERR << "Bad num-components";
5119  std::vector<Component*> components(num_components);
5120  for (int32 i = 0; i < num_components; i++)
5121  components[i] = ReadNew(is, binary);
5122  Init(components, max_rows_process);
5123  ExpectToken(is, binary, "</CompositeComponent>");
5124 }
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
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
static void ExpectToken(const std::string &token, const std::string &what_we_are_parsing, const std::string **next_token)
BaseFloat learning_rate_
learning rate (typically 0.0..0.01)
std::string ReadUpdatableCommon(std::istream &is, bool binary)
#define KALDI_ERR
Definition: kaldi-error.h:127
static Component * ReadNew(std::istream &is, bool binary)
Read component from stream (works out its type). Dies on error.
void Init(const std::vector< Component *> &components, int32 max_rows_process)
BaseFloat learning_rate_factor_
learning rate factor (normally 1.0, but can be set to another < value so that when < you call SetLear...
bool is_gradient_
True if this component is to be treated as a gradient rather than as parameters.

◆ Scale()

void Scale ( BaseFloat  scale)
virtual

This virtual function when called on – an UpdatableComponent scales the parameters by "scale" when called by an UpdatableComponent.

– a Nonlinear component (or another component that stores stats, like BatchNormComponent)– it relates to scaling activation stats, not parameters. Otherwise it will normally do nothing.

Reimplemented from Component.

Definition at line 5289 of file nnet-simple-component.cc.

References rnnlm::i, and Component::Scale().

5289  {
5290  for (size_t i = 0; i < components_.size(); i++)
5291  components_[i]->Scale(scale);
5292 }
std::vector< Component * > components_
virtual void Scale(BaseFloat scale)
This virtual function when called on – an UpdatableComponent scales the parameters by "scale" when c...

◆ SetActualLearningRate()

void SetActualLearningRate ( BaseFloat  lrate)
virtual

Sets the learning rate directly, bypassing learning_rate_factor_.

Reimplemented from UpdatableComponent.

Definition at line 5332 of file nnet-simple-component.cc.

References rnnlm::i, KALDI_ASSERT, kaldi::nnet3::kUpdatableComponent, MaxpoolingComponent::Properties(), and UpdatableComponent::SetActualLearningRate().

5332  {
5333  KALDI_ASSERT(this->IsUpdatable()); // or should not be called.
5335  for (size_t i = 0; i < components_.size(); i++) {
5337  UpdatableComponent *uc =
5338  dynamic_cast<UpdatableComponent*>(components_[i]);
5339  uc->SetActualLearningRate(lrate);
5340  }
5341  }
5342 }
std::vector< Component * > components_
virtual void SetActualLearningRate(BaseFloat lrate)
Sets the learning rate directly, bypassing learning_rate_factor_.
virtual int32 Properties() const
Return bitmask of the component&#39;s properties.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

◆ SetAsGradient()

void SetAsGradient ( )
virtual

Sets is_gradient_ to true and sets learning_rate_ to 1, ignoring learning_rate_factor_.

Reimplemented from UpdatableComponent.

Definition at line 5345 of file nnet-simple-component.cc.

References rnnlm::i, KALDI_ASSERT, kaldi::nnet3::kUpdatableComponent, MaxpoolingComponent::Properties(), and UpdatableComponent::SetAsGradient().

5345  {
5346  KALDI_ASSERT(this->IsUpdatable()); // or should not be called.
5348  for (size_t i = 0; i < components_.size(); i++) {
5350  UpdatableComponent *uc =
5351  dynamic_cast<UpdatableComponent*>(components_[i]);
5352  uc->SetAsGradient();
5353  }
5354  }
5355 }
std::vector< Component * > components_
virtual void SetAsGradient()
Sets is_gradient_ to true and sets learning_rate_ to 1, ignoring learning_rate_factor_.
virtual int32 Properties() const
Return bitmask of the component&#39;s properties.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

◆ SetComponent()

void SetComponent ( int32  i,
Component component 
)

Sets the ith component.

After this call, CompositeComponent owns the reference to the argument component. Frees the previous ith component.

Definition at line 5510 of file nnet-simple-component.cc.

References rnnlm::i, and KALDI_ASSERT.

Referenced by kaldi::nnet3::ConvertRepeatedToBlockAffine().

5510  {
5511  KALDI_ASSERT(static_cast<size_t>(i) < components_.size());
5512  delete components_[i];
5513  components_[i] = component;
5514 }
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

◆ SetUnderlyingLearningRate()

void SetUnderlyingLearningRate ( BaseFloat  lrate)
virtual

Sets the learning rate of gradient descent- gets multiplied by learning_rate_factor_.

Reimplemented from UpdatableComponent.

Definition at line 5316 of file nnet-simple-component.cc.

References rnnlm::i, KALDI_ASSERT, kaldi::nnet3::kUpdatableComponent, MaxpoolingComponent::Properties(), and UpdatableComponent::SetUnderlyingLearningRate().

5316  {
5317  KALDI_ASSERT(this->IsUpdatable()); // or should not be called.
5319 
5320  // apply any learning-rate-factor that's set at this level (ill-advised, but
5321  // we'll do it.)
5322  BaseFloat effective_lrate = LearningRate();
5323  for (size_t i = 0; i < components_.size(); i++) {
5325  UpdatableComponent *uc =
5326  dynamic_cast<UpdatableComponent*>(components_[i]);
5327  uc->SetUnderlyingLearningRate(effective_lrate);
5328  }
5329  }
5330 }
float BaseFloat
Definition: kaldi-types.h:29
std::vector< Component * > components_
virtual int32 Properties() const
Return bitmask of the component&#39;s properties.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
virtual void SetUnderlyingLearningRate(BaseFloat lrate)
Sets the learning rate of gradient descent- gets multiplied by learning_rate_factor_.
BaseFloat LearningRate() const
Gets the learning rate to be used in gradient descent.

◆ Type()

virtual std::string Type ( ) const
inlinevirtual

◆ UnVectorize()

void UnVectorize ( const VectorBase< BaseFloat > &  params)
virtual

Converts the parameters from vector form.

Reimplemented from UpdatableComponent.

Definition at line 5389 of file nnet-simple-component.cc.

References VectorBase< Real >::Dim(), rnnlm::i, KALDI_ASSERT, kaldi::nnet3::kUpdatableComponent, UpdatableComponent::NumParameters(), MaxpoolingComponent::Properties(), and UpdatableComponent::UnVectorize().

5389  {
5390  int32 cur_offset = 0;
5391  KALDI_ASSERT(this->IsUpdatable()); // or should not be called.
5392  for (size_t i = 0; i < components_.size(); i++) {
5394  UpdatableComponent *uc =
5395  dynamic_cast<UpdatableComponent*>(components_[i]);
5396  int32 this_size = uc->NumParameters();
5397  SubVector<BaseFloat> params_range(params, cur_offset, this_size);
5398  uc->UnVectorize(params_range);
5399  cur_offset += this_size;
5400  }
5401  }
5402  KALDI_ASSERT(cur_offset == params.Dim());
5403 }
std::vector< Component * > components_
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:63
virtual int32 Properties() const
Return bitmask of the component&#39;s properties.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

◆ Vectorize()

void Vectorize ( VectorBase< BaseFloat > *  params) const
virtual

Turns the parameters into vector form.

We put the vector form on the CPU, because in the kinds of situations where we do this, we'll tend to use too much memory for the GPU.

Reimplemented from UpdatableComponent.

Definition at line 5372 of file nnet-simple-component.cc.

References VectorBase< Real >::Dim(), rnnlm::i, KALDI_ASSERT, kaldi::nnet3::kUpdatableComponent, UpdatableComponent::NumParameters(), MaxpoolingComponent::Properties(), and UpdatableComponent::Vectorize().

5372  {
5373  int32 cur_offset = 0;
5374  KALDI_ASSERT(this->IsUpdatable()); // or should not be called.
5375  for (size_t i = 0; i < components_.size(); i++) {
5377  UpdatableComponent *uc =
5378  dynamic_cast<UpdatableComponent*>(components_[i]);
5379  int32 this_size = uc->NumParameters();
5380  SubVector<BaseFloat> params_range(*params, cur_offset, this_size);
5381  uc->Vectorize(&params_range);
5382  cur_offset += this_size;
5383  }
5384  }
5385  KALDI_ASSERT(cur_offset == params->Dim());
5386 }
std::vector< Component * > components_
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:63
virtual int32 Properties() const
Return bitmask of the component&#39;s properties.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

◆ Write()

void Write ( std::ostream &  os,
bool  binary 
) const
virtual

Write component to stream.

Implements Component.

Definition at line 5136 of file nnet-simple-component.cc.

References rnnlm::i, MaxpoolingComponent::Write(), kaldi::WriteBasicType(), and kaldi::WriteToken().

5136  {
5137  WriteUpdatableCommon(os, binary); // Write opening tag and learning rate.
5138  WriteToken(os, binary, "<MaxRowsProcess>");
5139  WriteBasicType(os, binary, max_rows_process_);
5140  WriteToken(os, binary, "<NumComponents>");
5141  int32 num_components = components_.size();
5142  WriteBasicType(os, binary, num_components);
5143  for (int32 i = 0; i < num_components; i++)
5144  components_[i]->Write(os, binary);
5145  WriteToken(os, binary, "</CompositeComponent>");
5146 }
std::vector< Component * > components_
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 WriteUpdatableCommon(std::ostream &is, bool binary) const
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
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.

◆ ZeroStats()

void ZeroStats ( )
virtual

Components that provide an implementation of StoreStats should also provide an implementation of ZeroStats(), to set those stats to zero.

Other components that store other types of statistics (e.g. regarding gradient clipping) should implement ZeroStats() also.

Reimplemented from Component.

Definition at line 5127 of file nnet-simple-component.cc.

References rnnlm::i, and Component::ZeroStats().

5127  {
5128  // we call ZeroStats() on all components without checking their flags; this
5129  // will do nothing if the component doesn't store stats. (components like
5130  // ReLU and sigmoid and tanh store stats on activations).
5131  for (size_t i = 0; i < components_.size(); i++)
5132  components_[i]->ZeroStats();
5133 }
virtual void ZeroStats()
Components that provide an implementation of StoreStats should also provide an implementation of Zero...
std::vector< Component * > components_

Member Data Documentation

◆ components_

std::vector<Component*> components_
private

◆ max_rows_process_

int32 max_rows_process_
private

Definition at line 2565 of file nnet-simple-component.h.


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