All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ClipGradientComponent Class Reference

#include <nnet-simple-component.h>

Inheritance diagram for ClipGradientComponent:
Collaboration diagram for ClipGradientComponent:

Public Member Functions

 ClipGradientComponent (int32 dim, BaseFloat clipping_threshold, bool norm_based_clipping, BaseFloat self_repair_clipped_proportion_threshold, BaseFloat self_repair_target, BaseFloat self_repair_scale, int32 num_clipped, int32 count, int32 num_self_repaired, int32 num_backpropped)
 
 ClipGradientComponent ()
 
virtual int32 InputDim () const
 Returns input-dimension of this component. More...
 
virtual int32 OutputDim () const
 Returns output-dimension of this component. More...
 
virtual void InitFromConfig (ConfigLine *cfl)
 Initialize, from a ConfigLine object. More...
 
void Init (int32 dim, BaseFloat clipping_threshold, bool norm_based_clipping, BaseFloat self_repair_clipped_proportion_threshold, BaseFloat self_repair_target, BaseFloat self_repair_scale, int32 num_clipped, int32 count, int32 num_self_repaired, int32 num_backpropped)
 
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 ZeroStats ()
 Components that provide an implementation of StoreStats should also provide an implementation of ZeroStats(), to set those stats to zero. More...
 
virtual ComponentCopy () const
 Copies component (deep copy). 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 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 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 std::string Info () const
 Returns some text-form information about this component, for diagnostics. More...
 
virtual ~ClipGradientComponent ()
 
- 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 ()
 

Protected Attributes

int32 num_clipped_
 
int32 count_
 
int32 num_self_repaired_
 
int32 num_backpropped_
 

Private Member Functions

void RepairGradients (const std::string &debug_info, const CuMatrixBase< BaseFloat > &in_value, CuMatrixBase< BaseFloat > *in_deriv, ClipGradientComponent *to_update) const
 
ClipGradientComponentoperator= (const ClipGradientComponent &other)
 

Private Attributes

int32 dim_
 
BaseFloat clipping_threshold_
 
bool norm_based_clipping_
 
BaseFloat self_repair_clipped_proportion_threshold_
 
BaseFloat self_repair_target_
 
BaseFloat self_repair_scale_
 
std::string debug_info_
 

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...
 

Detailed Description

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

Constructor & Destructor Documentation

ClipGradientComponent ( int32  dim,
BaseFloat  clipping_threshold,
bool  norm_based_clipping,
BaseFloat  self_repair_clipped_proportion_threshold,
BaseFloat  self_repair_target,
BaseFloat  self_repair_scale,
int32  num_clipped,
int32  count,
int32  num_self_repaired,
int32  num_backpropped 
)
inline

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

References ClipGradientComponent::Init().

1224  {
1225  Init(dim, clipping_threshold, norm_based_clipping,
1226  self_repair_clipped_proportion_threshold,
1227  self_repair_target,
1228  self_repair_scale,
1229  num_clipped, count,
1230  num_self_repaired, num_backpropped);}
void Init(int32 dim, BaseFloat clipping_threshold, bool norm_based_clipping, BaseFloat self_repair_clipped_proportion_threshold, BaseFloat self_repair_target, BaseFloat self_repair_scale, int32 num_clipped, int32 count, int32 num_self_repaired, int32 num_backpropped)
const size_t count

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

Referenced by ClipGradientComponent::Copy().

virtual ~ClipGradientComponent ( )
inlinevirtual

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

References ClipGradientComponent::debug_info_, KALDI_LOG, ClipGradientComponent::num_backpropped_, and ClipGradientComponent::num_self_repaired_.

1290  {
1291  if (num_self_repaired_ > 0)
1292  KALDI_LOG << "ClipGradientComponent(node_name=" << debug_info_
1293  << ")'s self-repair was activated " << num_self_repaired_
1294  << " time(s) out of " << num_backpropped_
1295  << " times of calling Backprop() in this training job.";
1296  }
#define KALDI_LOG
Definition: kaldi-error.h:133

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

References ClipGradientComponent::count_, KALDI_ASSERT, and ClipGradientComponent::num_clipped_.

748  {
749  const ClipGradientComponent *other =
750  dynamic_cast<const ClipGradientComponent*>(&other_in);
751  KALDI_ASSERT(other != NULL);
752  count_ += alpha * other->count_;
753  num_clipped_ += alpha * other->num_clipped_;
754 }
#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 595 of file nnet-simple-component.cc.

References CuVectorBase< Real >::AddDiagMat2(), CuMatrixBase< Real >::ApplyCeiling(), CuMatrixBase< Real >::ApplyFloor(), ClipGradientComponent::clipping_threshold_, CuMatrixBase< Real >::CopyFromMat(), ClipGradientComponent::count_, kaldi::kNoTrans, CuMatrixBase< Real >::MulRowsVec(), ClipGradientComponent::norm_based_clipping_, ClipGradientComponent::num_backpropped_, ClipGradientComponent::num_clipped_, CuMatrixBase< Real >::NumRows(), and ClipGradientComponent::RepairGradients().

603  {
604  // the following statement will do nothing if in_deriv and out_deriv have same
605  // memory.
606  in_deriv->CopyFromMat(out_deriv);
607 
608  ClipGradientComponent *to_update =
609  dynamic_cast<ClipGradientComponent*>(to_update_in);
610 
611  if (clipping_threshold_ > 0) {
612  if (norm_based_clipping_) {
613  // each row in the derivative matrix, which corresponds to one sample in
614  // the mini-batch, is scaled to have a max-norm of clipping_threshold_
615  CuVector<BaseFloat> clipping_scales(in_deriv->NumRows());
616  clipping_scales.AddDiagMat2(pow(clipping_threshold_, -2), *in_deriv,
617  kNoTrans, 0.0);
618  // now clipping_scales contains the squared (norm of each row divided by
619  // clipping_threshold)
620  int32 num_not_scaled;
621  clipping_scales.ApplyFloor(1.0, &num_not_scaled);
622  // now clipping_scales contains min(1,
623  // squared-(norm/clipping_threshold))
624  if (num_not_scaled != clipping_scales.Dim()) {
625  clipping_scales.ApplyPow(-0.5);
626  // now clipping_scales contains max(1,
627  // clipping_threshold/vector_norm)
628  in_deriv->MulRowsVec(clipping_scales);
629  if (to_update != NULL)
630  to_update->num_clipped_ += (clipping_scales.Dim() - num_not_scaled);
631  }
632  if (to_update != NULL)
633  to_update->count_ += clipping_scales.Dim();
634  } else {
635  // each element of the derivative matrix, is clipped to be below the
636  // clipping_threshold_
638  in_deriv->ApplyFloor(-1 * clipping_threshold_);
639  }
640 
641  if (to_update != NULL) {
642  to_update->num_backpropped_ += 1;
643  RepairGradients(debug_info, in_value, in_deriv, to_update);
644  }
645  }
646 }
void ApplyCeiling(Real ceiling_val)
Definition: cu-matrix.cc:2541
void CopyFromMat(const MatrixBase< OtherReal > &src, MatrixTransposeType trans=kNoTrans)
Definition: cu-matrix.cc:339
void MulRowsVec(const CuVectorBase< Real > &scale)
scale i'th row by scale[i]
Definition: cu-matrix.cc:779
void ApplyFloor(Real floor_val)
Definition: cu-matrix.cc:2523
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:205
void RepairGradients(const std::string &debug_info, const CuMatrixBase< BaseFloat > &in_value, CuMatrixBase< BaseFloat > *in_deriv, ClipGradientComponent *to_update) const
virtual Component* Copy ( ) const
inlinevirtual

Copies component (deep copy).

Implements Component.

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

References ClipGradientComponent::ClipGradientComponent(), ClipGradientComponent::clipping_threshold_, ClipGradientComponent::count_, ClipGradientComponent::dim_, ClipGradientComponent::norm_based_clipping_, ClipGradientComponent::num_backpropped_, ClipGradientComponent::num_clipped_, ClipGradientComponent::num_self_repaired_, ClipGradientComponent::self_repair_clipped_proportion_threshold_, ClipGradientComponent::self_repair_scale_, and ClipGradientComponent::self_repair_target_.

1259  {
1260  return new ClipGradientComponent(dim_,
1266  num_clipped_,
1267  count_,
1269  num_backpropped_);}
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 Component.

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

References ClipGradientComponent::clipping_threshold_, ClipGradientComponent::count_, ClipGradientComponent::dim_, ClipGradientComponent::norm_based_clipping_, ClipGradientComponent::num_clipped_, ClipGradientComponent::self_repair_clipped_proportion_threshold_, ClipGradientComponent::self_repair_scale_, ClipGradientComponent::self_repair_target_, and ClipGradientComponent::Type().

517  {
518  std::ostringstream stream;
519  stream << Type() << ", dim=" << dim_
520  << ", norm-based-clipping="
521  << (norm_based_clipping_ ? "true" : "false")
522  << ", clipping-threshold=" << clipping_threshold_
523  << ", clipped-proportion="
524  << (count_ > 0 ? static_cast<BaseFloat>(num_clipped_)/count_ : 0);
525  if (self_repair_scale_ != 0.0)
526  stream << ", self-repair-clipped-proportion-threshold="
528  << ", self-repair-target=" << self_repair_target_
529  << ", self-repair-scale=" << self_repair_scale_;
530  return stream.str();
531 }
virtual std::string Type() const
Returns a string such as "SigmoidComponent", describing the type of the object.
void Init ( int32  dim,
BaseFloat  clipping_threshold,
bool  norm_based_clipping,
BaseFloat  self_repair_clipped_proportion_threshold,
BaseFloat  self_repair_target,
BaseFloat  self_repair_scale,
int32  num_clipped,
int32  count,
int32  num_self_repaired,
int32  num_backpropped 
)

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

References ClipGradientComponent::clipping_threshold_, count, ClipGradientComponent::count_, ClipGradientComponent::dim_, KALDI_ASSERT, ClipGradientComponent::norm_based_clipping_, ClipGradientComponent::num_backpropped_, ClipGradientComponent::num_clipped_, ClipGradientComponent::num_self_repaired_, ClipGradientComponent::self_repair_clipped_proportion_threshold_, ClipGradientComponent::self_repair_scale_, and ClipGradientComponent::self_repair_target_.

Referenced by ClipGradientComponent::ClipGradientComponent(), and ClipGradientComponent::InitFromConfig().

542  {
543  KALDI_ASSERT(clipping_threshold >= 0 && dim > 0 &&
544  self_repair_clipped_proportion_threshold >= 0.0 &&
545  self_repair_target >= 0.0 && self_repair_scale >= 0.0);
546  dim_ = dim;
547  norm_based_clipping_ = norm_based_clipping;
548  clipping_threshold_ = clipping_threshold;
550  self_repair_clipped_proportion_threshold;
551  self_repair_target_ = self_repair_target;
552  self_repair_scale_ = self_repair_scale;
553  num_clipped_ = num_clipped;
554  count_ = count;
555  num_self_repaired_ = num_self_repaired;
556  num_backpropped_ = num_backpropped;
557 }
const size_t count
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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 559 of file nnet-simple-component.cc.

References ConfigLine::GetValue(), ConfigLine::HasUnusedValues(), ClipGradientComponent::Init(), KALDI_ERR, ClipGradientComponent::Type(), and ConfigLine::WholeLine().

559  {
560  int32 dim = 0;
561  bool ok = cfl->GetValue("dim", &dim);
562  bool norm_based_clipping = false;
563  BaseFloat clipping_threshold = 15.0;
564  BaseFloat self_repair_clipped_proportion_threshold = 0.01;
565  BaseFloat self_repair_target = 0.0;
566  BaseFloat self_repair_scale = 1.0;
567  cfl->GetValue("clipping-threshold", &clipping_threshold);
568  cfl->GetValue("norm-based-clipping", &norm_based_clipping);
569  cfl->GetValue("self-repair-clipped-proportion-threshold",
570  &self_repair_clipped_proportion_threshold);
571  cfl->GetValue("self-repair-target",
572  &self_repair_target);
573  cfl->GetValue("self-repair-scale", &self_repair_scale);
574  if (!ok || cfl->HasUnusedValues() ||
575  clipping_threshold < 0 || dim <= 0 ||
576  self_repair_clipped_proportion_threshold < 0.0 ||
577  self_repair_target < 0.0 || self_repair_scale < 0.0)
578  KALDI_ERR << "Invalid initializer for layer of type "
579  << Type() << ": \"" << cfl->WholeLine() << "\"";
580  Init(dim, clipping_threshold, norm_based_clipping,
581  self_repair_clipped_proportion_threshold,
582  self_repair_target,
583  self_repair_scale, 0, 0, 0, 0);
584 }
void Init(int32 dim, BaseFloat clipping_threshold, bool norm_based_clipping, BaseFloat self_repair_clipped_proportion_threshold, BaseFloat self_repair_target, BaseFloat self_repair_scale, int32 num_clipped, int32 count, int32 num_self_repaired, int32 num_backpropped)
virtual std::string Type() const
Returns a string such as "SigmoidComponent", describing the type of the object.
float BaseFloat
Definition: kaldi-types.h:29
#define KALDI_ERR
Definition: kaldi-error.h:127
virtual int32 InputDim ( ) const
inlinevirtual

Returns input-dimension of this component.

Implements Component.

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

References ClipGradientComponent::dim_.

ClipGradientComponent& operator= ( const ClipGradientComponent other)
private
virtual int32 OutputDim ( ) const
inlinevirtual

Returns output-dimension of this component.

Implements Component.

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

References ClipGradientComponent::dim_.

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

References CuMatrixBase< Real >::CopyFromMat().

589  {
590  out->CopyFromMat(in);
591  return NULL;
592 }
void CopyFromMat(const MatrixBase< OtherReal > &src, MatrixTransposeType trans=kNoTrans)
Definition: cu-matrix.cc:339
virtual int32 Properties ( ) const
inlinevirtual
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 451 of file nnet-simple-component.cc.

References ClipGradientComponent::clipping_threshold_, ClipGradientComponent::count_, ClipGradientComponent::dim_, kaldi::nnet3::ExpectOneOrTwoTokens(), kaldi::nnet3::ExpectToken(), KALDI_ASSERT, ClipGradientComponent::norm_based_clipping_, ClipGradientComponent::num_backpropped_, ClipGradientComponent::num_clipped_, ClipGradientComponent::num_self_repaired_, kaldi::ReadBasicType(), kaldi::ReadToken(), ClipGradientComponent::self_repair_clipped_proportion_threshold_, ClipGradientComponent::self_repair_scale_, and ClipGradientComponent::self_repair_target_.

451  {
452  // might not see the "<NaturalGradientAffineComponent>" part because
453  // of how ReadNew() works.
454  ExpectOneOrTwoTokens(is, binary, "<ClipGradientComponent>",
455  "<Dim>");
456  ReadBasicType(is, binary, &dim_);
457  ExpectToken(is, binary, "<ClippingThreshold>");
458  ReadBasicType(is, binary, &clipping_threshold_);
459  ExpectToken(is, binary, "<NormBasedClipping>");
460  ReadBasicType(is, binary, &norm_based_clipping_);
461  std::string token;
462  ReadToken(is, binary, &token);
463  if (token == "<SelfRepairClippedProportionThreshold>") {
465  ExpectToken(is, binary, "<SelfRepairTarget>");
466  ReadBasicType(is, binary, &self_repair_target_);
467  ExpectToken(is, binary, "<SelfRepairScale>");
468  ReadBasicType(is, binary, &self_repair_scale_);
469  ExpectToken(is, binary, "<NumElementsClipped>");
470  } else {
472  self_repair_target_ = 0.0;
473  self_repair_scale_ = 0.0;
474  KALDI_ASSERT(token == "<NumElementsClipped>");
475  }
476  ReadBasicType(is, binary, &num_clipped_);
477  ExpectToken(is, binary, "<NumElementsProcessed>");
478  ReadBasicType(is, binary, &count_);
479  ReadToken(is, binary, &token);
480  if (token == "<NumSelfRepaired>") {
481  ReadBasicType(is, binary, &num_self_repaired_);
482  ExpectToken(is, binary, "<NumBackpropped>");
483  ReadBasicType(is, binary, &num_backpropped_);
484  ExpectToken(is, binary, "</ClipGradientComponent>");
485  } else {
486  num_self_repaired_ = 0;
487  num_backpropped_ = 0;
488  KALDI_ASSERT(token == "</ClipGradientComponent>");
489  }
490 }
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 ExpectOneOrTwoTokens(std::istream &is, bool binary, const std::string &token1, const std::string &token2)
This function is like ExpectToken but for two tokens, and it will either accept token1 and then token...
Definition: nnet-parse.cc:224
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)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void RepairGradients ( const std::string &  debug_info,
const CuMatrixBase< BaseFloat > &  in_value,
CuMatrixBase< BaseFloat > *  in_deriv,
ClipGradientComponent to_update 
) const
private

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

References CuMatrixBase< Real >::Add(), CuVectorBase< Real >::AddDiagMat2(), CuMatrixBase< Real >::AddMat(), CuMatrixBase< Real >::ApplyFloor(), CuMatrixBase< Real >::ApplyHeaviside(), CuMatrixBase< Real >::ApplyPowAbs(), ClipGradientComponent::count_, ClipGradientComponent::debug_info_, KALDI_ASSERT, KALDI_LOG, kaldi::kNoTrans, CuMatrixBase< Real >::MulElements(), ClipGradientComponent::num_backpropped_, ClipGradientComponent::num_clipped_, ClipGradientComponent::num_self_repaired_, CuMatrixBase< Real >::NumRows(), kaldi::RandUniform(), CuMatrixBase< Real >::Scale(), ClipGradientComponent::self_repair_clipped_proportion_threshold_, ClipGradientComponent::self_repair_scale_, and ClipGradientComponent::self_repair_target_.

Referenced by ClipGradientComponent::Backprop().

659  {
660  KALDI_ASSERT(to_update != NULL);
661 
662  // we use this 'repair_probability' (hardcoded for now) to limit
663  // this code to running on about half of the minibatches.
664  BaseFloat repair_probability = 0.5;
666  self_repair_scale_ == 0.0 || count_ == 0 ||
667  RandUniform() > repair_probability)
668  return;
669 
671 
672  BaseFloat clipped_proportion =
673  (count_ > 0 ? static_cast<BaseFloat>(num_clipped_) / count_ : 0);
674  // in-deriv would be modified only when clipped_proportion exceeds the
675  // threshold
676  if (clipped_proportion <= self_repair_clipped_proportion_threshold_)
677  return;
678 
679  to_update->num_self_repaired_ += 1;
680  if (to_update->debug_info_ == "") // get the component-node name
681  to_update->debug_info_ = debug_info;
682  if (to_update->num_self_repaired_ == 1)
683  KALDI_LOG << "ClipGradientComponent(node_name=" << debug_info
684  << ")'s self-repair was activated as the first time at the "
685  << to_update->num_backpropped_
686  << "-th call of Backprop() in this training job.";
687 
688  // sign_mat = sign(in_value), i.e.,
689  // An element in sign_mat is 1 if its corresponding element in in_value > 0,
690  // or -1 otherwise
691  CuMatrix<BaseFloat> sign_mat(in_value);
692  sign_mat.ApplyHeaviside();
693  sign_mat.Scale(2.0);
694  sign_mat.Add(-1.0);
695 
696  // repair_mat =
697  // floor(abs(in_value) - self_repair_target_, 0) .* sign(in_value)
698  CuMatrix<BaseFloat> repair_mat(in_value);
699  repair_mat.ApplyPowAbs(1.0);
700  repair_mat.Add(-self_repair_target_);
701  repair_mat.ApplyFloor(0.0);
702  repair_mat.MulElements(sign_mat);
703 
704  // magnitude =
705  // self_repair_scale_ * clipped_proportion * average norm of in-deriv
706  CuVector<BaseFloat> in_deriv_norm_vec(in_deriv->NumRows());
707  in_deriv_norm_vec.AddDiagMat2(1.0, *in_deriv, kNoTrans, 0.0);
708  in_deriv_norm_vec.ApplyPow(0.5);
709  double in_deriv_norm_sum = in_deriv_norm_vec.Sum();
710  BaseFloat magnitude = self_repair_scale_ * clipped_proportion *
711  (in_deriv_norm_sum / in_deriv_norm_vec.Dim());
712 
713  CuVector<BaseFloat> repair_mat_norm_vec(repair_mat.NumRows());
714  repair_mat_norm_vec.AddDiagMat2(1.0, repair_mat, kNoTrans, 0.0);
715  repair_mat_norm_vec.ApplyPow(0.5);
716  double repair_mat_norm_sum = repair_mat_norm_vec.Sum();
717  double scale = 0.0;
718  if (repair_mat_norm_sum != 0.0)
719  scale = magnitude / (repair_mat_norm_sum / repair_mat_norm_vec.Dim());
720  // repair_mat is scaled so that on average the rows have the norm
721  // (magnitude / repair_probability). This will give higher magnitude of
722  // self-repair to input vectors that have larger absolute value, which tend to
723  // be those that are diverging.
724  in_deriv->AddMat(-scale / repair_probability, repair_mat);
725  CuVector<BaseFloat> in_deriv_repaired_norm_vec(in_deriv->NumRows());
726  in_deriv_repaired_norm_vec.AddDiagMat2(1.0, *in_deriv, kNoTrans, 0.0);
727  in_deriv_repaired_norm_vec.ApplyPow(0.5);
728  // scale in_deriv to have the same norm as that before adding the self-repair
729  // term, in order to avoid increase of the norm caused by self-repair,
730  // which may incur more clip of gradient and thus more self-repair
731  double in_deriv_repaired_norm_sum = in_deriv_repaired_norm_vec.Sum();
732  if (in_deriv_repaired_norm_sum != 0.0)
733  in_deriv->Scale(in_deriv_norm_sum / in_deriv_repaired_norm_sum);
734 }
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
Definition: kaldi-math.h:151
void Scale(Real value)
Definition: cu-matrix.cc:610
float BaseFloat
Definition: kaldi-types.h:29
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:205
void AddMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType trans=kNoTrans)
*this += alpha * A
Definition: cu-matrix.cc:941
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
#define KALDI_LOG
Definition: kaldi-error.h:133
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 743 of file nnet-simple-component.cc.

References ClipGradientComponent::count_, and ClipGradientComponent::num_clipped_.

virtual std::string Type ( ) const
inlinevirtual

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

Implements Component.

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

Referenced by ClipGradientComponent::Info(), and ClipGradientComponent::InitFromConfig().

1250 { return "ClipGradientComponent"; }
void Write ( std::ostream &  os,
bool  binary 
) const
virtual

Write component to stream.

Implements Component.

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

References ClipGradientComponent::clipping_threshold_, ClipGradientComponent::count_, ClipGradientComponent::dim_, ClipGradientComponent::norm_based_clipping_, ClipGradientComponent::num_backpropped_, ClipGradientComponent::num_clipped_, ClipGradientComponent::num_self_repaired_, ClipGradientComponent::self_repair_clipped_proportion_threshold_, ClipGradientComponent::self_repair_scale_, ClipGradientComponent::self_repair_target_, kaldi::WriteBasicType(), and kaldi::WriteToken().

492  {
493  WriteToken(os, binary, "<ClipGradientComponent>");
494  WriteToken(os, binary, "<Dim>");
495  WriteBasicType(os, binary, dim_);
496  WriteToken(os, binary, "<ClippingThreshold>");
497  WriteBasicType(os, binary, clipping_threshold_);
498  WriteToken(os, binary, "<NormBasedClipping>");
500  WriteToken(os, binary, "<SelfRepairClippedProportionThreshold>");
502  WriteToken(os, binary, "<SelfRepairTarget>");
503  WriteBasicType(os, binary, self_repair_target_);
504  WriteToken(os, binary, "<SelfRepairScale>");
505  WriteBasicType(os, binary, self_repair_scale_);
506  WriteToken(os, binary, "<NumElementsClipped>");
507  WriteBasicType(os, binary, num_clipped_);
508  WriteToken(os, binary, "<NumElementsProcessed>");
509  WriteBasicType(os, binary, count_);
510  WriteToken(os, binary, "<NumSelfRepaired>");
511  WriteBasicType(os, binary, num_self_repaired_);
512  WriteToken(os, binary, "<NumBackpropped>");
513  WriteBasicType(os, binary, num_backpropped_);
514  WriteToken(os, binary, "</ClipGradientComponent>");
515 }
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34
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 736 of file nnet-simple-component.cc.

References ClipGradientComponent::count_, ClipGradientComponent::num_backpropped_, ClipGradientComponent::num_clipped_, and ClipGradientComponent::num_self_repaired_.

Member Data Documentation

std::string debug_info_
private

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