BackpropTruncationComponent Class Reference

#include <nnet-general-component.h>

Inheritance diagram for BackpropTruncationComponent:
Collaboration diagram for BackpropTruncationComponent:

Public Member Functions

 BackpropTruncationComponent (int32 dim, BaseFloat scale, BaseFloat clipping_threshold, BaseFloat zeroing_threshold, int32 zeroing_interval, int32 recurrence_interval)
 
 BackpropTruncationComponent ()
 
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 scale, BaseFloat clipping_threshold, BaseFloat zeroing_threshold, int32 zeroing_interval, int32 recurrence_interval)
 
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 > &, 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 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 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 ~BackpropTruncationComponent ()
 
- 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 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...
 
virtual void ConsolidateMemory ()
 This virtual function relates to memory management, and avoiding fragmentation. More...
 
 Component ()
 
virtual ~Component ()
 

Protected Attributes

double num_clipped_
 
double num_zeroed_
 
double count_
 
double count_zeroing_boundaries_
 

Private Member Functions

BackpropTruncationComponentoperator= (const BackpropTruncationComponent &other)
 

Private Attributes

int32 dim_
 
BaseFloat scale_
 
BaseFloat clipping_threshold_
 
BaseFloat zeroing_threshold_
 
int32 zeroing_interval_
 
int32 recurrence_interval_
 
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 466 of file nnet-general-component.h.

Constructor & Destructor Documentation

◆ BackpropTruncationComponent() [1/2]

BackpropTruncationComponent ( int32  dim,
BaseFloat  scale,
BaseFloat  clipping_threshold,
BaseFloat  zeroing_threshold,
int32  zeroing_interval,
int32  recurrence_interval 
)
inline

Definition at line 468 of file nnet-general-component.h.

References DistributeComponent::Init().

473  {
474  Init(dim, scale, clipping_threshold, zeroing_threshold,
475  zeroing_interval, recurrence_interval);}
void Init(int32 dim, BaseFloat scale, BaseFloat clipping_threshold, BaseFloat zeroing_threshold, int32 zeroing_interval, int32 recurrence_interval)

◆ BackpropTruncationComponent() [2/2]

◆ ~BackpropTruncationComponent()

virtual ~BackpropTruncationComponent ( )
inlinevirtual

Definition at line 523 of file nnet-general-component.h.

523  {
524  }

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 1198 of file nnet-general-component.cc.

References BackpropTruncationComponent::count_, BackpropTruncationComponent::count_zeroing_boundaries_, KALDI_ASSERT, BackpropTruncationComponent::num_clipped_, and BackpropTruncationComponent::num_zeroed_.

1199  {
1200  const BackpropTruncationComponent *other =
1201  dynamic_cast<const BackpropTruncationComponent*>(&other_in);
1202  KALDI_ASSERT(other != NULL);
1203  count_ += alpha * other->count_;
1204  count_zeroing_boundaries_ += alpha * other->count_zeroing_boundaries_;
1205  num_clipped_ += alpha * other->num_clipped_;
1206  num_zeroed_ += alpha * other->num_zeroed_;
1207 }
#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 1105 of file nnet-general-component.cc.

References CuMatrixBase< Real >::Add(), CuVectorBase< Real >::AddDiagMat2(), CuMatrixBase< Real >::ApplyHeaviside(), CuMatrixBase< Real >::CopyFromMat(), BackpropTruncationComponent::count_, BackpropTruncationComponent::count_zeroing_boundaries_, KALDI_ASSERT, kaldi::kNoTrans, CuVectorBase< Real >::MulElements(), CuMatrixBase< Real >::MulElements(), CuMatrixBase< Real >::MulRowsVec(), BackpropTruncationComponent::num_clipped_, BackpropTruncationComponent::num_zeroed_, CuMatrixBase< Real >::NumRows(), NVTX_RANGE, CuMatrixBase< Real >::Scale(), CuMatrixBase< Real >::Set(), CuMatrixBase< Real >::Sum(), BackpropTruncationComponentPrecomputedIndexes::zeroing, and BackpropTruncationComponentPrecomputedIndexes::zeroing_sum.

1113  {
1114  NVTX_RANGE("BackpropTruncationComponent::Backprop");
1115  const BackpropTruncationComponentPrecomputedIndexes *indexes =
1116  dynamic_cast<const BackpropTruncationComponentPrecomputedIndexes*>(
1117  indexes_in);
1118  KALDI_ASSERT(indexes->zeroing.Dim() == out_deriv.NumRows());
1119  // the following statement will do nothing if in_deriv and out_deriv have same
1120  // memory.
1121  in_deriv->CopyFromMat(out_deriv);
1122  if (scale_ != 1.0)
1123  in_deriv->Scale(scale_);
1124 
1125  BackpropTruncationComponent *to_update =
1126  dynamic_cast<BackpropTruncationComponent*>(to_update_in);
1127 
1128  // computes clipping_scales
1129  BaseFloat clipping_threshold =
1130  (clipping_threshold_ <= 0.0 ? 1.0e+10 : clipping_threshold_);
1131  // each row in the derivative matrix, which corresponds to one sample in
1132  // the mini-batch, is scaled to have a max-norm of clipping_threshold_
1133  CuVector<BaseFloat> clipping_scales(in_deriv->NumRows());
1134  clipping_scales.AddDiagMat2(pow(clipping_threshold, -2), *in_deriv,
1135  kNoTrans, 0.0);
1136  // now clipping_scales contains the squared (norm of each row divided by
1137  // clipping_threshold)
1138  int32 num_not_scaled;
1139  clipping_scales.ApplyFloor(1.0, &num_not_scaled);
1140  // now clipping_scales contains min(1, squared-(norm/clipping_threshold))
1141  clipping_scales.ApplyPow(-0.5);
1142  // now clipping_scales contains max(1, clipping_threshold/vector_norm)
1143  if (to_update != NULL) {
1144  to_update->num_clipped_ += (clipping_scales.Dim() - num_not_scaled);
1145  to_update->count_ += clipping_scales.Dim();
1146  }
1147 
1148  // computes zeroing_scales
1149  BaseFloat zeroing_threshold =
1150  (zeroing_threshold_ <= 0.0 ? 1.0e+10 : zeroing_threshold_);
1151  // zeroing_scales_vec is actually a 1-row matrix. (the ApplyHeaviside
1152  // function isn't defined for vectors).
1153  CuMatrix<BaseFloat> zeroing_scales(1, in_deriv->NumRows());
1154  CuSubVector<BaseFloat> zeroing_scales_vec(zeroing_scales, 0);
1155  zeroing_scales_vec.Set(-pow(zeroing_threshold, 2));
1156  // now zeroing_scales_vec contains -(squared zeroing_threshold)
1157  zeroing_scales_vec.AddDiagMat2(1.0, *in_deriv, kNoTrans, 1.0);
1158  // now zeroing_scales_vec contains squared norm of each row -
1159  // squared zeroing_threshold
1160  zeroing_scales.ApplyHeaviside();
1161  // now the element of zeroing_scales_vec is 1.0 if its corresponding
1162  // sample's norm exceeds zero_threshold, and 0.0 otherwise
1163  zeroing_scales_vec.MulElements(indexes->zeroing);
1164  // now the element of zeroing_scales_vec is -1.0 if we want to zero its
1165  // corresponding sample's gradient, and 0.0 otherwise
1166  if (to_update != NULL) {
1167  to_update->num_zeroed_ -= zeroing_scales_vec.Sum(); // since it is negative
1168  to_update->count_zeroing_boundaries_ += indexes->zeroing_sum;
1169  }
1170  zeroing_scales_vec.Add(1.0);
1171  // now the element of zeroing_scales_vec is 0.0 if we want to zero its
1172  // corresponding sample's gradient, and 1.0 otherwise
1173 
1174  // combines clipping_scales and zeroing_scales and applies combined_scales
1175  // to in_deriv all at once
1176  CuVector<BaseFloat> combined_scales(clipping_scales);
1177  combined_scales.MulElements(zeroing_scales_vec);
1178  in_deriv->MulRowsVec(combined_scales);
1179 }
kaldi::int32 int32
float BaseFloat
Definition: kaldi-types.h:29
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
#define NVTX_RANGE(name)
Definition: cu-common.h:143

◆ Copy()

Component * Copy ( ) const
virtual

Copies component (deep copy).

Implements Component.

Definition at line 1042 of file nnet-general-component.cc.

References BackpropTruncationComponent::clipping_threshold_, BackpropTruncationComponent::count_, BackpropTruncationComponent::count_zeroing_boundaries_, BackpropTruncationComponent::dim_, BackpropTruncationComponent::num_clipped_, BackpropTruncationComponent::num_zeroed_, BackpropTruncationComponent::recurrence_interval_, BackpropTruncationComponent::scale_, BackpropTruncationComponent::zeroing_interval_, and BackpropTruncationComponent::zeroing_threshold_.

1042  {
1044  ans->dim_ = dim_;
1045  ans->scale_ = scale_;
1046  ans->clipping_threshold_ = clipping_threshold_;
1047  ans->zeroing_threshold_ = zeroing_threshold_;
1048  ans->zeroing_interval_ = zeroing_interval_;
1049  ans->recurrence_interval_ = recurrence_interval_;
1050  ans->num_clipped_ = num_clipped_;
1051  ans->num_zeroed_ = num_zeroed_;
1052  ans->count_ = count_;
1053  ans->count_zeroing_boundaries_ = count_zeroing_boundaries_;
1054  return ans;
1055 }

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

Definition at line 981 of file nnet-general-component.cc.

References StatisticsPoolingComponent::Type().

981  {
982  std::ostringstream stream;
983  stream << Type() << ", dim=" << dim_
984  << ", scale=" << scale_
985  << ", count=" << std::setprecision(3) << count_ << std::setprecision(6)
986  << ", recurrence-interval=" << recurrence_interval_
987  << ", clipping-threshold=" << clipping_threshold_
988  << ", clipped-proportion="
989  << (count_ > 0.0 ? num_clipped_ / count_ : 0)
990  << ", zeroing-threshold=" << zeroing_threshold_
991  << ", zeroing-interval=" << zeroing_interval_
992  << ", zeroed-proportion="
993  << (count_zeroing_boundaries_ > 0.0 ?
995  << ", count-zeroing-boundaries="
996  << static_cast<int32>(count_zeroing_boundaries_);
997  return stream.str();
998 }
virtual std::string Type() const
Returns a string such as "SigmoidComponent", describing the type of the object.
kaldi::int32 int32

◆ Init()

void Init ( int32  dim,
BaseFloat  scale,
BaseFloat  clipping_threshold,
BaseFloat  zeroing_threshold,
int32  zeroing_interval,
int32  recurrence_interval 
)

Definition at line 1000 of file nnet-general-component.cc.

References KALDI_ASSERT.

1003  {
1004  KALDI_ASSERT(clipping_threshold >= 0 && zeroing_threshold >= 0 &&
1005  scale > 0.0 && zeroing_interval > 0 &&
1006  recurrence_interval > 0 && dim > 0);
1007  dim_ = dim;
1008  scale_ = scale;
1009  clipping_threshold_ = clipping_threshold;
1010  zeroing_threshold_ = zeroing_threshold;
1011  zeroing_interval_ = zeroing_interval;
1012  recurrence_interval_ = recurrence_interval;
1013  num_clipped_ = 0.0;
1014  num_zeroed_ = 0.0;
1015  count_ = 0.0;
1017 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ 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 1020 of file nnet-general-component.cc.

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

1020  {
1021  int32 dim = 0;
1022  bool ok = cfl->GetValue("dim", &dim);
1023  BaseFloat scale = 1.0,
1024  clipping_threshold = 30.0,
1025  zeroing_threshold = 15.0;
1026  int32 zeroing_interval = 20, recurrence_interval = 1;
1027  cfl->GetValue("scale", &scale);
1028  cfl->GetValue("clipping-threshold", &clipping_threshold);
1029  cfl->GetValue("zeroing-threshold", &zeroing_threshold);
1030  cfl->GetValue("zeroing-interval", &zeroing_interval);
1031  cfl->GetValue("recurrence-interval", &recurrence_interval);
1032  if (!ok || cfl->HasUnusedValues() ||
1033  clipping_threshold < 0 || zeroing_threshold < 0 || zeroing_interval < 1 ||
1034  recurrence_interval < 1 || dim <= 0)
1035  KALDI_ERR << "Invalid initializer for layer of type "
1036  << Type() << ": \"" << cfl->WholeLine() << "\"";
1037  Init(dim, scale, clipping_threshold, zeroing_threshold,
1038  zeroing_interval, recurrence_interval);
1039 }
void Init(int32 dim, BaseFloat scale, BaseFloat clipping_threshold, BaseFloat zeroing_threshold, int32 zeroing_interval, int32 recurrence_interval)
virtual std::string Type() const
Returns a string such as "SigmoidComponent", describing the type of the object.
kaldi::int32 int32
float BaseFloat
Definition: kaldi-types.h:29
#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 481 of file nnet-general-component.h.

◆ operator=()

BackpropTruncationComponent& operator= ( const BackpropTruncationComponent other)
private

◆ OutputDim()

virtual int32 OutputDim ( ) const
inlinevirtual

Returns output-dimension of this component.

Implements Component.

Definition at line 482 of file nnet-general-component.h.

References DistributeComponent::Init(), and DistributeComponent::InitFromConfig().

◆ PrecomputeIndexes()

ComponentPrecomputedIndexes * PrecomputeIndexes ( const MiscComputationInfo misc_info,
const std::vector< Index > &  input_indexes,
const std::vector< Index > &  output_indexes,
bool  need_backprop 
) const
virtual

This function must return NULL for simple Components.

Returns a pointer to a class that may contain some precomputed component-specific and computation-specific indexes to be in used in the Propagate and Backprop functions.

Parameters
[in]misc_infoThis argument is supplied to handle things that the framework can't very easily supply: information like which time indexes are needed for AggregateComponent, which time-indexes are available at the input of a recurrent network, and so on. misc_info may not even ever be used here. We will add members to misc_info as needed.
[in]input_indexesA vector of indexes that explains what time-indexes (and other indexes) each row of the in/in_value/in_deriv matrices given to Propagate and Backprop will mean.
[in]output_indexesA vector of indexes that explains what time-indexes (and other indexes) each row of the out/out_value/out_deriv matrices given to Propagate and Backprop will mean.
[in]need_backpropTrue if we might need to do backprop with this component, so that if any different indexes are needed for backprop then those should be computed too.
Returns
Returns a child-class of class ComponentPrecomputedIndexes, or NULL if this component for does not need to precompute any indexes (e.g. if it is a simple component and does not care about indexes).

Reimplemented from Component.

Definition at line 1059 of file nnet-general-component.cc.

References kaldi::DivideRoundingDown(), rnnlm::i, KALDI_ASSERT, VectorBase< Real >::Sum(), BackpropTruncationComponentPrecomputedIndexes::zeroing, and BackpropTruncationComponentPrecomputedIndexes::zeroing_sum.

1063  {
1064  int32 num_input_indexes = input_indexes.size(),
1065  num_output_indexes = output_indexes.size();
1066  KALDI_ASSERT(num_input_indexes == num_output_indexes);
1067  Vector<BaseFloat> zeroing_cpu(num_output_indexes);
1068 
1069  for (int32 i = 0; i < num_output_indexes; i++) {
1070  const int32 output_n = output_indexes[i].n;
1071  const int32 output_t = output_indexes[i].t;
1072  // checks if output_t crosses a boundary that is a multiple of
1073  // zeroing_interval_. Note that frame (output_t - recurrence_interval_) is
1074  // right before frame output_t in RNNs. If the range
1075  // [output_t - recurrence_interval_, output_t] contains a multiple of
1076  // zeroing_interval_, then frame output_t crosses the boundary.
1077  // output_n is used to shift where we put the boundary, so that
1078  // we don't always zero out gradients on frame 0. It will help avoid
1079  // learning utterance-boundary effects.
1080  if (DivideRoundingDown(output_t - output_n, zeroing_interval_) !=
1081  DivideRoundingDown(output_t - recurrence_interval_ - output_n,
1083  zeroing_cpu(i) = -1.0;
1084  }
1085 
1086  BackpropTruncationComponentPrecomputedIndexes *ans = new
1087  BackpropTruncationComponentPrecomputedIndexes();
1088  ans->zeroing = zeroing_cpu;
1089  ans->zeroing_sum = -zeroing_cpu.Sum();
1090  return ans;
1091 }
static int32 DivideRoundingDown(int32 a, int32 b)
Returns a / b, rounding towards negative infinity in all cases.
Definition: kaldi-math.h:287
kaldi::int32 int32
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ 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 1094 of file nnet-general-component.cc.

References CuMatrixBase< Real >::CopyFromMat(), and CuMatrixBase< Real >::Scale().

1097  {
1098  out->CopyFromMat(in);
1099  if (scale_ != 1.0)
1100  out->Scale(scale_);
1101  return NULL;
1102 }

◆ Properties()

◆ Read()

void Read ( std::istream &  is,
bool  binary 
)
virtual

Read function (used after we know the type of the Component); accepts input that is missing the token that describes the component type, in case it has already been consumed.

Implements Component.

Definition at line 898 of file nnet-general-component.cc.

References kaldi::ExpectOneOrTwoTokens(), kaldi::nnet3::ExpectToken(), KALDI_ASSERT, kaldi::ReadBasicType(), and kaldi::ReadToken().

898  {
899  // might not see the "<NaturalGradientAffineComponent>" part because
900  // of how ReadNew() works.
901  ExpectOneOrTwoTokens(is, binary, "<BackpropTruncationComponent>",
902  "<Dim>");
903  ReadBasicType(is, binary, &dim_);
904  std::string tok;
905  ReadToken(is, binary, &tok);
906  if (tok == "<Scale>") {
907  ReadBasicType(is, binary, &scale_);
908  ReadToken(is, binary, &tok);
909  } else {
910  scale_ = 1.0;
911  }
912  KALDI_ASSERT(tok == "<ClippingThreshold>");
913  ReadBasicType(is, binary, &clipping_threshold_);
914  ExpectToken(is, binary, "<ZeroingThreshold>");
915  ReadBasicType(is, binary, &zeroing_threshold_);
916  ExpectToken(is, binary, "<ZeroingInterval>");
917  ReadBasicType(is, binary, &zeroing_interval_);
918  ExpectToken(is, binary, "<RecurrenceInterval>");
919  ReadBasicType(is, binary, &recurrence_interval_);
920  ExpectToken(is, binary, "<NumElementsClipped>");
921  ReadBasicType(is, binary, &num_clipped_);
922  ExpectToken(is, binary, "<NumElementsZeroed>");
923  ReadBasicType(is, binary, &num_zeroed_);
924  ExpectToken(is, binary, "<NumElementsProcessed>");
925  ReadBasicType(is, binary, &count_);
926  ExpectToken(is, binary, "<NumZeroingBoundaries>");
928  ExpectToken(is, binary, "</BackpropTruncationComponent>");
929 }
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
Definition: io-funcs.cc:154
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: text-utils.cc:536
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:185

◆ 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 1190 of file nnet-general-component.cc.

◆ Type()

virtual std::string Type ( ) const
inlinevirtual

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

Implements Component.

Definition at line 488 of file nnet-general-component.h.

488 { return "BackpropTruncationComponent"; }

◆ Write()

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

Write component to stream.

Implements Component.

Definition at line 932 of file nnet-general-component.cc.

References kaldi::WriteBasicType(), and kaldi::WriteToken().

932  {
933  WriteToken(os, binary, "<BackpropTruncationComponent>");
934  WriteToken(os, binary, "<Dim>");
935  WriteBasicType(os, binary, dim_);
936  WriteToken(os, binary, "<Scale>");
937  WriteBasicType(os, binary, scale_);
938  WriteToken(os, binary, "<ClippingThreshold>");
939  WriteBasicType(os, binary, clipping_threshold_);
940  WriteToken(os, binary, "<ZeroingThreshold>");
941  WriteBasicType(os, binary, zeroing_threshold_);
942  WriteToken(os, binary, "<ZeroingInterval>");
943  WriteBasicType(os, binary, zeroing_interval_);
944  WriteToken(os, binary, "<RecurrenceInterval>");
946  WriteToken(os, binary, "<NumElementsClipped>");
947  WriteBasicType(os, binary, num_clipped_);
948  WriteToken(os, binary, "<NumElementsZeroed>");
949  WriteBasicType(os, binary, num_zeroed_);
950  WriteToken(os, binary, "<NumElementsProcessed>");
951  WriteBasicType(os, binary, count_);
952  WriteToken(os, binary, "<NumZeroingBoundaries>");
954  WriteToken(os, binary, "</BackpropTruncationComponent>");
955 }
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

◆ ZeroStats()

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 1182 of file nnet-general-component.cc.

Member Data Documentation

◆ clipping_threshold_

BaseFloat clipping_threshold_
private

Definition at line 536 of file nnet-general-component.h.

Referenced by BackpropTruncationComponent::Copy().

◆ count_

◆ count_zeroing_boundaries_

double count_zeroing_boundaries_
protected

◆ debug_info_

std::string debug_info_
private

Definition at line 552 of file nnet-general-component.h.

◆ dim_

int32 dim_
private

Definition at line 527 of file nnet-general-component.h.

Referenced by BackpropTruncationComponent::Copy().

◆ num_clipped_

◆ num_zeroed_

◆ recurrence_interval_

int32 recurrence_interval_
private

Definition at line 548 of file nnet-general-component.h.

Referenced by BackpropTruncationComponent::Copy().

◆ scale_

BaseFloat scale_
private

Definition at line 533 of file nnet-general-component.h.

Referenced by BackpropTruncationComponent::Copy().

◆ zeroing_interval_

int32 zeroing_interval_
private

Definition at line 543 of file nnet-general-component.h.

Referenced by BackpropTruncationComponent::Copy().

◆ zeroing_threshold_

BaseFloat zeroing_threshold_
private

Definition at line 539 of file nnet-general-component.h.

Referenced by BackpropTruncationComponent::Copy().


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