ScaleAndOffsetComponent Class Reference

#include <nnet-simple-component.h>

Inheritance diagram for ScaleAndOffsetComponent:
Collaboration diagram for ScaleAndOffsetComponent:

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...
 
 ScaleAndOffsetComponent ()
 
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 > &, 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...
 
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 ConsolidateMemory ()
 This virtual function relates to memory management, and avoiding fragmentation. More...
 
 ScaleAndOffsetComponent (const ScaleAndOffsetComponent &other)
 
- 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)
 
virtual void FreezeNaturalGradient (bool freeze)
 freezes/unfreezes NaturalGradient updates, if applicable (to be overriden by components that use Natural Gradient). More...
 
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

void PropagateInternal (const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
 
void BackpropInternal (const std::string &debug_info, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, ScaleAndOffsetComponent *to_update, CuMatrixBase< BaseFloat > *in_deriv) const
 
BaseFloat Epsilon () const
 
void Update (const std::string &debug_info, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
 
const ScaleAndOffsetComponentoperator= (const ScaleAndOffsetComponent &other)
 

Private Attributes

int32 dim_
 
CuVector< BaseFloatscales_
 
CuVector< BaseFloatoffsets_
 
bool use_natural_gradient_
 
OnlineNaturalGradient scale_preconditioner_
 
OnlineNaturalGradient offset_preconditioner_
 

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 1853 of file nnet-simple-component.h.

Constructor & Destructor Documentation

◆ ScaleAndOffsetComponent() [1/2]

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

1861 { } // use Init to really initialize.

◆ ScaleAndOffsetComponent() [2/2]

ScaleAndOffsetComponent ( const ScaleAndOffsetComponent other)
explicit

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

2416  :
2417  UpdatableComponent(component),
2418  dim_(component.dim_),
2419  scales_(component.scales_),
2420  offsets_(component.offsets_),
2421  use_natural_gradient_(component.use_natural_gradient_),
2422  scale_preconditioner_(component.scale_preconditioner_),
2423  offset_preconditioner_(component.offset_preconditioner_) { }

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

References KALDI_ASSERT, PerElementOffsetComponent::offsets_, ScaleAndOffsetComponent::offsets_, and ScaleAndOffsetComponent::scales_.

2407  {
2408  const ScaleAndOffsetComponent *other =
2409  dynamic_cast<const ScaleAndOffsetComponent*>(&other_in);
2410  KALDI_ASSERT(other != NULL);
2411  scales_.AddVec(alpha, other->scales_);
2412  offsets_.AddVec(alpha, other->offsets_);
2413 }
#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 2487 of file nnet-simple-component.cc.

References ScaleAndOffsetComponent::BackpropInternal(), CuMatrixBase< Real >::Data(), ScaleAndOffsetComponent::dim_, KALDI_ASSERT, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), NVTX_RANGE, kaldi::SameDim(), kaldi::SameDimAndStride(), ScaleAndOffsetComponent::scales_, and CuMatrixBase< Real >::Stride().

2495  {
2496  NVTX_RANGE("ScaleAndOffsetComponent::Backprop");
2497  ScaleAndOffsetComponent *to_update =
2498  dynamic_cast<ScaleAndOffsetComponent*>(to_update_in);
2499 
2500  KALDI_ASSERT(SameDim(out_value, out_deriv));
2501 
2502  if (dim_ == scales_.Dim()) {
2503  BackpropInternal(debug_info, out_value, out_deriv,
2504  to_update, in_deriv);
2505  } else {
2506  KALDI_ASSERT(out_value.NumCols() == out_value.Stride() &&
2507  SameDimAndStride(out_value, out_deriv) &&
2508  (!in_deriv || SameDimAndStride(out_value, *in_deriv)));
2509  int32 multiple = dim_ / scales_.Dim(),
2510  num_rows = out_value.NumRows(),
2511  block_dim = scales_.Dim();
2512  CuSubMatrix<BaseFloat> out_value_rearranged(out_value.Data(),
2513  num_rows * multiple,
2514  block_dim, block_dim),
2515  out_deriv_rearranged(out_deriv.Data(), num_rows * multiple,
2516  block_dim, block_dim);
2517  if (in_deriv) {
2518  CuSubMatrix<BaseFloat> in_deriv_rearranged(in_deriv->Data(),
2519  num_rows * multiple,
2520  block_dim, block_dim);
2521  BackpropInternal(debug_info, out_value_rearranged,
2522  out_deriv_rearranged, to_update,
2523  &in_deriv_rearranged);
2524  } else {
2525  BackpropInternal(debug_info, out_value_rearranged,
2526  out_deriv_rearranged, to_update,
2527  NULL);
2528  }
2529  }
2530 }
kaldi::int32 int32
void BackpropInternal(const std::string &debug_info, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, ScaleAndOffsetComponent *to_update, CuMatrixBase< BaseFloat > *in_deriv) const
bool SameDim(const MatrixBase< Real > &M, const MatrixBase< Real > &N)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
#define NVTX_RANGE(name)
Definition: cu-common.h:143
bool SameDimAndStride(const CuMatrixBase< Real > &M, const CuMatrixBase< Real > &N)
Definition: cu-matrix.h:954

◆ BackpropInternal()

void BackpropInternal ( const std::string &  debug_info,
const CuMatrixBase< BaseFloat > &  out_value,
const CuMatrixBase< BaseFloat > &  out_deriv,
ScaleAndOffsetComponent to_update,
CuMatrixBase< BaseFloat > *  in_deriv 
) const
private

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

References CuMatrixBase< Real >::AddVecToRows(), CuMatrixBase< Real >::CopyFromMat(), CuMatrixBase< Real >::Data(), kaldi::cu::EnsureNonzero(), ScaleAndOffsetComponent::Epsilon(), CuVectorBase< Real >::InvertElements(), UpdatableComponent::is_gradient_, kaldi::kUndefined, UpdatableComponent::learning_rate_, CuMatrixBase< Real >::MulColsVec(), CuMatrixBase< Real >::MulElements(), ScaleAndOffsetComponent::offset_preconditioner_, ScaleAndOffsetComponent::offsets_, OnlineNaturalGradient::PreconditionDirections(), ScaleAndOffsetComponent::scale_preconditioner_, ScaleAndOffsetComponent::scales_, and ScaleAndOffsetComponent::use_natural_gradient_.

Referenced by ScaleAndOffsetComponent::Backprop().

2540  {
2541  if (to_update) {
2542  if (!to_update->use_natural_gradient_ || to_update->is_gradient_) {
2543  to_update->offsets_.AddRowSumMat(to_update->learning_rate_,
2544  out_deriv);
2545  } else {
2546  BaseFloat scale = 1.0;
2547  CuMatrix<BaseFloat> out_deriv_copy(out_deriv);
2548  to_update->offset_preconditioner_.PreconditionDirections(
2549  &out_deriv_copy, &scale);
2550  to_update->offsets_.AddRowSumMat(scale * to_update->learning_rate_,
2551  out_deriv_copy);
2552  }
2553  // The backprop actually needs the input to the component, not the output;
2554  // but we make the output available because in the common topologies that
2555  // will already be required for backprop-- it's for memory efficiency.
2556  CuMatrix<BaseFloat> in_value_reconstructed(out_value);
2557  int32 dim = scales_.Dim();
2558  CuVector<BaseFloat> scales_nonzero(dim, kUndefined);
2559  BaseFloat epsilon = Epsilon();
2560  cu::EnsureNonzero(scales_, epsilon, &scales_nonzero);
2561  scales_nonzero.InvertElements();
2562  in_value_reconstructed.AddVecToRows(-1.0, offsets_);
2563  // Actually scales_nonzero are now the inverses of the scales.
2564  in_value_reconstructed.MulColsVec(scales_nonzero);
2565  // OK, at this point in_value_reconstructed is the input to the component.
2566  // Multiply its elements by 'out_deriv' to get the derivatives
2567  // (for each frame) w.r.t. the scales.
2568  in_value_reconstructed.MulElements(out_deriv);
2569  BaseFloat scale = 1.0;
2570  if (to_update->use_natural_gradient_ && !to_update->is_gradient_) {
2571  to_update->scale_preconditioner_.PreconditionDirections(
2572  &in_value_reconstructed, &scale);
2573  }
2574  to_update->scales_.AddRowSumMat(scale * to_update->learning_rate_,
2575  in_value_reconstructed);
2576  }
2577  if (in_deriv) {
2578  if (in_deriv->Data() != out_deriv.Data())
2579  in_deriv->CopyFromMat(out_deriv);
2580  in_deriv->MulColsVec(scales_);
2581  }
2582 }
kaldi::int32 int32
float BaseFloat
Definition: kaldi-types.h:29
void EnsureNonzero(const CuMatrixBase< Real > &src, Real epsilon, CuMatrixBase< Real > *dest)
This function requires that src and dest have the same dimension and epsilon > 0. ...
Definition: cu-math.cc:209

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

References ScaleAndOffsetComponent::offset_preconditioner_, ScaleAndOffsetComponent::scale_preconditioner_, and OnlineNaturalGradient::Swap().

2584  {
2585  OnlineNaturalGradient temp_scale(scale_preconditioner_);
2586  scale_preconditioner_.Swap(&temp_scale);
2587  OnlineNaturalGradient temp_offset(offset_preconditioner_);
2588  offset_preconditioner_.Swap(&temp_offset);
2589 }
void Swap(OnlineNaturalGradient *other)

◆ Copy()

virtual Component* Copy ( ) const
inlinevirtual

Copies component (deep copy).

Implements Component.

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

References Component::Add(), kaldi::nnet3::DotProduct(), kaldi::nnet3::PerturbParams(), and Component::Scale().

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

References ScaleAndOffsetComponent::offsets_, ScaleAndOffsetComponent::scales_, and kaldi::VecVec().

2434  {
2435  const ScaleAndOffsetComponent *other =
2436  dynamic_cast<const ScaleAndOffsetComponent*>(&other_in);
2437  return VecVec(other->scales_, scales_) + VecVec(other->offsets_, offsets_);
2438 }
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
Definition: kaldi-vector.cc:37

◆ Epsilon()

BaseFloat Epsilon ( ) const
inlineprivate

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

Referenced by ScaleAndOffsetComponent::BackpropInternal(), and ScaleAndOffsetComponent::PropagateInternal().

1921 { return 1.0e-04; }

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

References PerElementOffsetComponent::dim_, UpdatableComponent::Info(), PerElementOffsetComponent::offsets_, and kaldi::nnet3::PrintParameterStats().

2321  {
2322  std::ostringstream stream;
2323  stream << UpdatableComponent::Info()
2324  << ", rank=" << scale_preconditioner_.GetRank();
2325  if (dim_ != scales_.Dim())
2326  stream << ", block-size=" << scales_.Dim();
2327  PrintParameterStats(stream, "scales", scales_, true);
2328  PrintParameterStats(stream, "offsets", offsets_, true);
2329  return stream.str();
2330 }
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

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

References PerElementOffsetComponent::dim_, ConfigLine::GetValue(), ConfigLine::HasUnusedValues(), UpdatableComponent::InitLearningRatesFromConfig(), KALDI_ERR, PerElementOffsetComponent::offsets_, ConfigLine::UnusedValues(), PerElementOffsetComponent::use_natural_gradient_, and ConfigLine::WholeLine().

2332  {
2333 
2335  if (!cfl->GetValue("dim", &dim_) || dim_ <= 0) {
2336  KALDI_ERR << "Dimension 'dim' must be specified and >0: "
2337  << cfl->WholeLine();
2338  }
2339  use_natural_gradient_ = true;
2340  cfl->GetValue("use-natural-gradient", &use_natural_gradient_);
2341  int32 block_dim = dim_,
2342  rank = 20;
2343  cfl->GetValue("block-dim", &block_dim);
2344  if (block_dim <= 0 || dim_ % block_dim != 0) {
2345  KALDI_ERR << "Invalid block-dim: " << cfl->WholeLine();
2346  }
2347  cfl->GetValue("rank", &rank);
2348  scales_.Resize(block_dim);
2349  scales_.Set(1.0);
2350  offsets_.Resize(block_dim);
2351  // offsets are all zero when initialized.
2352  if (cfl->HasUnusedValues())
2353  KALDI_ERR << "Could not process these elements in initializer: "
2354  << cfl->UnusedValues();
2357  // the update period can't be configured for now; we'll add an option if we
2358  // want to.
2361 }
void InitLearningRatesFromConfig(ConfigLine *cfl)
kaldi::int32 int32
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ InputDim()

virtual int32 InputDim ( ) const
inlinevirtual

Returns input-dimension of this component.

Implements Component.

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

Referenced by ConstantFunctionComponent::Info().

◆ NumParameters()

virtual int32 NumParameters ( ) const
inlinevirtual

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

Reimplemented from UpdatableComponent.

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

References Component::ConsolidateMemory().

1898 { return 2 * scales_.Dim(); }

◆ operator=()

const ScaleAndOffsetComponent& operator= ( const ScaleAndOffsetComponent other)
private

◆ OutputDim()

virtual int32 OutputDim ( ) const
inlinevirtual

Returns output-dimension of this component.

Implements Component.

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

References Component::Info(), and PnormComponent::InitFromConfig().

Referenced by ConstantFunctionComponent::Info().

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

References kaldi::kUndefined, ScaleAndOffsetComponent::offsets_, ScaleAndOffsetComponent::scales_, and CuVectorBase< Real >::SetRandn().

2425  {
2426  CuVector<BaseFloat> temp(scales_.Dim(), kUndefined);
2427  temp.SetRandn();
2428  scales_.AddVec(stddev, temp);
2429  temp.SetRandn();
2430  offsets_.AddVec(stddev, temp);
2431 }

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

References CuMatrixBase< Real >::Data(), ScaleAndOffsetComponent::dim_, KALDI_ASSERT, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), ScaleAndOffsetComponent::PropagateInternal(), kaldi::SameDimAndStride(), ScaleAndOffsetComponent::scales_, and CuMatrixBase< Real >::Stride().

2456  {
2457  if (dim_ == scales_.Dim()) {
2458  PropagateInternal(in, out);
2459  } else {
2460  int32 multiple = dim_ / scales_.Dim(),
2461  num_rows = in.NumRows(), block_dim = scales_.Dim();
2462  KALDI_ASSERT(in.NumCols() == in.Stride() &&
2463  SameDimAndStride(in, *out));
2464  // Reinterpret the data as matrices with more rows but fewer columns.
2465  CuSubMatrix<BaseFloat> in_rearranged(in.Data(), num_rows * multiple,
2466  block_dim, block_dim),
2467  out_rearranged(out->Data(), num_rows * multiple,
2468  block_dim, block_dim);
2469  PropagateInternal(in_rearranged, &out_rearranged);
2470  }
2471  return NULL;
2472 }
void PropagateInternal(const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
kaldi::int32 int32
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
bool SameDimAndStride(const CuMatrixBase< Real > &M, const CuMatrixBase< Real > &N)
Definition: cu-matrix.h:954

◆ PropagateInternal()

void PropagateInternal ( const CuMatrixBase< BaseFloat > &  in,
CuMatrixBase< BaseFloat > *  out 
) const
private

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

References CuMatrixBase< Real >::AddVecToRows(), CuMatrixBase< Real >::CopyFromMat(), CuMatrixBase< Real >::Data(), kaldi::cu::EnsureNonzero(), ScaleAndOffsetComponent::Epsilon(), kaldi::kUndefined, CuMatrixBase< Real >::MulColsVec(), ScaleAndOffsetComponent::offsets_, and ScaleAndOffsetComponent::scales_.

Referenced by ScaleAndOffsetComponent::Propagate().

2476  {
2477  if (out->Data() != in.Data())
2478  out->CopyFromMat(in);
2479  BaseFloat epsilon = Epsilon();
2480  int32 dim = scales_.Dim();
2481  CuVector<BaseFloat> scales_nonzero(dim, kUndefined);
2482  cu::EnsureNonzero(scales_, epsilon, &scales_nonzero);
2483  out->MulColsVec(scales_nonzero);
2484  out->AddVecToRows(1.0, offsets_);
2485 }
kaldi::int32 int32
float BaseFloat
Definition: kaldi-types.h:29
void EnsureNonzero(const CuMatrixBase< Real > &src, Real epsilon, CuMatrixBase< Real > *dest)
This function requires that src and dest have the same dimension and epsilon > 0. ...
Definition: cu-math.cc:209

◆ Properties()

virtual int32 Properties ( ) const
inlinevirtual

Return bitmask of the component's properties.

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

Implements Component.

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

References PnormComponent::Backprop(), kaldi::nnet3::kBackpropInPlace, kaldi::nnet3::kBackpropNeedsOutput, kaldi::nnet3::kInputContiguous, kaldi::nnet3::kOutputContiguous, kaldi::nnet3::kPropagateInPlace, kaldi::nnet3::kSimpleComponent, kaldi::nnet3::kUpdatableComponent, PnormComponent::Propagate(), PnormComponent::Read(), and PnormComponent::Write().

1863  {
1864  // Note: the backprop would most naturally consume the input, but we
1865  // have arranged things so that the backprop consumes the output value
1866  // instead; this allows less memory use, since in typical configurations,
1867  // this will be followed by an affine component which needs its input
1868  // for the backprop (so requiring it to be present adds no extra
1869  // burden).
1873  (dim_ != scales_.Dim() ?
1875  }

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

References PerElementOffsetComponent::dim_, kaldi::nnet3::ExpectToken(), PerElementOffsetComponent::offsets_, kaldi::ReadBasicType(), UpdatableComponent::ReadUpdatableCommon(), and PerElementOffsetComponent::use_natural_gradient_.

2363  {
2364  ReadUpdatableCommon(is, binary); // Read opening tag and learning rate
2365  ExpectToken(is, binary, "<Dim>");
2366  ReadBasicType(is, binary, &dim_);
2367  ExpectToken(is, binary, "<Scales>");
2368  scales_.Read(is, binary);
2369  ExpectToken(is, binary, "<Offsets>");
2370  offsets_.Read(is, binary);
2371  ExpectToken(is, binary, "<UseNaturalGradient>");
2372  ReadBasicType(is, binary, &use_natural_gradient_);
2373  int32 rank;
2374  ExpectToken(is, binary, "<Rank>");
2375  ReadBasicType(is, binary, &rank);
2378  ExpectToken(is, binary, "</ScaleAndOffsetComponent>");
2379 }
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
kaldi::int32 int32
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)

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

References PerElementOffsetComponent::offsets_.

2396  {
2397  if (scale == 0.0) {
2398  scales_.SetZero();
2399  offsets_.SetZero();
2400  } else {
2401  scales_.Scale(scale);
2402  offsets_.Scale(scale);
2403  }
2404 }

◆ Type()

virtual std::string Type ( ) const
inlinevirtual

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

Implements Component.

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

Referenced by ConstantFunctionComponent::Info().

1862 { return "ScaleAndOffsetComponent"; }

◆ UnVectorize()

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

Converts the parameters from vector form.

Reimplemented from UpdatableComponent.

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

References ScaleAndOffsetComponent::offsets_, VectorBase< Real >::Range(), and ScaleAndOffsetComponent::scales_.

2447  {
2448  int32 dim = scales_.Dim();
2449  scales_.CopyFromVec(params.Range(0, dim));
2450  offsets_.CopyFromVec(params.Range(dim, dim));
2451 }
kaldi::int32 int32

◆ Update()

void Update ( const std::string &  debug_info,
const CuMatrixBase< BaseFloat > &  in_value,
const CuMatrixBase< BaseFloat > &  out_deriv 
)
private

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

References ScaleAndOffsetComponent::offsets_, VectorBase< Real >::Range(), and ScaleAndOffsetComponent::scales_.

2440  {
2441  int32 dim = scales_.Dim();
2442  params->Range(0, dim).CopyFromVec(scales_);
2443  params->Range(dim, dim).CopyFromVec(offsets_);
2444 }
kaldi::int32 int32

◆ Write()

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

Write component to stream.

Implements Component.

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

References PerElementOffsetComponent::dim_, PerElementOffsetComponent::offsets_, PerElementOffsetComponent::use_natural_gradient_, kaldi::WriteBasicType(), kaldi::WriteToken(), and UpdatableComponent::WriteUpdatableCommon().

2381  {
2382  WriteUpdatableCommon(os, binary); // Write opening tag and learning rate
2383  WriteToken(os, binary, "<Dim>");
2384  WriteBasicType(os, binary, dim_);
2385  WriteToken(os, binary, "<Scales>");
2386  scales_.Write(os, binary);
2387  WriteToken(os, binary, "<Offsets>");
2388  offsets_.Write(os, binary);
2389  WriteToken(os, binary, "<UseNaturalGradient>");
2391  WriteToken(os, binary, "<Rank>");
2393  WriteToken(os, binary, "</ScaleAndOffsetComponent>");
2394 }
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

Member Data Documentation

◆ dim_

◆ offset_preconditioner_

◆ offsets_

◆ scale_preconditioner_

◆ scales_

◆ use_natural_gradient_

bool use_natural_gradient_
private

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