LinearComponent Class Reference

#include <nnet-simple-component.h>

Inheritance diagram for LinearComponent:
Collaboration diagram for LinearComponent:

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 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 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...
 
 LinearComponent ()
 
void InitFromConfig (ConfigLine *cfl)
 Initialize, from a ConfigLine object. More...
 
virtual std::string Info () const
 Returns some text-form information about this component, for diagnostics. More...
 
virtual ComponentCopy () const
 Copies component (deep copy). 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)
 freezes/unfreezes NaturalGradient updates, if applicable (to be overriden by components that use Natural Gradient). More...
 
virtual void ConsolidateMemory ()
 This virtual function relates to memory management, and avoiding fragmentation. More...
 
 LinearComponent (const LinearComponent &other)
 
 LinearComponent (const CuMatrix< BaseFloat > &params)
 
BaseFloat OrthonormalConstraint () const
 
CuMatrixBase< BaseFloat > & Params ()
 
const CuMatrixBase< BaseFloat > & Params () const
 
- Public Member Functions inherited from UpdatableComponent
 UpdatableComponent (const UpdatableComponent &other)
 
 UpdatableComponent ()
 
virtual ~UpdatableComponent ()
 
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 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 ZeroStats ()
 Components that provide an implementation of StoreStats should also provide an implementation of ZeroStats(), to set those stats to zero. 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

LinearComponentoperator= (const LinearComponent &)
 

Private Attributes

CuMatrix< BaseFloatparams_
 
BaseFloat orthonormal_constraint_
 
bool use_natural_gradient_
 
OnlineNaturalGradient preconditioner_in_
 
OnlineNaturalGradient preconditioner_out_
 

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

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

Constructor & Destructor Documentation

◆ LinearComponent() [1/3]

◆ LinearComponent() [2/3]

LinearComponent ( const LinearComponent other)
explicit

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

3276  :
3277  UpdatableComponent(other),
3278  params_(other.params_),
3279  orthonormal_constraint_(other.orthonormal_constraint_),
3280  use_natural_gradient_(other.use_natural_gradient_),
3281  preconditioner_in_(other.preconditioner_in_),
3282  preconditioner_out_(other.preconditioner_out_) { }
OnlineNaturalGradient preconditioner_in_
OnlineNaturalGradient preconditioner_out_

◆ LinearComponent() [3/3]

LinearComponent ( const CuMatrix< BaseFloat > &  params)
explicit

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

References LinearComponent::preconditioner_in_, LinearComponent::preconditioner_out_, OnlineNaturalGradient::SetRank(), and OnlineNaturalGradient::SetUpdatePeriod().

3284  :
3285  params_(params),
3287  use_natural_gradient_(true) {
3288  // Set defaults for natural gradient.
3293  // the component-level defaults of alpha and num_samples_history, at 4.0 and
3294  // 2000.0, are the same as in the NaturalGradientOnline code, so there is no
3295  // need to set those here.
3296 }
OnlineNaturalGradient preconditioner_in_
OnlineNaturalGradient preconditioner_out_

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

References KALDI_ASSERT, and LinearComponent::params_.

3303  {
3304  const LinearComponent *other =
3305  dynamic_cast<const LinearComponent*>(&other_in);
3306  KALDI_ASSERT(other != NULL);
3307  params_.AddMat(alpha, other->params_);
3308 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

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

References CuMatrixBase< Real >::AddMatMat(), UpdatableComponent::is_gradient_, kaldi::kNoTrans, kaldi::kTrans, UpdatableComponent::learning_rate_, NVTX_RANGE, LinearComponent::params_, OnlineNaturalGradient::PreconditionDirections(), LinearComponent::preconditioner_in_, and LinearComponent::preconditioner_out_.

3238  {
3239  NVTX_RANGE("LinearComponent::Backprop");
3240  LinearComponent *to_update = dynamic_cast<LinearComponent*>(to_update_in);
3241 
3242  // Propagate the derivative back to the input. add with coefficient 1.0 since
3243  // property kBackpropAdds is true. If we wanted to add with coefficient 0.0
3244  // we'd need to zero the in_deriv, in case of infinities.
3245  if (in_deriv)
3246  in_deriv->AddMatMat(1.0, out_deriv, kNoTrans, params_, kNoTrans, 1.0);
3247 
3248  if (to_update != NULL) {
3249  if (!to_update->is_gradient_) {
3250  CuMatrix<BaseFloat> in_value_temp(in_value), out_deriv_temp(out_deriv);
3251  // These "scale" values get will get multiplied into the learning rate (faster
3252  // than having the matrices scaled inside the preconditioning code).
3253  BaseFloat in_scale, out_scale;
3254  to_update->preconditioner_in_.PreconditionDirections(&in_value_temp,
3255  &in_scale);
3256  to_update->preconditioner_out_.PreconditionDirections(&out_deriv_temp,
3257  &out_scale);
3258  BaseFloat local_lrate = in_scale * out_scale * to_update->learning_rate_;
3259 
3260  to_update->params_.AddMatMat(local_lrate, out_deriv_temp, kTrans,
3261  in_value_temp, kNoTrans, 1.0);
3262  } else {
3263  to_update->params_.AddMatMat(to_update->learning_rate_,
3264  out_deriv, kTrans,
3265  in_value, kNoTrans, 1.0);
3266  }
3267  }
3268 }
float BaseFloat
Definition: kaldi-types.h:29
#define NVTX_RANGE(name)
Definition: cu-common.h:143

◆ ConsolidateMemory()

void ConsolidateMemory ( )
virtual

This virtual function relates to memory management, and avoiding fragmentation.

It is called only once per model, after we do the first minibatch of training. The default implementation does nothing, but it can be overridden by child classes, where it may re-initialize certain quantities that may possibly have been allocated during the forward pass (e.g. certain statistics; OnlineNaturalGradient objects). We use our own CPU-based allocator (see cu-allocator.h) and since it can't do paging since we're not in control of the GPU page table, fragmentation can be a problem. The allocator always tries to put things in 'low-address memory' (i.e. at smaller memory addresses) near the beginning of the block it allocated, to avoid fragmentation; but if permanent things (belonging to the model) are allocated in the forward pass, they can permanently stay in high memory. This function helps to prevent that, by re-allocating those things into low-address memory (It's important that it's called after all the temporary buffers for the forward-backward have been freed, so that there is low-address memory available)).

Reimplemented from Component.

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

References LinearComponent::preconditioner_in_, LinearComponent::preconditioner_out_, and OnlineNaturalGradient::Swap().

3337  {
3338  OnlineNaturalGradient temp_in(preconditioner_in_);
3339  preconditioner_in_.Swap(&temp_in);
3340  OnlineNaturalGradient temp_out(preconditioner_out_);
3341  preconditioner_out_.Swap(&temp_out);
3342 }
OnlineNaturalGradient preconditioner_in_
void Swap(OnlineNaturalGradient *other)
OnlineNaturalGradient preconditioner_out_

◆ Copy()

Component * Copy ( ) const
virtual

Copies component (deep copy).

Implements Component.

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

3271  {
3272  return new LinearComponent(*this);
3273 }

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

References kaldi::kTrans, LinearComponent::params_, and kaldi::TraceMatMat().

3326  {
3327  const LinearComponent *other =
3328  dynamic_cast<const LinearComponent*>(&other_in);
3329  return TraceMatMat(params_, other->params_, kTrans);
3330 }
Real TraceMatMat(const MatrixBase< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType trans)
We need to declare this here as it will be a friend function.

◆ FreezeNaturalGradient()

void FreezeNaturalGradient ( bool  freeze)
virtual

freezes/unfreezes NaturalGradient updates, if applicable (to be overriden by components that use Natural Gradient).

Reimplemented from UpdatableComponent.

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

References OnlineNaturalGradient::Freeze(), LinearComponent::preconditioner_in_, and LinearComponent::preconditioner_out_.

3332  {
3333  preconditioner_in_.Freeze(freeze);
3334  preconditioner_out_.Freeze(freeze);
3335 }
OnlineNaturalGradient preconditioner_in_
OnlineNaturalGradient preconditioner_out_

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

References OnlineNaturalGradient::GetAlpha(), OnlineNaturalGradient::GetNumSamplesHistory(), OnlineNaturalGradient::GetRank(), OnlineNaturalGradient::GetUpdatePeriod(), kaldi::GetVerboseLevel(), UpdatableComponent::Info(), AffineComponent::orthonormal_constraint_, NaturalGradientAffineComponent::preconditioner_in_, NaturalGradientAffineComponent::preconditioner_out_, and kaldi::nnet3::PrintParameterStats().

3203  {
3204  std::ostringstream stream;
3205  stream << UpdatableComponent::Info();
3206  PrintParameterStats(stream, "params", params_,
3207  false, // include_mean
3208  true, // include_row_norms
3209  true, // include_column_norms
3210  GetVerboseLevel() >= 2); // include_singular_values
3211  if (orthonormal_constraint_ != 0.0)
3212  stream << ", orthonormal-constraint=" << orthonormal_constraint_;
3213  stream << ", use-natural-gradient="
3214  << (use_natural_gradient_ ? "true" : "false")
3215  << ", rank-in=" << preconditioner_in_.GetRank()
3216  << ", rank-out=" << preconditioner_out_.GetRank()
3217  << ", num-samples-history="
3219  << ", update-period=" << preconditioner_in_.GetUpdatePeriod()
3220  << ", alpha=" << preconditioner_in_.GetAlpha();
3221  return stream.str();
3222 }
int32 GetVerboseLevel()
Get verbosity level, usually set via command line &#39;–verbose=&#39; switch.
Definition: kaldi-error.h:60
OnlineNaturalGradient preconditioner_in_
virtual std::string Info() const
Returns some text-form information about this component, for diagnostics.
void PrintParameterStats(std::ostringstream &os, const std::string &name, const CuVectorBase< BaseFloat > &params, bool include_mean)
Print to &#39;os&#39; some information about the mean and standard deviation of some parameters, used in Info() functions in nnet-simple-component.cc.
Definition: nnet-parse.cc:157
OnlineNaturalGradient preconditioner_out_

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

References ConfigLine::GetValue(), ConfigLine::HasUnusedValues(), UpdatableComponent::InitLearningRatesFromConfig(), AffineComponent::InputDim(), UpdatableComponent::is_gradient_, KALDI_ASSERT, KALDI_ERR, AffineComponent::orthonormal_constraint_, AffineComponent::OutputDim(), NaturalGradientAffineComponent::preconditioner_in_, NaturalGradientAffineComponent::preconditioner_out_, kaldi::ReadKaldiObject(), OnlineNaturalGradient::SetAlpha(), OnlineNaturalGradient::SetNumSamplesHistory(), OnlineNaturalGradient::SetRank(), OnlineNaturalGradient::SetUpdatePeriod(), ConfigLine::UnusedValues(), and ConfigLine::WholeLine().

3108  {
3109  bool ok = true;
3110  std::string matrix_filename;
3111  is_gradient_ = false; // not configurable; there's no reason you'd want this
3112 
3114 
3115  int32 input_dim = -1, output_dim = -1;
3116  if (cfl->GetValue("matrix", &matrix_filename)) {
3117  ReadKaldiObject(matrix_filename, &params_); // will abort on failure.
3118  KALDI_ASSERT(params_.NumRows() != 0);
3119  if (cfl->GetValue("input-dim", &input_dim))
3120  KALDI_ASSERT(input_dim == InputDim() &&
3121  "input-dim mismatch vs. matrix.");
3122  if (cfl->GetValue("output-dim", &output_dim))
3123  KALDI_ASSERT(output_dim == OutputDim() &&
3124  "output-dim mismatch vs. matrix.");
3125  } else {
3126  ok = ok && cfl->GetValue("input-dim", &input_dim);
3127  ok = ok && cfl->GetValue("output-dim", &output_dim);
3128  if (!ok)
3129  KALDI_ERR << "Bad initializer " << cfl->WholeLine();
3130  BaseFloat param_stddev = 1.0 / std::sqrt(input_dim);
3131  cfl->GetValue("param-stddev", &param_stddev);
3132  params_.Resize(output_dim, input_dim);
3133  KALDI_ASSERT(output_dim > 0 && input_dim > 0 && param_stddev >= 0.0);
3134  params_.SetRandn(); // sets to random normally distributed noise.
3135  params_.Scale(param_stddev);
3136  }
3137  // Read various natural-gradient-related configs.
3138  int32 rank_in = -1, rank_out = -1, update_period = 4;
3139  BaseFloat alpha = 4.0,
3140  num_samples_history = 2000.0;
3141 
3142  use_natural_gradient_ = true;
3143 
3144  cfl->GetValue("num-samples-history", &num_samples_history);
3145  cfl->GetValue("alpha", &alpha);
3146  cfl->GetValue("rank-in", &rank_in);
3147  cfl->GetValue("rank-out", &rank_out);
3148  cfl->GetValue("update-period", &update_period);
3149  cfl->GetValue("use-natural-gradient", &use_natural_gradient_);
3150 
3151  if (rank_in < 0)
3152  rank_in = std::min<int32>(20, (InputDim() + 1) / 2);
3153  if (rank_out < 0)
3154  rank_out = std::min<int32>(80, (OutputDim() + 1) / 2);
3155 
3158  preconditioner_in_.SetRank(rank_in);
3159  preconditioner_out_.SetRank(rank_out);
3160  preconditioner_in_.SetNumSamplesHistory(num_samples_history);
3161  preconditioner_out_.SetNumSamplesHistory(num_samples_history);
3162  preconditioner_in_.SetUpdatePeriod(update_period);
3163  preconditioner_out_.SetUpdatePeriod(update_period);
3164 
3166  cfl->GetValue("orthonormal-constraint", &orthonormal_constraint_);
3167 
3168  if (cfl->HasUnusedValues())
3169  KALDI_ERR << "Could not process these elements in initializer: "
3170  << cfl->UnusedValues();
3171 }
void SetNumSamplesHistory(BaseFloat num_samples_history)
void InitLearningRatesFromConfig(ConfigLine *cfl)
kaldi::int32 int32
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
OnlineNaturalGradient preconditioner_in_
virtual int32 InputDim() const
Returns input-dimension of this component.
float BaseFloat
Definition: kaldi-types.h:29
#define KALDI_ERR
Definition: kaldi-error.h:147
bool is_gradient_
True if this component is to be treated as a gradient rather than as parameters.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
virtual int32 OutputDim() const
Returns output-dimension of this component.
OnlineNaturalGradient preconditioner_out_

◆ InputDim()

virtual int32 InputDim ( ) const
inlinevirtual

Returns input-dimension of this component.

Implements Component.

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

929 { return params_.NumCols(); }

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

References LinearComponent::params_.

Referenced by LinearComponent::UnVectorize(), and LinearComponent::Vectorize().

3315  {
3316  return params_.NumRows() * params_.NumCols();
3317 }

◆ operator=()

LinearComponent& operator= ( const LinearComponent )
private

◆ OrthonormalConstraint()

BaseFloat OrthonormalConstraint ( ) const
inline

◆ OutputDim()

virtual int32 OutputDim ( ) const
inlinevirtual

Returns output-dimension of this component.

Implements Component.

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

Referenced by ModelCollapser::GetDiagonallyPreModifiedComponentIndex().

930 { return params_.NumRows(); }

◆ Params() [1/2]

◆ Params() [2/2]

const CuMatrixBase<BaseFloat>& Params ( ) const
inline

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

973 { return params_; }

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

References LinearComponent::params_, and CuMatrixBase< Real >::SetRandn().

3310  {
3311  CuMatrix<BaseFloat> temp_params(params_);
3312  temp_params.SetRandn();
3313  params_.AddMat(stddev, temp_params);
3314 }

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

References CuMatrixBase< Real >::AddMatMat(), kaldi::kNoTrans, and kaldi::kTrans.

3226  {
3227  out->AddMatMat(1.0, in, kNoTrans, params_, kTrans, 1.0);
3228  return NULL;
3229 }

◆ Properties()

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

References kaldi::nnet3::ExpectToken(), KALDI_ASSERT, AffineComponent::orthonormal_constraint_, kaldi::PeekToken(), NaturalGradientAffineComponent::preconditioner_in_, NaturalGradientAffineComponent::preconditioner_out_, kaldi::ReadBasicType(), UpdatableComponent::ReadUpdatableCommon(), OnlineNaturalGradient::SetAlpha(), OnlineNaturalGradient::SetNumSamplesHistory(), OnlineNaturalGradient::SetRank(), and OnlineNaturalGradient::SetUpdatePeriod().

3069  {
3070  std::string token = ReadUpdatableCommon(is, binary);
3071  KALDI_ASSERT(token == "");
3072  ExpectToken(is, binary, "<Params>");
3073  params_.Read(is, binary);
3074  if (PeekToken(is, binary) == 'O') {
3075  ExpectToken(is, binary, "<OrthonormalConstraint>");
3076  ReadBasicType(is, binary, &orthonormal_constraint_);
3077  } else {
3079  }
3080  ExpectToken(is, binary, "<UseNaturalGradient>");
3081  ReadBasicType(is, binary, &use_natural_gradient_);
3082 
3083  // Read various natural-gradient-related configs.
3084  int32 rank_in, rank_out, update_period;
3085  BaseFloat alpha, num_samples_history;
3086  ExpectToken(is, binary, "<RankInOut>");
3087  ReadBasicType(is, binary, &rank_in);
3088  ReadBasicType(is, binary, &rank_out);
3089  ExpectToken(is, binary, "<Alpha>");
3090  ReadBasicType(is, binary, &alpha);
3091  ExpectToken(is, binary, "<NumSamplesHistory>");
3092  ReadBasicType(is, binary, &num_samples_history);
3093  ExpectToken(is, binary, "<UpdatePeriod>");
3094  ReadBasicType(is, binary, &update_period);
3095 
3098  preconditioner_in_.SetRank(rank_in);
3099  preconditioner_out_.SetRank(rank_out);
3100  preconditioner_in_.SetNumSamplesHistory(num_samples_history);
3101  preconditioner_out_.SetNumSamplesHistory(num_samples_history);
3102  preconditioner_in_.SetUpdatePeriod(update_period);
3103  preconditioner_out_.SetUpdatePeriod(update_period);
3104 
3105  ExpectToken(is, binary, "</LinearComponent>");
3106 }
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 SetNumSamplesHistory(BaseFloat num_samples_history)
kaldi::int32 int32
OnlineNaturalGradient preconditioner_in_
float BaseFloat
Definition: kaldi-types.h:29
static void ExpectToken(const std::string &token, const std::string &what_we_are_parsing, const std::string **next_token)
std::string ReadUpdatableCommon(std::istream &is, bool binary)
int PeekToken(std::istream &is, bool binary)
PeekToken will return the first character of the next token, or -1 if end of file.
Definition: io-funcs.cc:170
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
OnlineNaturalGradient preconditioner_out_

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

References LinearComponent::params_.

Referenced by ModelCollapser::GetScaledComponentIndex().

3298  {
3299  if (scale == 0.0) params_.SetZero();
3300  else params_.Scale(scale);
3301 }

◆ Type()

virtual std::string Type ( ) const
inlinevirtual

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

Implements Component.

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

Referenced by FixedAffineComponent::InitFromConfig().

932 { return "LinearComponent"; }

◆ UnVectorize()

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

Converts the parameters from vector form.

Reimplemented from UpdatableComponent.

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

References VectorBase< Real >::Dim(), KALDI_ASSERT, LinearComponent::NumParameters(), and LinearComponent::params_.

3322  {
3323  KALDI_ASSERT(params.Dim() == this->NumParameters());
3324  params_.CopyRowsFromVec(params);
3325 }
virtual int32 NumParameters() const
The following new virtual function returns the total dimension of the parameters in this class...
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

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

References VectorBase< Real >::CopyRowsFromMat(), VectorBase< Real >::Dim(), KALDI_ASSERT, LinearComponent::NumParameters(), and LinearComponent::params_.

3318  {
3319  KALDI_ASSERT(params->Dim() == this->NumParameters());
3320  params->CopyRowsFromMat(params_);
3321 }
virtual int32 NumParameters() const
The following new virtual function returns the total dimension of the parameters in this class...
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Write()

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

Write component to stream.

Implements Component.

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

References OnlineNaturalGradient::GetAlpha(), OnlineNaturalGradient::GetNumSamplesHistory(), OnlineNaturalGradient::GetRank(), OnlineNaturalGradient::GetUpdatePeriod(), AffineComponent::orthonormal_constraint_, NaturalGradientAffineComponent::preconditioner_in_, NaturalGradientAffineComponent::preconditioner_out_, kaldi::WriteBasicType(), kaldi::WriteToken(), and UpdatableComponent::WriteUpdatableCommon().

3175  {
3176  WriteUpdatableCommon(os, binary); // Write the opening tag and learning rate
3177  WriteToken(os, binary, "<Params>");
3178  params_.Write(os, binary);
3179  if (orthonormal_constraint_ != 0.0) {
3180  WriteToken(os, binary, "<OrthonormalConstraint>");
3182  }
3183  WriteToken(os, binary, "<UseNaturalGradient>");
3185 
3186  int32 rank_in = preconditioner_in_.GetRank(),
3187  rank_out = preconditioner_out_.GetRank(),
3188  update_period = preconditioner_in_.GetUpdatePeriod();
3190  num_samples_history = preconditioner_in_.GetNumSamplesHistory();
3191  WriteToken(os, binary, "<RankInOut>");
3192  WriteBasicType(os, binary, rank_in);
3193  WriteBasicType(os, binary, rank_out);
3194  WriteToken(os, binary, "<Alpha>");
3195  WriteBasicType(os, binary, alpha);
3196  WriteToken(os, binary, "<NumSamplesHistory>");
3197  WriteBasicType(os, binary, num_samples_history);
3198  WriteToken(os, binary, "<UpdatePeriod>");
3199  WriteBasicType(os, binary, update_period);
3200  WriteToken(os, binary, "</LinearComponent>");
3201 }
kaldi::int32 int32
OnlineNaturalGradient preconditioner_in_
float BaseFloat
Definition: kaldi-types.h:29
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
OnlineNaturalGradient preconditioner_out_

Member Data Documentation

◆ orthonormal_constraint_

BaseFloat orthonormal_constraint_
private

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

◆ params_

◆ preconditioner_in_

◆ preconditioner_out_

◆ use_natural_gradient_

bool use_natural_gradient_
private

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


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