StatisticsPoolingComponent Class Reference

#include <nnet-general-component.h>

Inheritance diagram for StatisticsPoolingComponent:
Collaboration diagram for StatisticsPoolingComponent:

Public Member Functions

 StatisticsPoolingComponent ()
 
 StatisticsPoolingComponent (const StatisticsPoolingComponent &other)
 
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...
 
virtual std::string Type () const
 Returns a string such as "SigmoidComponent", describing the type of the object. More...
 
virtual int32 Properties () const
 Return bitmask of the component's properties. More...
 
virtual void * Propagate (const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
 Propagate function. More...
 
virtual void Backprop (const std::string &debug_info, const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, void *memo, Component *, CuMatrixBase< BaseFloat > *in_deriv) const
 Backprop function; depending on which of the arguments 'to_update' and 'in_deriv' are non-NULL, this can compute input-data derivatives and/or perform model update. More...
 
virtual void Read (std::istream &is, bool binary)
 Read function (used after we know the type of the Component); accepts input that is missing the token that describes the component type, in case it has already been consumed. More...
 
virtual void Write (std::ostream &os, bool binary) const
 Write component to stream. More...
 
virtual ComponentCopy () const
 Copies component (deep copy). More...
 
virtual void GetInputIndexes (const MiscComputationInfo &misc_info, const Index &output_index, std::vector< Index > *desired_indexes) const
 This function only does something interesting for non-simple Components. More...
 
virtual bool IsComputable (const MiscComputationInfo &misc_info, const Index &output_index, const IndexSet &input_index_set, std::vector< Index > *used_inputs) const
 This function only does something interesting for non-simple Components, and it exists to make it possible to manage optionally-required inputs. More...
 
virtual void ReorderIndexes (std::vector< Index > *input_indexes, std::vector< Index > *output_indexes) const
 This function only does something interesting for non-simple Components. More...
 
virtual ComponentPrecomputedIndexesPrecomputeIndexes (const MiscComputationInfo &misc_info, const std::vector< Index > &input_indexes, const std::vector< Index > &output_indexes, bool need_backprop) const
 This function must return NULL for simple Components. More...
 
void SetRequireDirectInput (bool b)
 
- Public Member Functions inherited from Component
virtual void StoreStats (const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, void *memo)
 This function may store stats on average activation values, and for some component types, the average value of the derivative of the nonlinearity. More...
 
virtual void ZeroStats ()
 Components that provide an implementation of StoreStats should also provide an implementation of ZeroStats(), to set those stats to zero. More...
 
virtual std::string Info () const
 Returns some text-form information about this component, for diagnostics. 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 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 ()
 

Private Member Functions

void Check () const
 
StatisticsPoolingComponentoperator= (const StatisticsPoolingComponent &other)
 

Private Attributes

int32 input_dim_
 
int32 input_period_
 
int32 left_context_
 
int32 right_context_
 
int32 num_log_count_features_
 
bool output_stddevs_
 
BaseFloat variance_floor_
 
bool require_direct_input_
 

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 337 of file nnet-general-component.h.

Constructor & Destructor Documentation

◆ StatisticsPoolingComponent() [1/2]

◆ StatisticsPoolingComponent() [2/2]

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

References StatisticsPoolingComponent::Check().

Member Function Documentation

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

References CuMatrixBase< Real >::AddRowRanges(), CuVectorBase< Real >::ApplyExp(), StatisticsPoolingComponentPrecomputedIndexes::backward_indexes, CuMatrixBase< Real >::ColRange(), CuVectorBase< Real >::CopyColFromMat(), CuVectorBase< Real >::Data(), CuMatrixBase< Real >::DivRowsVec(), StatisticsPoolingComponentPrecomputedIndexes::forward_indexes, StatisticsPoolingComponent::input_dim_, KALDI_ASSERT, kaldi::kUndefined, StatisticsPoolingComponent::num_log_count_features_, CuMatrixBase< Real >::NumRows(), NVTX_RANGE, StatisticsPoolingComponent::output_stddevs_, and CuVectorBase< Real >::SetZero().

834  {
835  NVTX_RANGE("StatisticsPoolingComponent::Backprop");
836  KALDI_ASSERT(indexes_in != NULL);
837  const StatisticsPoolingComponentPrecomputedIndexes *indexes =
838  dynamic_cast<const StatisticsPoolingComponentPrecomputedIndexes*>(
839  indexes_in);
840  int32 num_rows_out = out_deriv_in.NumRows();
841  CuMatrix<BaseFloat> out_deriv(out_deriv_in);
842  if (output_stddevs_) {
843  // for now we actually ignore the covariance flooring in the backprop- this
844  // is an approximation. Typically the derivatives computed will be quite
845  // tiny for floored variances (they should be zero), so it won't affect the
846  // derivatives much.
847  int32 feature_dim = (input_dim_ - 1) / 2;
848  CuSubMatrix<BaseFloat> mean_deriv(out_deriv, 0, num_rows_out,
849  num_log_count_features_, feature_dim),
850  variance_deriv(out_deriv, 0, num_rows_out,
851  num_log_count_features_ + feature_dim, feature_dim),
852  mean_value(out_value, 0, num_rows_out,
853  num_log_count_features_, feature_dim),
854  stddev_value(out_value, 0, num_rows_out,
855  num_log_count_features_ + feature_dim, feature_dim);
856  // we currently have the deriv w.r.t. the stddev. step 1 is to get it
857  // w.r.t. the centered variance. If the centered variance is s,
858  // and the stddev is sqrt(s), then d/ds sqrt(s) = 0.5 / sqrt(s),
859  // so we need to multiply variance_deriv by 0.5 / the stddev.
860  variance_deriv.DivElements(stddev_value);
861  variance_deriv.Scale(0.5);
862 
863  // the deriv w.r.t. the uncentered variance is the same as w.r.t. the
864  // uncentered variance (since they difer by a constant term of -(mean *
865  // mean), but we need to add to dF/dmean, the value -2.0 * mean *
866  // dF/dvariance.
867  mean_deriv.AddMatMatElements(-2.0, mean_value, variance_deriv, 1.0);
868  }
869  // now we have to account for the effect of division by the count, on
870  // the derivative.
871  CuVector<BaseFloat> counts(num_rows_out, kUndefined);
872  if (num_log_count_features_ > 0) {
873  counts.CopyColFromMat(out_value, 0);
874  counts.ApplyExp();
875  } else {
876  counts.SetZero();
877  // we need to recompute the counts from the input since they are not in the
878  // output. The submatrix initializer below takes num-rows, num-cols,
879  // stride; num-cols and stride are 1.
880  CuSubMatrix<BaseFloat> counts_mat(counts.Data(), num_rows_out, 1, 1);
881  counts_mat.AddRowRanges(in_value.ColRange(0, 1), indexes->forward_indexes);
882  }
883  // Divide the output derivative by the counts. This is what we want as it
884  // concerns the mean and x^2 stats. As for the counts themselves, the
885  // derivative will end up being discarded when we backprop to the
886  // StatisticsExtractionComponent (as the count is not differentiable) so it
887  // doesn't really matter.
888  out_deriv.DivRowsVec(counts);
889 
890  // Now propagate the derivative back to the input. we don't propagate it
891  // back for the count's row since it's non-differentiable.
892  in_deriv->ColRange(1, input_dim_ - 1).
893  AddRowRanges(out_deriv.ColRange(num_log_count_features_, input_dim_ - 1),
894  indexes->backward_indexes);
895 }
kaldi::int32 int32
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
#define NVTX_RANGE(name)
Definition: cu-common.h:143

◆ Check()

void Check ( ) const
private

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

References StatisticsPoolingComponent::input_dim_, StatisticsPoolingComponent::input_period_, KALDI_ASSERT, StatisticsPoolingComponent::left_context_, StatisticsPoolingComponent::output_stddevs_, StatisticsPoolingComponent::right_context_, and StatisticsPoolingComponent::variance_floor_.

Referenced by StatisticsPoolingComponent::Read(), and StatisticsPoolingComponent::StatisticsPoolingComponent().

◆ Copy()

virtual Component* Copy ( ) const
inlinevirtual

◆ GetInputIndexes()

void GetInputIndexes ( const MiscComputationInfo misc_info,
const Index output_index,
std::vector< Index > *  desired_indexes 
) const
virtual

This function only does something interesting for non-simple Components.

For a given index at the output of the component, tells us what indexes are required at its input (note: "required" encompasses also optionally-required things; it will enumerate all things that we'd like to have). See also IsComputable().

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. We will add members to misc_info as needed.
[in]output_indexThe Index at the output of the component, for which we are requesting the list of indexes at the component's input.
[out]desired_indexesA list of indexes that are desired at the input. are to be written to here. By "desired" we mean required or optionally-required.

The default implementation of this function is suitable for any SimpleComponent; it just copies the output_index to a single identical element in input_indexes.

Reimplemented from Component.

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

References StatisticsPoolingComponent::input_period_, KALDI_ASSERT, StatisticsPoolingComponent::left_context_, StatisticsPoolingComponent::right_context_, and Index::t.

657  {
658  desired_indexes->clear();
659  Index input_index(output_index);
660  int32 middle_t = output_index.t,
661  t_start = middle_t - left_context_,
662  t_last = middle_t + right_context_;
663  KALDI_ASSERT(middle_t % input_period_ == 0);
664  for (int32 t = t_start; t <= t_last; t += input_period_) {
665  input_index.t = t;
666  desired_indexes->push_back(input_index);
667  }
668 }
kaldi::int32 int32
#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 555 of file nnet-general-component.cc.

References StatisticsExtractionComponent::Check(), ConfigLine::GetValue(), ConfigLine::HasUnusedValues(), StatisticsExtractionComponent::input_dim_, StatisticsExtractionComponent::input_period_, KALDI_ERR, StatisticsExtractionComponent::Type(), ConfigLine::UnusedValues(), and ConfigLine::WholeLine().

555  {
556  bool ok = cfl->GetValue("input-dim", &input_dim_);
557  cfl->GetValue("input-period", &input_period_);
558  cfl->GetValue("left-context", &left_context_);
559  cfl->GetValue("right-context", &right_context_);
560  cfl->GetValue("num-log-count-features", &num_log_count_features_);
561  cfl->GetValue("output-stddevs", &output_stddevs_);
562  cfl->GetValue("variance-floor", &variance_floor_);
563 
564  if (cfl->HasUnusedValues())
565  KALDI_ERR << "Could not process these elements in initializer: "
566  << cfl->UnusedValues();
567  // do some basic checks here but Check() will check more completely.
568  if (!ok || input_dim_ <= 0 || left_context_ + right_context_ <= 0 ||
570  KALDI_ERR << "Invalid initializer for layer of type "
571  << Type() << ": \"" << cfl->WholeLine() << "\"";
572  Check();
573 }
virtual std::string Type() const
Returns a string such as "SigmoidComponent", describing the type of the object.
#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 345 of file nnet-general-component.h.

References DistributeComponent::input_dim_.

◆ IsComputable()

bool IsComputable ( const MiscComputationInfo misc_info,
const Index output_index,
const IndexSet input_index_set,
std::vector< Index > *  used_inputs 
) const
virtual

This function only does something interesting for non-simple Components, and it exists to make it possible to manage optionally-required inputs.

It tells the user whether a given output index is computable from a given set of input indexes, and if so, says which input indexes will be used in the computation.

Implementations of this function are required to have the property that adding an element to "input_index_set" can only ever change IsComputable from false to true, never vice versa.

Parameters
[in]misc_infoSome information specific to the computation, such as minimum and maximum times for certain components to do adaptation on; it's a place to put things that don't easily fit in the framework.
[in]output_indexThe index that is to be computed at the output of this Component.
[in]input_index_setThe set of indexes that is available at the input of this Component.
[out]used_inputsIf this is non-NULL and the output is computable this will be set to the list of input indexes that will actually be used in the computation.
Returns
Returns true iff this output is computable from the provided inputs.

The default implementation of this function is suitable for any SimpleComponent: it just returns true if output_index is in input_index_set, and if so sets used_inputs to vector containing that one Index.

Reimplemented from Component.

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

References StatisticsPoolingComponent::input_period_, StatisticsPoolingComponent::left_context_, StatisticsPoolingComponent::right_context_, and Index::t.

674  {
675  if (used_inputs)
676  used_inputs->clear();
677  // you are not supposed to access the output of this component other than at
678  // multiples of the input period. We could make this an error but decided to
679  // just have it return false.
680  if (output_index.t % input_period_ != 0)
681  return false;
682 
683  Index input_index(output_index);
684  int32 output_t = output_index.t,
685  t_start = output_t - left_context_,
686  t_last = output_t + right_context_;
687  if (!used_inputs) {
688  for (int32 t = t_start; t <= t_last; t += input_period_) {
689  input_index.t = t;
690  if (input_index_set(input_index))
691  return true;
692  }
693  return false;
694  } else {
695  bool ans = false;
696  for (int32 t = t_start; t <= t_last; t += input_period_) {
697  input_index.t = t;
698  if (input_index_set(input_index)) {
699  ans = true;
700  used_inputs->push_back(input_index);
701  }
702  }
703  return ans;
704  }
705 }
kaldi::int32 int32

◆ operator=()

StatisticsPoolingComponent& operator= ( const StatisticsPoolingComponent other)
private

◆ OutputDim()

virtual int32 OutputDim ( ) const
inlinevirtual

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

References StatisticsPoolingComponentPrecomputedIndexes::backward_indexes, Int32Pair::first, StatisticsPoolingComponentPrecomputedIndexes::forward_indexes, rnnlm::i, StatisticsPoolingComponent::input_period_, KALDI_ASSERT, StatisticsPoolingComponent::left_context_, StatisticsPoolingComponent::right_context_, Int32Pair::second, and Index::t.

712  {
713  int32 num_input_indexes = input_indexes.size(),
714  num_output_indexes = output_indexes.size();
715  StatisticsPoolingComponentPrecomputedIndexes *ans = new
716  StatisticsPoolingComponentPrecomputedIndexes();
717 
718  Int32Pair invalid_pair;
719  invalid_pair.first = -1;
720  invalid_pair.second = -1;
721  // forward_indexes_cpu[i] will be the (begin, end) of input indexes
722  // included in the sum for the i'th output index.
723  std::vector<Int32Pair> forward_indexes_cpu(num_output_indexes,
724  invalid_pair);
725  // backward_indexes_cpu[i] will be the (begin, end) of output indexes
726  // for which the i'th input index participates in the sum.
727  // because of the way the indexes are sorted (and the fact that only
728  // required indexes are present at the input), it naturally has this
729  // structure [i.e. no gaps in the sets of indexes].
730  std::vector<Int32Pair> backward_indexes_cpu(num_input_indexes,
731  invalid_pair);
732 
733  // this map maps from Index to the position in 'input_indexes'.
734  unordered_map<Index, int32, IndexHasher> index_to_input_pos;
735  for (int32 i = 0; i < num_input_indexes; i++)
736  index_to_input_pos[input_indexes[i]] = i;
737 
738  for (int32 i = 0; i < num_output_indexes; i++) {
739  Index input_index(output_indexes[i]);
740  int32 middle_t = input_index.t,
741  t_start = middle_t - left_context_,
742  t_last = middle_t + right_context_;
743  for (int32 t = t_start; t <= t_last; t += input_period_) {
744  input_index.t = t;
745  unordered_map<Index, int32, IndexHasher>::iterator iter =
746  index_to_input_pos.find(input_index);
747  if (iter != index_to_input_pos.end()) {
748  int32 input_pos = iter->second;
749  if (forward_indexes_cpu[i].first == -1) {
750  forward_indexes_cpu[i].first = input_pos;
751  forward_indexes_cpu[i].second = input_pos + 1;
752  } else {
753  KALDI_ASSERT(forward_indexes_cpu[i].second == input_pos);
754  forward_indexes_cpu[i].second++;
755  }
756  if (backward_indexes_cpu[input_pos].first == -1) {
757  backward_indexes_cpu[input_pos].first = i;
758  backward_indexes_cpu[input_pos].second = i + 1;
759  } else {
760  KALDI_ASSERT(backward_indexes_cpu[input_pos].second == i);
761  backward_indexes_cpu[input_pos].second++;
762  }
763  }
764  }
765  KALDI_ASSERT(forward_indexes_cpu[i].first != -1);
766  }
767  for (int32 i = 0; i < num_input_indexes; i++) {
768  KALDI_ASSERT(backward_indexes_cpu[i].first != -1);
769  }
770 
771  ans->forward_indexes = forward_indexes_cpu;
772  if (need_backprop)
773  ans->backward_indexes = backward_indexes_cpu;
774  return ans;
775 }
kaldi::int32 int32
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32_cuda second
Definition: cu-matrixdim.h:80
int32_cuda first
Definition: cu-matrixdim.h:79

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

References CuMatrixBase< Real >::AddMatMatElements(), CuMatrixBase< Real >::AddRowRanges(), CuMatrixBase< Real >::ApplyFloor(), CuMatrixBase< Real >::ApplyPow(), CuMatrixBase< Real >::ColRange(), CuArrayBase< T >::Dim(), CuMatrixBase< Real >::DivRowsVec(), StatisticsPoolingComponentPrecomputedIndexes::forward_indexes, StatisticsPoolingComponent::input_dim_, KALDI_ASSERT, kaldi::kUndefined, StatisticsPoolingComponent::num_log_count_features_, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), StatisticsPoolingComponent::output_stddevs_, StatisticsPoolingComponent::OutputDim(), CuVectorBase< Real >::Set(), CuMatrixBase< Real >::SetZero(), and StatisticsPoolingComponent::variance_floor_.

780  {
781  out->SetZero();
782  KALDI_ASSERT(indexes_in != NULL);
783  const StatisticsPoolingComponentPrecomputedIndexes *indexes =
784  dynamic_cast<const StatisticsPoolingComponentPrecomputedIndexes*>(indexes_in);
785  int32 num_rows_out = out->NumRows();
786  KALDI_ASSERT(indexes != NULL &&
787  indexes->forward_indexes.Dim() == num_rows_out &&
788  in.NumCols() == input_dim_ &&
789  out->NumCols() == OutputDim());
790  CuVector<BaseFloat> counts(num_rows_out);
791  // counts_mat is a fake matrix with one column, containing the counts.
792  CuSubMatrix<BaseFloat> counts_mat(counts.Data(), num_rows_out, 1, 1);
793  counts_mat.AddRowRanges(in.ColRange(0, 1), indexes->forward_indexes);
794 
795  CuSubMatrix<BaseFloat> out_non_count(*out, 0, num_rows_out,
797  out_non_count.AddRowRanges(in.ColRange(1, input_dim_ - 1),
798  indexes->forward_indexes);
799  out_non_count.DivRowsVec(counts);
800 
801  if (num_log_count_features_ > 0) {
802  counts.ApplyLog();
803  CuVector<BaseFloat> ones(num_log_count_features_, kUndefined);
804  ones.Set(1.0);
805  out->ColRange(0, num_log_count_features_).AddVecVec(1.0, counts, ones);
806  }
807 
808  if (output_stddevs_) {
809  // if this is true, then we assume the input contains x^2 stats as well as x
810  // stats, and we want to process them into a standard deviation.
811  KALDI_ASSERT((input_dim_ - 1) % 2 == 0);
812  int32 feature_dim = (input_dim_ - 1) / 2;
813  CuSubMatrix<BaseFloat> mean(*out, 0, num_rows_out,
814  num_log_count_features_, feature_dim),
815  variance(*out, 0, num_rows_out,
816  num_log_count_features_ + feature_dim, feature_dim);
817  // subtract mean-squared from average of x^2 to get the variance.
818  variance.AddMatMatElements(-1.0, mean, mean, 1.0);
819  variance.ApplyFloor(variance_floor_);
820  // compute the standard deviation via square root.
821  variance.ApplyPow(0.5);
822  }
823  return NULL;
824 }
virtual int32 OutputDim() const
Returns output-dimension of this component.
kaldi::int32 int32
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

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

References StatisticsPoolingComponent::Check(), kaldi::ExpectOneOrTwoTokens(), kaldi::nnet3::ExpectToken(), StatisticsPoolingComponent::input_dim_, StatisticsPoolingComponent::input_period_, StatisticsPoolingComponent::left_context_, StatisticsPoolingComponent::num_log_count_features_, StatisticsPoolingComponent::output_stddevs_, kaldi::ReadBasicType(), StatisticsPoolingComponent::require_direct_input_, StatisticsPoolingComponent::right_context_, and StatisticsPoolingComponent::variance_floor_.

603  {
604  ExpectOneOrTwoTokens(is, binary, "<StatisticsPoolingComponent>",
605  "<InputDim>");
606  ReadBasicType(is, binary, &input_dim_);
607  ExpectToken(is, binary, "<InputPeriod>");
608  ReadBasicType(is, binary, &input_period_);
609  ExpectToken(is, binary, "<LeftContext>");
610  ReadBasicType(is, binary, &left_context_);
611  ExpectToken(is, binary, "<RightContext>");
612  ReadBasicType(is, binary, &right_context_);
613  ExpectToken(is, binary, "<NumLogCountFeatures>");
615  ExpectToken(is, binary, "<OutputStddevs>");
616  ReadBasicType(is, binary, &output_stddevs_);
617  ExpectToken(is, binary, "<VarianceFloor>");
618  ReadBasicType(is, binary, &variance_floor_);
619  ExpectToken(is, binary, "</StatisticsPoolingComponent>");
620  require_direct_input_ = false; // This is not written to disk, it's only used
621  // temporarily, in memory (see
622  // nnet3-xvector-compute-batched.cc).
623  Check();
624 }
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: text-utils.cc:536
static void ExpectToken(const std::string &token, const std::string &what_we_are_parsing, const std::string **next_token)

◆ ReorderIndexes()

void ReorderIndexes ( std::vector< Index > *  input_indexes,
std::vector< Index > *  output_indexes 
) const
virtual

This function only does something interesting for non-simple Components.

It provides an opportunity for a Component to reorder the or pad the indexes at its input and output. This might be useful, for instance, if a component requires a particular ordering of the indexes that doesn't correspond to their natural ordering. Components that might modify the indexes are required to return the kReordersIndexes flag in their Properties(). The ReorderIndexes() function is now allowed to insert blanks into the indexes. The 'blanks' must be of the form (n,kNoTime,x), where the marker kNoTime (a very negative number) is there where the 't' indexes normally live. The reason we don't just have, say, (-1,-1,-1), relates to the need to preserve a regular pattern over the 'n' indexes so that 'shortcut compilation' (c.f. ExpandComputation()) can work correctly

Parameters
[in,out]Indexesat the input of the Component.
[in,out]Indexesat the output of the Component

Reimplemented from Component.

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

647  {
648  std::sort(input_indexes->begin(), input_indexes->end(),
649  IndexLessNxt());
650  std::sort(output_indexes->begin(), output_indexes->end(),
651  IndexLessNxt());
652 }

◆ SetRequireDirectInput()

void SetRequireDirectInput ( bool  b)
inline

◆ Type()

virtual std::string Type ( ) const
inlinevirtual

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

Implements Component.

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

Referenced by BackpropTruncationComponent::Info(), ConstantComponent::Info(), and BackpropTruncationComponent::InitFromConfig().

350 { return "StatisticsPoolingComponent"; }

◆ Write()

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

Write component to stream.

Implements Component.

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

References StatisticsPoolingComponent::input_dim_, StatisticsPoolingComponent::input_period_, StatisticsPoolingComponent::left_context_, StatisticsPoolingComponent::num_log_count_features_, StatisticsPoolingComponent::output_stddevs_, StatisticsPoolingComponent::right_context_, StatisticsPoolingComponent::variance_floor_, kaldi::WriteBasicType(), and kaldi::WriteToken().

626  {
627  WriteToken(os, binary, "<StatisticsPoolingComponent>");
628  WriteToken(os, binary, "<InputDim>");
629  WriteBasicType(os, binary, input_dim_);
630  WriteToken(os, binary, "<InputPeriod>");
631  WriteBasicType(os, binary, input_period_);
632  WriteToken(os, binary, "<LeftContext>");
633  WriteBasicType(os, binary, left_context_);
634  WriteToken(os, binary, "<RightContext>");
635  WriteBasicType(os, binary, right_context_);
636  WriteToken(os, binary, "<NumLogCountFeatures>");
638  WriteToken(os, binary, "<OutputStddevs>");
639  WriteBasicType(os, binary, output_stddevs_);
640  WriteToken(os, binary, "<VarianceFloor>");
641  WriteBasicType(os, binary, variance_floor_);
642  WriteToken(os, binary, "</StatisticsPoolingComponent>");
643 }
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

Member Data Documentation

◆ input_dim_

◆ input_period_

◆ left_context_

◆ num_log_count_features_

◆ output_stddevs_

◆ require_direct_input_

bool require_direct_input_
private

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

Referenced by StatisticsPoolingComponent::Read().

◆ right_context_

◆ variance_floor_


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