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 by. 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 1149 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 1151 of file nnet-simple-component.h.

References ClipGradientComponent::Init().

1159  {
1160  Init(dim, clipping_threshold, norm_based_clipping,
1161  self_repair_clipped_proportion_threshold,
1162  self_repair_target,
1163  self_repair_scale,
1164  num_clipped, count,
1165  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 1167 of file nnet-simple-component.h.

Referenced by ClipGradientComponent::Copy().

virtual ~ClipGradientComponent ( )
inlinevirtual

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

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

1225  {
1226  if (num_self_repaired_ > 0)
1227  KALDI_LOG << "ClipGradientComponent(node_name=" << debug_info_
1228  << ")'s self-repair was activated " << num_self_repaired_
1229  << " time(s) out of " << num_backpropped_
1230  << " times of calling Backprop() in this training job.";
1231  }
#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 it relates to adding stats Otherwise it should do nothing.

Reimplemented from Component.

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

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

884  {
885  const ClipGradientComponent *other =
886  dynamic_cast<const ClipGradientComponent*>(&other_in);
887  KALDI_ASSERT(other != NULL);
888  count_ += alpha * other->count_;
889  num_clipped_ += alpha * other->num_clipped_;
890 }
#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 732 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().

740  {
741  // the following statement will do nothing if in_deriv and out_deriv have same
742  // memory.
743  in_deriv->CopyFromMat(out_deriv);
744 
745  ClipGradientComponent *to_update =
746  dynamic_cast<ClipGradientComponent*>(to_update_in);
747 
748  if (clipping_threshold_ > 0) {
749  if (norm_based_clipping_) {
750  // each row in the derivative matrix, which corresponds to one sample in
751  // the mini-batch, is scaled to have a max-norm of clipping_threshold_
752  CuVector<BaseFloat> clipping_scales(in_deriv->NumRows());
753  clipping_scales.AddDiagMat2(pow(clipping_threshold_, -2), *in_deriv,
754  kNoTrans, 0.0);
755  // now clipping_scales contains the squared (norm of each row divided by
756  // clipping_threshold)
757  int32 num_not_scaled = clipping_scales.ApplyFloor(1.0);
758  // now clipping_scales contains min(1,
759  // squared-(norm/clipping_threshold))
760  if (num_not_scaled != clipping_scales.Dim()) {
761  clipping_scales.ApplyPow(-0.5);
762  // now clipping_scales contains max(1,
763  // clipping_threshold/vector_norm)
764  in_deriv->MulRowsVec(clipping_scales);
765  if (to_update != NULL)
766  to_update->num_clipped_ += (clipping_scales.Dim() - num_not_scaled);
767  }
768  if (to_update != NULL)
769  to_update->count_ += clipping_scales.Dim();
770  } else {
771  // each element of the derivative matrix, is clipped to be below the
772  // clipping_threshold_
774  in_deriv->ApplyFloor(-1 * clipping_threshold_);
775  }
776 
777  if (to_update != NULL) {
778  to_update->num_backpropped_ += 1;
779  RepairGradients(debug_info, in_value, in_deriv, to_update);
780  }
781  }
782 }
void ApplyCeiling(Real ceiling_val)
Definition: cu-matrix.cc:2385
void CopyFromMat(const MatrixBase< OtherReal > &src, MatrixTransposeType trans=kNoTrans)
Definition: cu-matrix.cc:337
void MulRowsVec(const CuVectorBase< Real > &scale)
scale i'th row by scale[i]
Definition: cu-matrix.cc:777
void ApplyFloor(Real floor_val)
Definition: cu-matrix.cc:2367
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:195
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 1194 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_.

1194  {
1195  return new ClipGradientComponent(dim_,
1201  num_clipped_,
1202  count_,
1204  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 654 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().

654  {
655  std::ostringstream stream;
656  stream << Type() << ", dim=" << dim_
657  << ", norm-based-clipping="
658  << (norm_based_clipping_ ? "true" : "false")
659  << ", clipping-threshold=" << clipping_threshold_
660  << ", clipped-proportion="
661  << (count_ > 0 ? static_cast<BaseFloat>(num_clipped_)/count_ : 0);
662  if (self_repair_scale_ != 0.0)
663  stream << ", self-repair-clipped-proportion-threshold="
665  << ", self-repair-target=" << self_repair_target_
666  << ", self-repair-scale=" << self_repair_scale_;
667  return stream.str();
668 }
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 670 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().

679  {
680  KALDI_ASSERT(clipping_threshold >= 0 && dim > 0 &&
681  self_repair_clipped_proportion_threshold >= 0.0 &&
682  self_repair_target >= 0.0 && self_repair_scale >= 0.0);
683  dim_ = dim;
684  norm_based_clipping_ = norm_based_clipping;
685  clipping_threshold_ = clipping_threshold;
687  self_repair_clipped_proportion_threshold;
688  self_repair_target_ = self_repair_target;
689  self_repair_scale_ = self_repair_scale;
690  num_clipped_ = num_clipped;
691  count_ = count;
692  num_self_repaired_ = num_self_repaired;
693  num_backpropped_ = num_backpropped;
694 }
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 696 of file nnet-simple-component.cc.

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

696  {
697  int32 dim = 0;
698  bool ok = cfl->GetValue("dim", &dim);
699  bool norm_based_clipping = false;
700  BaseFloat clipping_threshold = 15.0;
701  BaseFloat self_repair_clipped_proportion_threshold = 0.01;
702  BaseFloat self_repair_target = 0.0;
703  BaseFloat self_repair_scale = 1.0;
704  cfl->GetValue("clipping-threshold", &clipping_threshold);
705  cfl->GetValue("norm-based-clipping", &norm_based_clipping);
706  cfl->GetValue("self-repair-clipped-proportion-threshold",
707  &self_repair_clipped_proportion_threshold);
708  cfl->GetValue("self-repair-target",
709  &self_repair_target);
710  cfl->GetValue("self-repair-scale", &self_repair_scale);
711  if (!ok || cfl->HasUnusedValues() ||
712  clipping_threshold < 0 || dim <= 0 ||
713  self_repair_clipped_proportion_threshold < 0.0 ||
714  self_repair_target < 0.0 || self_repair_scale < 0.0)
715  KALDI_ERR << "Invalid initializer for layer of type "
716  << Type() << ": \"" << cfl->WholeLine() << "\"";
717  Init(dim, clipping_threshold, norm_based_clipping,
718  self_repair_clipped_proportion_threshold,
719  self_repair_target,
720  self_repair_scale, 0, 0, 0, 0);
721 }
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 1175 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 1176 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 723 of file nnet-simple-component.cc.

References CuMatrixBase< Real >::CopyFromMat().

726  {
727  out->CopyFromMat(in);
728  return NULL;
729 }
void CopyFromMat(const MatrixBase< OtherReal > &src, MatrixTransposeType trans=kNoTrans)
Definition: cu-matrix.cc:337
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 588 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_.

588  {
589  // might not see the "<NaturalGradientAffineComponent>" part because
590  // of how ReadNew() works.
591  ExpectOneOrTwoTokens(is, binary, "<ClipGradientComponent>",
592  "<Dim>");
593  ReadBasicType(is, binary, &dim_);
594  ExpectToken(is, binary, "<ClippingThreshold>");
595  ReadBasicType(is, binary, &clipping_threshold_);
596  ExpectToken(is, binary, "<NormBasedClipping>");
597  ReadBasicType(is, binary, &norm_based_clipping_);
598  std::string token;
599  ReadToken(is, binary, &token);
600  if (token == "<SelfRepairClippedProportionThreshold>") {
602  ExpectToken(is, binary, "<SelfRepairTarget>");
603  ReadBasicType(is, binary, &self_repair_target_);
604  ExpectToken(is, binary, "<SelfRepairScale>");
605  ReadBasicType(is, binary, &self_repair_scale_);
606  ExpectToken(is, binary, "<NumElementsClipped>");
607  } else {
609  self_repair_target_ = 0.0;
610  self_repair_scale_ = 0.0;
611  KALDI_ASSERT(token == "<NumElementsClipped>");
612  }
613  ReadBasicType(is, binary, &num_clipped_);
614  ExpectToken(is, binary, "<NumElementsProcessed>");
615  ReadBasicType(is, binary, &count_);
616  ReadToken(is, binary, &token);
617  if (token == "<NumSelfRepaired>") {
618  ReadBasicType(is, binary, &num_self_repaired_);
619  ExpectToken(is, binary, "<NumBackpropped>");
620  ReadBasicType(is, binary, &num_backpropped_);
621  ExpectToken(is, binary, "</ClipGradientComponent>");
622  } else {
623  num_self_repaired_ = 0;
624  num_backpropped_ = 0;
625  KALDI_ASSERT(token == "</ClipGradientComponent>");
626  }
627 }
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 792 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().

795  {
796  KALDI_ASSERT(to_update != NULL);
797 
798  // we use this 'repair_probability' (hardcoded for now) to limit
799  // this code to running on about half of the minibatches.
800  BaseFloat repair_probability = 0.5;
802  self_repair_scale_ == 0.0 || count_ == 0 ||
803  RandUniform() > repair_probability)
804  return;
805 
807 
808  BaseFloat clipped_proportion =
809  (count_ > 0 ? static_cast<BaseFloat>(num_clipped_) / count_ : 0);
810  // in-deriv would be modified only when clipped_proportion exceeds the
811  // threshold
812  if (clipped_proportion <= self_repair_clipped_proportion_threshold_)
813  return;
814 
815  to_update->num_self_repaired_ += 1;
816  if (to_update->debug_info_ == "") // get the component-node name
817  to_update->debug_info_ = debug_info;
818  if (to_update->num_self_repaired_ == 1)
819  KALDI_LOG << "ClipGradientComponent(node_name=" << debug_info
820  << ")'s self-repair was activated as the first time at the "
821  << to_update->num_backpropped_
822  << "-th call of Backprop() in this training job.";
823 
824  // sign_mat = sign(in_value), i.e.,
825  // An element in sign_mat is 1 if its corresponding element in in_value > 0,
826  // or -1 otherwise
827  CuMatrix<BaseFloat> sign_mat(in_value);
828  sign_mat.ApplyHeaviside();
829  sign_mat.Scale(2.0);
830  sign_mat.Add(-1.0);
831 
832  // repair_mat =
833  // floor(abs(in_value) - self_repair_target_, 0) .* sign(in_value)
834  CuMatrix<BaseFloat> repair_mat(in_value);
835  repair_mat.ApplyPowAbs(1.0);
836  repair_mat.Add(-self_repair_target_);
837  repair_mat.ApplyFloor(0.0);
838  repair_mat.MulElements(sign_mat);
839 
840  // magnitude =
841  // self_repair_scale_ * clipped_proportion * average norm of in-deriv
842  CuVector<BaseFloat> in_deriv_norm_vec(in_deriv->NumRows());
843  in_deriv_norm_vec.AddDiagMat2(1.0, *in_deriv, kNoTrans, 0.0);
844  in_deriv_norm_vec.ApplyPow(0.5);
845  double in_deriv_norm_sum = in_deriv_norm_vec.Sum();
846  BaseFloat magnitude = self_repair_scale_ * clipped_proportion *
847  (in_deriv_norm_sum / in_deriv_norm_vec.Dim());
848 
849  CuVector<BaseFloat> repair_mat_norm_vec(repair_mat.NumRows());
850  repair_mat_norm_vec.AddDiagMat2(1.0, repair_mat, kNoTrans, 0.0);
851  repair_mat_norm_vec.ApplyPow(0.5);
852  double repair_mat_norm_sum = repair_mat_norm_vec.Sum();
853  double scale = 0.0;
854  if (repair_mat_norm_sum != 0.0)
855  scale = magnitude / (repair_mat_norm_sum / repair_mat_norm_vec.Dim());
856  // repair_mat is scaled so that on average the rows have the norm
857  // (magnitude / repair_probability). This will give higher magnitude of
858  // self-repair to input vectors that have larger absolute value, which tend to
859  // be those that are diverging.
860  in_deriv->AddMat(-scale / repair_probability, repair_mat);
861  CuVector<BaseFloat> in_deriv_repaired_norm_vec(in_deriv->NumRows());
862  in_deriv_repaired_norm_vec.AddDiagMat2(1.0, *in_deriv, kNoTrans, 0.0);
863  in_deriv_repaired_norm_vec.ApplyPow(0.5);
864  // scale in_deriv to have the same norm as that before adding the self-repair
865  // term, in order to avoid increase of the norm caused by self-repair,
866  // which may incur more clip of gradient and thus more self-repair
867  double in_deriv_repaired_norm_sum = in_deriv_repaired_norm_vec.Sum();
868  if (in_deriv_repaired_norm_sum != 0.0)
869  in_deriv->Scale(in_deriv_norm_sum / in_deriv_repaired_norm_sum);
870 }
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:608
float BaseFloat
Definition: kaldi-types.h:29
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:195
void AddMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType trans=kNoTrans)
*this += alpha * A
Definition: cu-matrix.cc:939
#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 by.

by "scale" when called by an UpdatableComponent. stores stats, like BatchNormComponent– it relates to scaling activation stats, not parameters.

Reimplemented from Component.

Definition at line 879 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 1185 of file nnet-simple-component.h.

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

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

Write component to stream.

Implements Component.

Definition at line 629 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().

629  {
630  WriteToken(os, binary, "<ClipGradientComponent>");
631  WriteToken(os, binary, "<Dim>");
632  WriteBasicType(os, binary, dim_);
633  WriteToken(os, binary, "<ClippingThreshold>");
634  WriteBasicType(os, binary, clipping_threshold_);
635  WriteToken(os, binary, "<NormBasedClipping>");
637  WriteToken(os, binary, "<SelfRepairClippedProportionThreshold>");
639  WriteToken(os, binary, "<SelfRepairTarget>");
640  WriteBasicType(os, binary, self_repair_target_);
641  WriteToken(os, binary, "<SelfRepairScale>");
642  WriteBasicType(os, binary, self_repair_scale_);
643  WriteToken(os, binary, "<NumElementsClipped>");
644  WriteBasicType(os, binary, num_clipped_);
645  WriteToken(os, binary, "<NumElementsProcessed>");
646  WriteBasicType(os, binary, count_);
647  WriteToken(os, binary, "<NumSelfRepaired>");
648  WriteBasicType(os, binary, num_self_repaired_);
649  WriteToken(os, binary, "<NumBackpropped>");
650  WriteBasicType(os, binary, num_backpropped_);
651  WriteToken(os, binary, "</ClipGradientComponent>");
652 }
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 872 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: