All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
ComponentPrecomputedIndexes
PrecomputeIndexes (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 2342 of file nnet-simple-component.h.

Constructor & Destructor Documentation

CompositeComponent ( )
inline

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

Referenced by CompositeComponent::Copy().

2353 { } // use Init() or InitFromConfig() to really initialize.
virtual ~CompositeComponent ( )
inlinevirtual

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

References CompositeComponent::components_, and kaldi::DeletePointers().

std::vector< Component * > components_
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

Member Function Documentation

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 5178 of file nnet-simple-component.cc.

References CompositeComponent::components_, rnnlm::i, and KALDI_ASSERT.

5178  {
5179  const CompositeComponent *other = dynamic_cast<const CompositeComponent*>(
5180  &other_in);
5181  KALDI_ASSERT(other != NULL && other->components_.size() ==
5182  components_.size() && "Mismatching nnet topologies");
5183  for (size_t i = 0; i < components_.size(); i++)
5184  components_[i]->Add(alpha, *(other->components_[i]));
5185 }
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
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 5033 of file nnet-simple-component.cc.

References CompositeComponent::components_, CompositeComponent::GetStrideType(), rnnlm::i, CompositeComponent::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, CompositeComponent::max_rows_process_, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), CompositeComponent::OutputDim(), CompositeComponent::Properties(), and Component::StoreStats().

5040  {
5041  KALDI_ASSERT(in_value.NumRows() == out_deriv.NumRows() &&
5042  in_value.NumCols() == InputDim() &&
5043  out_deriv.NumCols() == OutputDim());
5044  int32 num_rows = in_value.NumRows(),
5045  num_components = components_.size();
5046  if (max_rows_process_ > 0 && num_rows > max_rows_process_) {
5048  // recurse and process smaller parts of the data, to save memory.
5049  for (int32 row_offset = 0; row_offset < num_rows;
5050  row_offset += max_rows_process_) {
5051  bool have_output_value = (out_value.NumRows() != 0);
5052  int32 this_num_rows = std::min<int32>(max_rows_process_,
5053  num_rows - row_offset);
5054  // out_value_part will only be used if out_value is nonempty; otherwise we
5055  // make it a submatrix of 'out_deriv' to avoid errors in the constructor.
5056  const CuSubMatrix<BaseFloat> out_value_part(have_output_value ? out_value : out_deriv,
5057  row_offset, this_num_rows,
5058  0, out_deriv.NumCols());
5059  // in_deriv_value_part will only be used if in_deriv != NULL; otherwise we
5060  // make it a submatrix of 'in_value' to avoid errors in the constructor.
5061  CuSubMatrix<BaseFloat> in_deriv_part(in_deriv != NULL ? *in_deriv : in_value,
5062  row_offset, this_num_rows,
5063  0, in_value.NumCols());
5064  CuSubMatrix<BaseFloat> in_value_part(in_value, row_offset, this_num_rows,
5065  0, in_value.NumCols());
5066  const CuSubMatrix<BaseFloat> out_deriv_part(out_deriv,
5067  row_offset, this_num_rows,
5068  0, out_deriv.NumCols());
5069  CuMatrix<BaseFloat> empty_mat;
5070  this->Backprop(debug_info, NULL, in_value_part,
5071  (have_output_value ? static_cast<const CuMatrixBase<BaseFloat>&>(out_value_part) :
5072  static_cast<const CuMatrixBase<BaseFloat>&>(empty_mat)),
5073  out_deriv_part, NULL, to_update,
5074  in_deriv != NULL ? &in_deriv_part : NULL);
5075  }
5076  return;
5077  }
5078  // For now, assume all intermediate values and derivatives need to be
5079  // computed. in_value and out_deriv will always be supplied.
5080 
5081  // intermediate_outputs[i] contains the output of component i.
5082  std::vector<CuMatrix<BaseFloat> > intermediate_outputs(num_components);
5083  // intermediate_derivs[i] contains the deriative at the output of component i.
5084  std::vector<CuMatrix<BaseFloat> > intermediate_derivs(num_components - 1);
5085 
5086  KALDI_ASSERT(memo == NULL);
5087  // note: only a very few components use memos, but we need to support them.
5088  std::vector<void*> memos(num_components, NULL);
5089 
5090  int32 num_components_to_propagate = num_components;
5091  if (!(components_[num_components - 1]->Properties() & kUsesMemo)) {
5092  // we only need to propagate the very last component if it uses a memo.
5093  num_components_to_propagate--;
5094  if (num_components > 1) {
5095  // skip the last-but-one component's propagate if the last component's
5096  // backprop doesn't need the input and the last-but-one component's
5097  // backprop doesn't need the output. This is the lowest hanging fruit for
5098  // optimization; other propagates might also be skippable.
5099  int32 properties = components_[num_components - 2]->Properties(),
5100  next_properties = components_[num_components - 1]->Properties();
5101  if (!(properties & (kBackpropNeedsOutput || kUsesMemo)) &&
5102  !(next_properties & kBackpropNeedsInput)) {
5103  num_components_to_propagate--;
5104  }
5105  }
5106  }
5107 
5108 
5109  // Do the propagation again.
5110  for (int32 i = 0; i < num_components_to_propagate; i++) {
5111  MatrixResizeType resize_type =
5112  ((components_[i]->Properties() & kPropagateAdds) ?
5113  kSetZero : kUndefined);
5114  intermediate_outputs[i].Resize(num_rows, components_[i]->OutputDim(),
5115  resize_type, GetStrideType(i));
5116  memos[i] =
5117  components_[i]->Propagate(NULL,
5118  (i == 0 ? in_value : intermediate_outputs[i-1]),
5119  &(intermediate_outputs[i]));
5120  }
5121 
5122  for (int32 i = num_components - 1; i >= 0; i--) {
5123  const CuMatrixBase<BaseFloat> &this_in_value =
5124  (i == 0 ? in_value : intermediate_outputs[i-1]),
5125  &this_out_value =
5126  (i == num_components - 1 ? out_value : intermediate_outputs[i]);
5127 
5128  Component *component_to_update =
5129  (to_update == NULL ? NULL :
5130  dynamic_cast<CompositeComponent*>(to_update)->components_[i]);
5131 
5132  if (component_to_update != NULL &&
5134  component_to_update->StoreStats(this_in_value, this_out_value, memos[i]);
5135 
5136  if (i > 0) {
5137  MatrixResizeType resize_type =
5138  ((components_[i]->Properties() & kBackpropAdds) ?
5139  kSetZero : kUndefined);
5140  intermediate_derivs[i-1].Resize(num_rows, components_[i]->InputDim(),
5141  resize_type, GetStrideType(i - 1));
5142  }
5143  // skip the first component's backprop if it's not updatable and in_deriv is
5144  // not requested. Again, this is the lowest-hanging fruit to optimize.
5145  if (!(i == 0 && !(components_[0]->Properties() & kUpdatableComponent) &&
5146  in_deriv == NULL)) {
5147  components_[i]->Backprop(debug_info, NULL,
5148  this_in_value, this_out_value,
5149  (i + 1 == num_components ? out_deriv : intermediate_derivs[i]),
5150  memos[i], component_to_update,
5151  (i == 0 ? in_deriv : &(intermediate_derivs[i-1])));
5152  }
5153  if (memos[i] != NULL)
5154  components_[i]->DeleteMemo(memos[i]);
5155  }
5156 }
MatrixResizeType
Definition: matrix-common.h:35
MatrixIndexT NumCols() const
Definition: cu-matrix.h:206
virtual int32 Properties() const
Return bitmask of the component's properties.
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...
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:205
virtual int32 InputDim() const
Returns input-dimension of this component.
std::vector< Component * > components_
MatrixStrideType GetStrideType(int32 i) const
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
virtual int32 OutputDim() const
Returns output-dimension of this component.
Component * Copy ( ) const
virtual

Copies component (deep copy).

Implements Component.

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

References CompositeComponent::components_, CompositeComponent::CompositeComponent(), rnnlm::i, CompositeComponent::Init(), and CompositeComponent::max_rows_process_.

5322  {
5323  std::vector<Component*> components(components_.size());
5324  for (size_t i = 0; i < components_.size(); i++)
5325  components[i] = components_[i]->Copy();
5327  ans->Init(components, max_rows_process_);
5328  return ans;
5329 }
virtual Component * Copy() const
Copies component (deep copy).
std::vector< Component * > components_
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 5289 of file nnet-simple-component.cc.

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

5290  {
5291  const CompositeComponent *other = dynamic_cast<const CompositeComponent*>(
5292  &other_in);
5293  KALDI_ASSERT(other != NULL && other->components_.size() ==
5294  components_.size() && "Mismatching nnet topologies");
5295  BaseFloat ans = 0.0;
5296  for (size_t i = 0.0; i < components_.size(); i++) {
5298  UpdatableComponent *uc =
5299  dynamic_cast<UpdatableComponent*>(components_[i]);
5300  const UpdatableComponent *uc_other =
5301  dynamic_cast<UpdatableComponent*>(other->components_[i]);
5302  KALDI_ASSERT(uc != NULL && uc_other != NULL);
5303  ans += uc->DotProduct(*uc_other);
5304  }
5305  }
5306  return ans;
5307 }
virtual int32 Properties() const
Return bitmask of the component's properties.
float BaseFloat
Definition: kaldi-types.h:29
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void FreezeNaturalGradient ( bool  freeze)
virtual

virtual

Reimplemented from UpdatableComponent.

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

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

5310  {
5311  for (size_t i = 0; i < components_.size(); i++) {
5313  UpdatableComponent *uc =
5314  dynamic_cast<UpdatableComponent*>(components_[i]);
5315  KALDI_ASSERT(uc != NULL);
5316  uc->FreezeNaturalGradient(freeze);
5317  }
5318  }
5319 }
virtual int32 Properties() const
Return bitmask of the component's properties.
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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 5388 of file nnet-simple-component.cc.

References CompositeComponent::components_, rnnlm::i, and KALDI_ASSERT.

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

5388  {
5389  KALDI_ASSERT(static_cast<size_t>(i) < components_.size());
5390  return components_[i];
5391 }
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixStrideType GetStrideType ( int32  i) const
inlineprivate
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 5160 of file nnet-simple-component.cc.

References CompositeComponent::components_, rnnlm::i, and CompositeComponent::Type().

5160  {
5161  std::ostringstream stream;
5162  stream << Type() << " ";
5163  for (size_t i = 0; i < components_.size(); i++) {
5164  if (i > 0) stream << ", ";
5165  stream << "sub-component" << (i+1) << " = { "
5166  << components_[i]->Info() << " }";
5167  }
5168  return stream.str();
5169 }
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_
void Init ( const std::vector< Component * > &  components,
int32  max_rows_process 
)

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

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

Referenced by CompositeComponent::Copy(), CompositeComponent::InitFromConfig(), and CompositeComponent::Read().

4941  {
4942  DeletePointers(&components_); // clean up.
4943  components_ = components;
4944  KALDI_ASSERT(!components.empty());
4945  max_rows_process_ = max_rows_process;
4946 
4947  for (size_t i = 0; i < components_.size(); i++) {
4948  // make sure all constituent components are simple.
4950  if (i > 0) {
4951  // make sure all the internal dimensions match up.
4953  components_[i-1]->OutputDim());
4954  }
4955  }
4956 }
virtual int32 Properties() const
Return bitmask of the component's properties.
virtual int32 InputDim() const
Returns input-dimension of this component.
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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
virtual int32 OutputDim() const
Returns output-dimension of this component.
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 5333 of file nnet-simple-component.cc.

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

5333  {
5334  int32 max_rows_process = 4096, num_components = -1;
5335  cfl->GetValue("max-rows-process", &max_rows_process);
5336  if (!cfl->GetValue("num-components", &num_components) ||
5337  num_components < 1)
5338  KALDI_ERR << "Expected num-components to be defined in "
5339  << "CompositeComponent config line '" << cfl->WholeLine() << "'";
5340  std::vector<Component*> components;
5341  for (int32 i = 1; i <= num_components; i++) {
5342  std::ostringstream name_stream;
5343  name_stream << "component" << i;
5344  std::string component_config;
5345  if (!cfl->GetValue(name_stream.str(), &component_config)) {
5346  DeletePointers(&components);
5347  KALDI_ERR << "Expected '" << name_stream.str() << "' to be defined in "
5348  << "CompositeComponent config line '" << cfl->WholeLine() << "'";
5349  }
5350  ConfigLine nested_line;
5351  // note: the nested line may not contain comments.
5352  std::string component_type;
5353  Component *this_component = NULL;
5354  if (!nested_line.ParseLine(component_config) ||
5355  !nested_line.GetValue("type", &component_type) ||
5356  !(this_component = NewComponentOfType(component_type)) ||
5357  nested_line.FirstToken() != "") {
5358  DeletePointers(&components);
5359  KALDI_ERR << "Could not parse config line for '" << name_stream.str()
5360  << "(or undefined or bad component type [type=xxx]), in "
5361  << "CompositeComponent config line '" << cfl->WholeLine() << "'";
5362  }
5363  if(this_component->Type() == "CompositeComponent") {
5364  DeletePointers(&components);
5365  delete this_component;
5366  // This is not allowed. If memory is too much with just one
5367  // CompositeComponent, try decreasing max-rows-process instead.
5368  KALDI_ERR << "Found CompositeComponent nested within CompositeComponent."
5369  << "Nested line: '" << nested_line.WholeLine() << "'\n"
5370  << "Toplevel CompositeComponent line '" << cfl->WholeLine()
5371  << "'";
5372  }
5373  this_component->InitFromConfig(&nested_line);
5374  int32 props = this_component->Properties();
5375  if ((props & kRandomComponent) != 0 ||
5376  (props & kSimpleComponent) == 0) {
5377  KALDI_ERR << "CompositeComponent contains disallowed component type: "
5378  << nested_line.WholeLine();
5379  }
5380  components.push_back(this_component);
5381  }
5382  if (cfl->HasUnusedValues())
5383  KALDI_ERR << "Could not process these elements in initializer: "
5384  << cfl->UnusedValues();
5385  this->Init(components, max_rows_process);
5386 }
#define KALDI_ERR
Definition: kaldi-error.h:127
static Component * NewComponentOfType(const std::string &type)
Returns a new Component of the given type e.g.
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
void Init(const std::vector< Component * > &components, int32 max_rows_process)
int32 InputDim ( ) const
virtual

Returns input-dimension of this component.

Implements Component.

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

References CompositeComponent::components_, and KALDI_ASSERT.

Referenced by CompositeComponent::Backprop(), CompositeComponent::Init(), and CompositeComponent::Propagate().

4847  {
4848  KALDI_ASSERT(!components_.empty());
4849  return components_.front()->InputDim();
4850 };
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
bool IsUpdatable ( ) const
private
int32 NumComponents ( ) const
inline
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 5241 of file nnet-simple-component.cc.

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

5241  {
5242  KALDI_ASSERT(this->IsUpdatable()); // or should not be called.
5243  int32 ans = 0;
5244  for (size_t i = 0; i < components_.size(); i++) {
5246  UpdatableComponent *uc =
5247  dynamic_cast<UpdatableComponent*>(components_[i]);
5248  ans += uc->NumParameters();
5249  }
5250  }
5251  return ans;
5252 }
virtual int32 Properties() const
Return bitmask of the component's properties.
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
int32 OutputDim ( ) const
virtual

Returns output-dimension of this component.

Implements Component.

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

References CompositeComponent::components_, and KALDI_ASSERT.

Referenced by CompositeComponent::Backprop(), CompositeComponent::Init(), and CompositeComponent::Propagate().

4853  {
4854  KALDI_ASSERT(!components_.empty());
4855  return components_.back()->OutputDim();
4856 };
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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 5188 of file nnet-simple-component.cc.

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

5188  {
5189  KALDI_ASSERT(this->IsUpdatable()); // or should not be called.
5190  for (size_t i = 0; i < components_.size(); i++) {
5192  UpdatableComponent *uc =
5193  dynamic_cast<UpdatableComponent*>(components_[i]);
5194  uc->PerturbParams(stddev);
5195  }
5196  }
5197 }
virtual int32 Properties() const
Return bitmask of the component's properties.
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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 4893 of file nnet-simple-component.cc.

References CompositeComponent::components_, CompositeComponent::GetStrideType(), rnnlm::i, CompositeComponent::InputDim(), KALDI_ASSERT, kaldi::nnet3::kPropagateAdds, kaldi::kSetZero, kaldi::kUndefined, CompositeComponent::max_rows_process_, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), and CompositeComponent::OutputDim().

4896  {
4897  KALDI_ASSERT(in.NumRows() == out->NumRows() && in.NumCols() == InputDim() &&
4898  out->NumCols() == OutputDim());
4899  int32 num_rows = in.NumRows(),
4900  num_components = components_.size();
4901  if (max_rows_process_ > 0 && num_rows > max_rows_process_) {
4902  // recurse and process smaller parts of the data, to save memory.
4903  for (int32 row_offset = 0; row_offset < num_rows;
4904  row_offset += max_rows_process_) {
4905  int32 this_num_rows = std::min<int32>(max_rows_process_,
4906  num_rows - row_offset);
4907  const CuSubMatrix<BaseFloat> in_part(in, row_offset, this_num_rows,
4908  0, in.NumCols());
4909  CuSubMatrix<BaseFloat> out_part(*out, row_offset, this_num_rows,
4910  0, out->NumCols());
4911  this->Propagate(NULL, in_part, &out_part);
4912  }
4913  return NULL;
4914  }
4915  std::vector<CuMatrix<BaseFloat> > intermediate_outputs(num_components - 1);
4916  for (int32 i = 0; i < num_components; i++) {
4917  if (i + 1 < num_components) {
4918  MatrixResizeType resize_type =
4919  ((components_[i]->Properties() & kPropagateAdds) ?
4920  kSetZero : kUndefined);
4921  intermediate_outputs[i].Resize(num_rows, components_[i]->OutputDim(),
4922  resize_type, GetStrideType(i));
4923  }
4924  const CuMatrixBase<BaseFloat> &this_in = (i == 0 ? in :
4925  intermediate_outputs[i-1]);
4926  CuMatrixBase<BaseFloat> *this_out = (i + 1 == num_components ?
4927  out : &(intermediate_outputs[i]));
4928  void *memo = components_[i]->Propagate(NULL, this_in, this_out);
4929  // we'll re-do the forward propagation in the backprop, and we can
4930  // regenerate any memos there, so no need to keep them.
4931  if (memo != NULL)
4932  components_[i]->DeleteMemo(memo);
4933  if (i > 0)
4934  intermediate_outputs[i-1].Resize(0, 0);
4935  }
4936  return NULL;
4937 }
MatrixResizeType
Definition: matrix-common.h:35
MatrixIndexT NumCols() const
Definition: cu-matrix.h:206
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:205
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.
std::vector< Component * > components_
MatrixStrideType GetStrideType(int32 i) const
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
virtual int32 OutputDim() const
Returns output-dimension of this component.
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 4859 of file nnet-simple-component.cc.

References CompositeComponent::components_, CompositeComponent::IsUpdatable(), 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.

Referenced by CompositeComponent::Backprop(), CompositeComponent::DotProduct(), CompositeComponent::FreezeNaturalGradient(), CompositeComponent::GetStrideType(), CompositeComponent::Init(), CompositeComponent::NumParameters(), CompositeComponent::PerturbParams(), CompositeComponent::SetActualLearningRate(), CompositeComponent::SetAsGradient(), CompositeComponent::SetUnderlyingLearningRate(), CompositeComponent::UnVectorize(), and CompositeComponent::Vectorize().

4859  {
4860  KALDI_ASSERT(!components_.empty());
4861  int32 last_component_properties = components_.back()->Properties(),
4862  first_component_properties = components_.front()->Properties();
4863  // We always assume backprop needs the input, as this would be necessary to
4864  // get the activations at intermediate layers, if these were not needed in
4865  // backprop, there would be no reason to use a CompositeComponent.
4866  int32 ans = kSimpleComponent | kBackpropNeedsInput |
4867  (last_component_properties &
4869  (first_component_properties &
4871  (IsUpdatable() ? kUpdatableComponent : 0);
4872  // note, we don't return the kStoresStats property because that function is
4873  // not implemented; instead, for efficiency, we call StoreStats() on any
4874  // sub-components as part of the backprop phase.
4875  if (last_component_properties & kStoresStats)
4876  ans |= kBackpropNeedsOutput;
4877  return ans;
4878 };
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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 4959 of file nnet-simple-component.cc.

References kaldi::nnet3::ExpectToken(), rnnlm::i, CompositeComponent::Init(), UpdatableComponent::is_gradient_, KALDI_ERR, UpdatableComponent::learning_rate_, UpdatableComponent::learning_rate_factor_, kaldi::ReadBasicType(), Component::ReadNew(), kaldi::ReadToken(), and UpdatableComponent::ReadUpdatableCommon().

4959  {
4960  // Because we didn't previously write out the learning rate,
4961  // we need some temporary code.
4962  int32 max_rows_process;
4963  if (false) {
4964  ReadUpdatableCommon(is, binary);
4965  ExpectToken(is, binary, "<MaxRowsProcess>");
4966  ReadBasicType(is, binary, &max_rows_process);
4967  } else { // temporary code.
4968  std::string token;
4969  ReadToken(is, binary, &token);
4970  if (token == "<CompositeComponent>") {
4971  // if the first token is the opening tag, then
4972  // ignore it and get the next tag.
4973  ReadToken(is, binary, &token);
4974  }
4975  if (token == "<LearningRateFactor>") {
4976  ReadBasicType(is, binary, &learning_rate_factor_);
4977  ReadToken(is, binary, &token);
4978  } else {
4979  learning_rate_factor_ = 1.0;
4980  }
4981  if (token == "<IsGradient>") {
4982  ReadBasicType(is, binary, &is_gradient_);
4983  ReadToken(is, binary, &token);
4984  } else {
4985  is_gradient_ = false;
4986  }
4987  if (token == "<LearningRate>") {
4988  ReadBasicType(is, binary, &learning_rate_);
4989  ReadToken(is, binary, &token);
4990  }
4991  if (token != "<MaxRowsProcess>") {
4992  KALDI_ERR << "Expected token <MaxRowsProcess>, got "
4993  << token;
4994  }
4995  ReadBasicType(is, binary, &max_rows_process);
4996  }
4997  ExpectToken(is, binary, "<NumComponents>");
4998  int32 num_components;
4999  ReadBasicType(is, binary, &num_components); // Read dimension.
5000  if (num_components < 0 || num_components > 100000)
5001  KALDI_ERR << "Bad num-components";
5002  std::vector<Component*> components(num_components);
5003  for (int32 i = 0; i < num_components; i++)
5004  components[i] = ReadNew(is, binary);
5005  Init(components, max_rows_process);
5006  ExpectToken(is, binary, "</CompositeComponent>");
5007 }
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.
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.
void Init(const std::vector< Component * > &components, int32 max_rows_process)
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 5172 of file nnet-simple-component.cc.

References CompositeComponent::components_, and rnnlm::i.

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

Sets the learning rate directly, bypassing learning_rate_factor_.

Reimplemented from UpdatableComponent.

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

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

5215  {
5216  KALDI_ASSERT(this->IsUpdatable()); // or should not be called.
5218  for (size_t i = 0; i < components_.size(); i++) {
5220  UpdatableComponent *uc =
5221  dynamic_cast<UpdatableComponent*>(components_[i]);
5222  uc->SetActualLearningRate(lrate);
5223  }
5224  }
5225 }
virtual int32 Properties() const
Return bitmask of the component's properties.
std::vector< Component * > components_
virtual void SetActualLearningRate(BaseFloat lrate)
Sets the learning rate directly, bypassing learning_rate_factor_.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void SetAsGradient ( )
virtual

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

Reimplemented from UpdatableComponent.

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

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

5228  {
5229  KALDI_ASSERT(this->IsUpdatable()); // or should not be called.
5231  for (size_t i = 0; i < components_.size(); i++) {
5233  UpdatableComponent *uc =
5234  dynamic_cast<UpdatableComponent*>(components_[i]);
5235  uc->SetAsGradient();
5236  }
5237  }
5238 }
virtual int32 Properties() const
Return bitmask of the component's properties.
std::vector< Component * > components_
virtual void SetAsGradient()
Sets is_gradient_ to true and sets learning_rate_ to 1, ignoring learning_rate_factor_.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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 5393 of file nnet-simple-component.cc.

References CompositeComponent::components_, rnnlm::i, and KALDI_ASSERT.

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

5393  {
5394  KALDI_ASSERT(static_cast<size_t>(i) < components_.size());
5395  delete components_[i];
5396  components_[i] = component;
5397 }
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void SetUnderlyingLearningRate ( BaseFloat  lrate)
virtual

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

Reimplemented from UpdatableComponent.

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

References CompositeComponent::components_, rnnlm::i, CompositeComponent::IsUpdatable(), KALDI_ASSERT, kaldi::nnet3::kUpdatableComponent, UpdatableComponent::LearningRate(), CompositeComponent::Properties(), and UpdatableComponent::SetUnderlyingLearningRate().

5199  {
5200  KALDI_ASSERT(this->IsUpdatable()); // or should not be called.
5202 
5203  // apply any learning-rate-factor that's set at this level (ill-advised, but
5204  // we'll do it.)
5205  BaseFloat effective_lrate = LearningRate();
5206  for (size_t i = 0; i < components_.size(); i++) {
5208  UpdatableComponent *uc =
5209  dynamic_cast<UpdatableComponent*>(components_[i]);
5210  uc->SetUnderlyingLearningRate(effective_lrate);
5211  }
5212  }
5213 }
virtual int32 Properties() const
Return bitmask of the component's properties.
BaseFloat LearningRate() const
Gets the learning rate to be used in gradient descent.
float BaseFloat
Definition: kaldi-types.h:29
std::vector< Component * > components_
#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_.
virtual std::string Type ( ) const
inlinevirtual

Returns a string such as "SigmoidComponent", describing the type of the object.

Implements Component.

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

Referenced by CompositeComponent::Info().

2359 { return "CompositeComponent"; }
void UnVectorize ( const VectorBase< BaseFloat > &  params)
virtual

Converts the parameters from vector form.

Reimplemented from UpdatableComponent.

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

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

5272  {
5273  int32 cur_offset = 0;
5274  KALDI_ASSERT(this->IsUpdatable()); // or should not be called.
5275  for (size_t i = 0; i < components_.size(); i++) {
5277  UpdatableComponent *uc =
5278  dynamic_cast<UpdatableComponent*>(components_[i]);
5279  int32 this_size = uc->NumParameters();
5280  SubVector<BaseFloat> params_range(params, cur_offset, this_size);
5281  uc->UnVectorize(params_range);
5282  cur_offset += this_size;
5283  }
5284  }
5285  KALDI_ASSERT(cur_offset == params.Dim());
5286 }
virtual int32 Properties() const
Return bitmask of the component's properties.
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:63
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 5255 of file nnet-simple-component.cc.

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

5255  {
5256  int32 cur_offset = 0;
5257  KALDI_ASSERT(this->IsUpdatable()); // or should not be called.
5258  for (size_t i = 0; i < components_.size(); i++) {
5260  UpdatableComponent *uc =
5261  dynamic_cast<UpdatableComponent*>(components_[i]);
5262  int32 this_size = uc->NumParameters();
5263  SubVector<BaseFloat> params_range(*params, cur_offset, this_size);
5264  uc->Vectorize(&params_range);
5265  cur_offset += this_size;
5266  }
5267  }
5268  KALDI_ASSERT(cur_offset == params->Dim());
5269 }
virtual int32 Properties() const
Return bitmask of the component's properties.
std::vector< Component * > components_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:63
void Write ( std::ostream &  os,
bool  binary 
) const
virtual

Write component to stream.

Implements Component.

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

References CompositeComponent::components_, rnnlm::i, CompositeComponent::max_rows_process_, kaldi::WriteBasicType(), kaldi::WriteToken(), and UpdatableComponent::WriteUpdatableCommon().

5019  {
5020  WriteUpdatableCommon(os, binary); // Write opening tag and learning rate.
5021  WriteToken(os, binary, "<MaxRowsProcess>");
5022  WriteBasicType(os, binary, max_rows_process_);
5023  WriteToken(os, binary, "<NumComponents>");
5024  int32 num_components = components_.size();
5025  WriteBasicType(os, binary, num_components);
5026  for (int32 i = 0; i < num_components; i++)
5027  components_[i]->Write(os, binary);
5028  WriteToken(os, binary, "</CompositeComponent>");
5029 }
void WriteUpdatableCommon(std::ostream &is, bool binary) const
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
virtual void Write(std::ostream &os, bool binary) const
Write component to stream.
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
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 5010 of file nnet-simple-component.cc.

References CompositeComponent::components_, and rnnlm::i.

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

Member Data Documentation


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