GeneralDropoutComponent Class Reference

GeneralDropoutComponent implements dropout, including a continuous variant where the thing we multiply is not just zero or one, but may be a continuous value. More...

#include <nnet-general-component.h>

Inheritance diagram for GeneralDropoutComponent:
Collaboration diagram for GeneralDropoutComponent:

Public Member Functions

virtual int32 InputDim () const
 Returns input-dimension of this component. More...
 
virtual int32 OutputDim () const
 Returns output-dimension of this component. More...
 
virtual std::string Info () const
 Returns some text-form information about this component, for diagnostics. More...
 
virtual void InitFromConfig (ConfigLine *cfl)
 Initialize, from a ConfigLine object. More...
 
 GeneralDropoutComponent ()
 
 GeneralDropoutComponent (const GeneralDropoutComponent &other)
 
virtual std::string Type () const
 Returns a string such as "SigmoidComponent", describing the type of the object. More...
 
virtual int32 Properties () const
 Return bitmask of the component's properties. More...
 
virtual void * Propagate (const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
 Propagate function. More...
 
virtual void Backprop (const std::string &debug_info, const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &, const CuMatrixBase< BaseFloat > &, const CuMatrixBase< BaseFloat > &out_deriv, void *memo, Component *to_update, CuMatrixBase< BaseFloat > *in_deriv) const
 Backprop function; depending on which of the arguments 'to_update' and 'in_deriv' are non-NULL, this can compute input-data derivatives and/or perform model update. More...
 
virtual void 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 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 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...
 
void SetDropoutProportion (BaseFloat p)
 
- Public Member Functions inherited from RandomComponent
void ResetGenerator ()
 
void SetTestMode (bool test_mode)
 
 RandomComponent ()
 
 RandomComponent (const RandomComponent &other)
 
- Public Member Functions inherited from Component
virtual void StoreStats (const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, void *memo)
 This function may store stats on average activation values, and for some component types, the average value of the derivative of the nonlinearity. More...
 
virtual void ZeroStats ()
 Components that provide an implementation of StoreStats should also provide an implementation of ZeroStats(), to set those stats to zero. More...
 
virtual void GetInputIndexes (const MiscComputationInfo &misc_info, const Index &output_index, std::vector< Index > *desired_indexes) const
 This function only does something interesting for non-simple Components. More...
 
virtual bool IsComputable (const MiscComputationInfo &misc_info, const Index &output_index, const IndexSet &input_index_set, std::vector< Index > *used_inputs) const
 This function only does something interesting for non-simple Components, and it exists to make it possible to manage optionally-required inputs. More...
 
virtual void ReorderIndexes (std::vector< Index > *input_indexes, std::vector< Index > *output_indexes) const
 This function only does something interesting for non-simple Components. More...
 
virtual 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 ConsolidateMemory ()
 This virtual function relates to memory management, and avoiding fragmentation. More...
 
 Component ()
 
virtual ~Component ()
 

Private Member Functions

CuMatrix< BaseFloat > * GetMemo (int32 num_mask_rows) const
 
const GeneralDropoutComponentoperator= (const GeneralDropoutComponent &other)
 

Private Attributes

int32 dim_
 
int32 block_dim_
 
int32 time_period_
 
BaseFloat dropout_proportion_
 
BaseFloat specaugment_max_proportion_
 
int32 specaugment_max_regions_
 
bool continuous_
 

Additional Inherited Members

- Static Public Member Functions inherited from Component
static ComponentReadNew (std::istream &is, bool binary)
 Read component from stream (works out its type). Dies on error. More...
 
static ComponentNewComponentOfType (const std::string &type)
 Returns a new Component of the given type e.g. More...
 
- Protected Attributes inherited from RandomComponent
CuRand< BaseFloatrandom_generator_
 
bool test_mode_
 

Detailed Description

GeneralDropoutComponent implements dropout, including a continuous variant where the thing we multiply is not just zero or one, but may be a continuous value.

It is intended for the case where you want to either share the dropout mask across all of time, or across groups of 't' values (e.g. the first block of 10 values gets one dropout mask, the second block of 10 gets another one, and so on).

It also has support for the frequency component of SpecAugment.

Configuration values accepted on the command line, with defaults:

dim Dimension of the input and output of this component, e.g. 512

block-dim Block size if you want the dropout mask to repeat, e.g. if dim=512 and you sent block-dim=128, there will be a mask of dimension 128 repeated 4 times. This can be useful in convolutional setups. If not specified, block-dim defaults to 'dim'; if specified, it must be a divisor of 'dim'.

dropout-proportion=0.5 For conventional dropout, this is the proportion of mask values that (in expectation) are zero; it would normally be between 0 and 0.5. The nonzero mask values will be given values 1.0 / dropout_proportion, so that the expected value is 1.0. This behavior is different from DropoutComponent and DropoutMaskComponent.

For continuous dropout (continuous==true), the dropout scales will have values (1.0 + 2 * dropout-proportion * Uniform[-1,1]). This might seem like a strange choice, but it means that dropout-proportion=0.5 gives us a kind of 'extremal' case where the dropout scales are distributed as Uniform[0, 2] and we can pass in the dropout scale as if it were a conventional dropout scale.

time-period=0 This determines how the dropout mask interacts with the time index (t). In all cases, different sequences (different 'n' values) get different dropout masks. If time-period==0, then the dropout mask is shared across all time values. If you set time-period > 0, then the dropout mask is shared across blocks of time values: for instance if time-period==10, then we'll use one dropout mask for t values 0 through 9, another for 10 through 19, and so on. In all cases, the dropout mask will be shared across all 'x' values, although in most setups the x values are just zero so this isn't very interesting. If you set time-period==1 it would be similar to regular dropout, and it would probably make more sense to just use the normal DropoutComponent.

specaugment-max-proportion=0 If nonzero, causes this component to implement SpecAugment. (Note: you probably would want this after a batch-norm component so the average at input is zero), and the input dim will be interpreted as some kind of frequency space, e.g. linear or mel. specaugment-max-proportion will be the maximum proportion of the frequency space that this component might zero out (so multiply this by by input dim to get the maximum columns that might be zeroed out); the actual number of columns zeroed out for each sequence will be randomly chosen between zero and the maximum. Note: the non-zeroed frequencies won't be multiplied by a constant more than one as we would in the normal dropout mode.

specaugment-max-regions=1 This can be set to a value greater than one (e.g., 2) to implement a variant of SpecAugment where instead of zeroing out a single region of the frequency spectrum we zero out a randomly chosen number of regions, from one to this number. The maximum proportion of the frequency spectrum that we remove is unaffected.

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

Constructor & Destructor Documentation

◆ GeneralDropoutComponent() [1/2]

◆ GeneralDropoutComponent() [2/2]

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

1553  :
1554  dim_(other.dim_),
1555  block_dim_(other.block_dim_),
1556  time_period_(other.time_period_),
1557  dropout_proportion_(other.dropout_proportion_),
1558  specaugment_max_proportion_(other.specaugment_max_proportion_),
1559  specaugment_max_regions_(other.specaugment_max_regions_),
1560  continuous_(other.continuous_) { }

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

References GeneralDropoutComponent::block_dim_, CuMatrixBase< Real >::CopyFromMat(), CuMatrixBase< Real >::Data(), GeneralDropoutComponent::dim_, GeneralDropoutComponent::dropout_proportion_, GeneralDropoutComponentPrecomputedIndexes::indexes, KALDI_ASSERT, CuMatrixBase< Real >::MulRows(), CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), NVTX_RANGE, kaldi::SameDim(), GeneralDropoutComponent::specaugment_max_proportion_, CuMatrixBase< Real >::Stride(), and RandomComponent::test_mode_.

1604  {
1605  NVTX_RANGE("GeneralDropoutComponent::Backprop");
1606  KALDI_ASSERT(in_deriv != NULL && SameDim(*in_deriv, out_deriv));
1607 
1608  // The following will do no work if in_deriv->Data() == out_deriv.Data().
1609  in_deriv->CopyFromMat(out_deriv);
1610 
1611  if (test_mode_ ||
1612  (dropout_proportion_ == 0.0 && specaugment_max_proportion_ == 0.0)) {
1613  KALDI_ASSERT(memo == NULL);
1614  return;
1615  }
1616 
1617  const GeneralDropoutComponentPrecomputedIndexes *indexes =
1618  dynamic_cast<const GeneralDropoutComponentPrecomputedIndexes*>(indexes_in);
1619  KALDI_ASSERT(indexes != NULL && memo != NULL);
1620  CuMatrix<BaseFloat> *mask = reinterpret_cast<CuMatrix<BaseFloat>*>(memo);
1621 
1622  if (block_dim_ < dim_) {
1623  KALDI_ASSERT(in_deriv->Stride() == in_deriv->NumCols());
1624  int32 num_rows = in_deriv->NumRows(),
1625  dim_multiple = dim_ / block_dim_,
1626  num_rows_reshaped = num_rows * dim_multiple;
1627  CuSubMatrix<BaseFloat> in_deriv_reshaped(in_deriv->Data(),
1628  num_rows_reshaped,
1630  in_deriv_reshaped.MulRows(*mask, indexes->indexes);
1631  } else {
1632  in_deriv->MulRows(*mask, indexes->indexes);
1633  }
1634 }
kaldi::int32 int32
bool SameDim(const MatrixBase< Real > &M, const MatrixBase< Real > &N)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
#define NVTX_RANGE(name)
Definition: cu-common.h:143

◆ Copy()

Component * Copy ( ) const
virtual

Copies component (deep copy).

Implements Component.

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

References GeneralDropoutComponent::GeneralDropoutComponent().

1699  {
1700  return new GeneralDropoutComponent(*this);
1701 }

◆ DeleteMemo()

virtual void DeleteMemo ( void *  memo) const
inlinevirtual

This virtual function only needs to be overwritten by Components that return a non-NULL memo from their Propagate() function.

It's called by NnetComputer in cases where Propagate returns a memo but there will be no backprop to consume it.

Reimplemented from Component.

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

References DistributeComponent::Copy(), DistributeComponent::PrecomputeIndexes(), DistributeComponent::Read(), and DistributeComponent::Write().

907  {
908  delete static_cast<CuMatrix<BaseFloat>*>(memo);
909  }

◆ GetMemo()

CuMatrix< BaseFloat > * GetMemo ( int32  num_mask_rows) const
private

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

References CuMatrixBase< Real >::Add(), CuMatrixBase< Real >::ApplyHeaviside(), GeneralDropoutComponent::block_dim_, GeneralDropoutComponent::continuous_, CuMatrixBase< Real >::CopyFromMat(), GeneralDropoutComponent::dropout_proportion_, rnnlm::i, KALDI_ASSERT, kaldi::kUndefined, rnnlm::n, kaldi::RandInt(), RandomComponent::random_generator_, kaldi::RandUniform(), CuMatrixBase< Real >::Scale(), MatrixBase< Real >::Set(), GeneralDropoutComponent::specaugment_max_proportion_, GeneralDropoutComponent::specaugment_max_regions_, kaldi::swap(), and RandomComponent::test_mode_.

Referenced by GeneralDropoutComponent::Propagate().

1740  {
1741  KALDI_ASSERT(num_mask_rows > 0 && !test_mode_ &&
1742  (dropout_proportion_ > 0.0 ||
1743  specaugment_max_proportion_ != 0.0));
1744  CuMatrix<BaseFloat> *ans = new CuMatrix<BaseFloat>(num_mask_rows, block_dim_,
1745  kUndefined);
1746 
1747  if (specaugment_max_proportion_ != 0.0) {
1748  // This block takes care of the case where we are doing SpecAugment.
1749  int32 num_freq_bins = block_dim_;
1750  Matrix<BaseFloat> mask(num_mask_rows, block_dim_);
1751  mask.Set(1.0);
1752  int32 specaugment_max_zeroed = static_cast<int32>(
1753  num_freq_bins * specaugment_max_proportion_ + 0.5);
1754  for (int32 seq = 0; seq < num_mask_rows; seq++) {
1755  // actually seq is more like a sub-part of a sequence, in the case where
1756  // time_period_ is not zero.
1757  SubVector<BaseFloat> this_mask(mask, seq); // will be all ones, right now.
1758  int32 num_bins_zeroed = RandInt(0, specaugment_max_zeroed);
1759  if (num_bins_zeroed != 0) {
1760  // This is not quite the same as the paper, it is allowed to "wrap around"
1761  // from the top to the bottom of the frequency spectrum.
1762  int32 start_bin = RandInt(0, num_freq_bins - 1);
1763  for (int32 i = start_bin; i < start_bin + num_bins_zeroed; i++)
1764  this_mask(i % num_freq_bins) = 0.0;
1765 
1766  // if specaugment_max_regions_ is not 1 (e.g. if it's 2 or 3), we want
1767  // to (possibly) split up the zeroed region into more segments.
1768  // The way we do this is a bit odd, but it was hard to think of
1769  // an elegant way to do it. We just choose a random half of the spectrum
1770  // (viewing it as a circle, so choosing a random half of the circle)
1771  // and swap around that half, i.e. flip it on its head.
1772  for (int32 n = 1; n < specaugment_max_regions_; n++) {
1773  int32 half_bin_size = num_freq_bins / 2,
1774  quarter_bin_size = half_bin_size / 2,
1775  start_bin = RandInt(0, num_freq_bins - 1),
1776  end_bin = start_bin + half_bin_size;
1777  for (int32 i = 0; i < quarter_bin_size; i++) {
1778  BaseFloat &a = this_mask((start_bin + i) % num_freq_bins),
1779  &b = this_mask((end_bin - i) % num_freq_bins);
1780  std::swap(a, b);
1781  }
1782  }
1783  }
1784  }
1785  ans->CopyFromMat(mask);
1786  return ans;
1787  }
1788 
1789  BaseFloat dropout_proportion = dropout_proportion_;
1790 
1791  // This const_cast is only safe assuming you don't attempt
1792  // to use multi-threaded code with the GPU.
1793  const_cast<CuRand<BaseFloat>&>(random_generator_).RandUniform(ans);
1794 
1795  if (!continuous_) {
1796  ans->Add(-dropout_proportion);
1797  // now, a proportion "dropout_proportion" will be < 0.0. After applying the
1798  // function (x>0?1:0), a proportion "dropout_proportion" will be zero and (1 -
1799  // dropout_proportion) will be 1.0.
1800  ans->ApplyHeaviside();
1801  ans->Scale(1.0 / (1.0 - dropout_proportion));
1802  } else {
1803  ans->Scale(dropout_proportion * 4.0);
1804  // make the expected value 1.0.
1805  ans->Add(1.0 - (2.0 * dropout_proportion));
1806  }
1807  return ans;
1808 }
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
Definition: kaldi-math.h:151
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
kaldi::int32 int32
float BaseFloat
Definition: kaldi-types.h:29
struct rnnlm::@11::@12 n
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
CuRand< BaseFloat > random_generator_
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95

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

References DropoutMaskComponent::continuous_, DropoutMaskComponent::dropout_proportion_, and DropoutMaskComponent::Type().

1529  {
1530  std::ostringstream stream;
1531  stream << Type()
1532  << ", dim=" << dim_
1533  << ", block-dim=" << block_dim_
1534  << ", dropout-proportion=" << dropout_proportion_;
1535  if (continuous_)
1536  stream << ", continuous=true";
1537  if (specaugment_max_proportion_ != 0)
1538  stream << ", specaugment-max-proportion=" << specaugment_max_proportion_
1539  << ", specaugment-max-regions=" << specaugment_max_regions_;
1540  if (time_period_ > 0)
1541  stream << ", time-period=" << time_period_;
1542  return stream.str();
1543 }
virtual std::string Type() const
Returns a string such as "SigmoidComponent", describing the type of the object.

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

References GeneralDropoutComponent::block_dim_, GeneralDropoutComponent::continuous_, GeneralDropoutComponent::dim_, GeneralDropoutComponent::dropout_proportion_, ConfigLine::GetValue(), KALDI_ASSERT, KALDI_ERR, GeneralDropoutComponent::specaugment_max_proportion_, GeneralDropoutComponent::specaugment_max_regions_, RandomComponent::test_mode_, and GeneralDropoutComponent::time_period_.

1703  {
1704  dim_ = 0;
1705  bool ok = cfl->GetValue("dim", &dim_);
1706  KALDI_ASSERT(ok && dim_ > 0);
1707  block_dim_ = dim_;
1708  cfl->GetValue("block-dim", &block_dim_);
1709  if (!(block_dim_ > 0 && dim_ % block_dim_ == 0))
1710  KALDI_ERR << "Invalid configuration dim=" << dim_
1711  << ", block-dim=" << block_dim_;
1712  time_period_ = 0;
1713  cfl->GetValue("time-period", &time_period_);
1714  dropout_proportion_ = 0.5;
1715  cfl->GetValue("dropout-proportion", &dropout_proportion_);
1716 
1718  cfl->GetValue("specaugment-max-proportion", &specaugment_max_proportion_);
1720  cfl->GetValue("specaugment-max-regions", &specaugment_max_regions_);
1721  continuous_ = false;
1722  cfl->GetValue("continuous", &continuous_);
1723  test_mode_ = false;
1724  cfl->GetValue("test-mode", &test_mode_);
1725 
1726  if (specaugment_max_proportion_ != 0.0) {
1727  if (specaugment_max_proportion_ < 0.0 ||
1730  KALDI_ERR << "Invalid config values: specaugment-max-proportion = "
1731  << specaugment_max_proportion_ << ", continuous = "
1732  << std::boolalpha << continuous_
1733  << ", specaugment-max-regions = " << specaugment_max_regions_;
1734  }
1735  }
1736 }
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ InputDim()

virtual int32 InputDim ( ) const
inlinevirtual

Returns input-dimension of this component.

Implements Component.

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

◆ operator=()

const GeneralDropoutComponent& operator= ( const GeneralDropoutComponent other)
private

◆ OutputDim()

virtual int32 OutputDim ( ) const
inlinevirtual

Returns output-dimension of this component.

Implements Component.

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

References Component::Info(), 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 1810 of file nnet-general-component.cc.

References GeneralDropoutComponent::block_dim_, CuArray< T >::CopyFromVec(), GeneralDropoutComponent::dim_, kaldi::DivideRoundingDown(), rnnlm::i, GeneralDropoutComponentPrecomputedIndexes::indexes, rnnlm::j, KALDI_ASSERT, rnnlm::n, GeneralDropoutComponentPrecomputedIndexes::num_mask_rows, and GeneralDropoutComponent::time_period_.

1814  {
1815  KALDI_ASSERT(input_indexes == output_indexes);
1816 
1817  GeneralDropoutComponentPrecomputedIndexes *ans = new
1818  GeneralDropoutComponentPrecomputedIndexes;
1819  int32 size = input_indexes.size(), time_period = time_period_,
1820  cur_row = 0;
1821  std::vector<int32> indexes(size);
1822  // the map 'm' will map from a pair from (n, t) value to the row-index of the
1823  // dropout-mask matrix*. However, the 't' isn't a real 't' value;
1824  // if time_period_ == 0, the 't' value will just be zero; otherwise,
1825  // it will be t divided by time_period_ (rounding towards negative infinity).
1826 
1827  // *before considering effects related to when block_dim_ != dim_.
1828 
1829  std::unordered_map<std::pair<int32,int32>, int32, PairHasher<int32> > m;
1830  for (int32 i = 0; i < size; i++) {
1831  int32 n = input_indexes[i].n,
1832  t = (time_period == 0 ? 0 : DivideRoundingDown(input_indexes[i].t,
1833  time_period));
1834  std::pair<int32, int32> p(n, t);
1835 
1836  std::unordered_map<std::pair<int32,int32>, int32,
1837  PairHasher<int32> >::const_iterator
1838  iter = m.find(p);
1839  if (iter != m.end()) {
1840  indexes[i] = iter->second;
1841  } else {
1842  m[p] = cur_row;
1843  indexes[i] = cur_row;
1844  cur_row++;
1845  }
1846  }
1847  int32 multiple = dim_ / block_dim_;
1848  ans->num_mask_rows = cur_row;
1849  if (multiple == 1) {
1850  ans->indexes.CopyFromVec(indexes);
1851  } else {
1852  ans->num_mask_rows = cur_row * multiple;
1853  std::vector<int32> repeated_indexes;
1854  repeated_indexes.reserve(size * multiple);
1855  for (int32 i = 0; i < size; i++) {
1856  int32 row = indexes[i];
1857  for (int32 j = 0; j < multiple; j++)
1858  repeated_indexes.push_back(row);
1859  }
1860  ans->indexes.CopyFromVec(repeated_indexes);
1861  }
1862  return ans;
1863 }
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
struct rnnlm::@11::@12 n
#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 1562 of file nnet-general-component.cc.

References GeneralDropoutComponent::block_dim_, CuMatrixBase< Real >::CopyFromMat(), CuMatrixBase< Real >::Data(), GeneralDropoutComponent::dim_, GeneralDropoutComponent::dropout_proportion_, GeneralDropoutComponent::GetMemo(), GeneralDropoutComponentPrecomputedIndexes::indexes, KALDI_ASSERT, CuMatrixBase< Real >::MulRows(), GeneralDropoutComponentPrecomputedIndexes::num_mask_rows, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), kaldi::SameDim(), GeneralDropoutComponent::specaugment_max_proportion_, CuMatrixBase< Real >::Stride(), and RandomComponent::test_mode_.

1565  {
1566 
1567  KALDI_ASSERT(SameDim(in, *out));
1568 
1569  // The following will do nothing if 'out' and 'in' refer to the same data.
1570  out->CopyFromMat(in);
1571 
1572  if (test_mode_ ||
1574  return NULL;
1575 
1576  const GeneralDropoutComponentPrecomputedIndexes *indexes =
1577  dynamic_cast<const GeneralDropoutComponentPrecomputedIndexes*>(indexes_in);
1578  KALDI_ASSERT(indexes != NULL);
1579 
1580  CuMatrix<BaseFloat> *mask = GetMemo(indexes->num_mask_rows);
1581 
1582  if (block_dim_ < dim_) {
1583  KALDI_ASSERT(out->Stride() == out->NumCols());
1584  int32 num_rows = out->NumRows(),
1585  dim_multiple = dim_ / block_dim_,
1586  num_rows_reshaped = num_rows * dim_multiple;
1587  CuSubMatrix<BaseFloat> out_reshaped(out->Data(), num_rows_reshaped,
1589  out_reshaped.MulRows(*mask, indexes->indexes);
1590  } else {
1591  out->MulRows(*mask, indexes->indexes);
1592  }
1593  return mask;
1594 }
kaldi::int32 int32
bool SameDim(const MatrixBase< Real > &M, const MatrixBase< Real > &N)
CuMatrix< BaseFloat > * GetMemo(int32 num_mask_rows) const
#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 1636 of file nnet-general-component.cc.

References GeneralDropoutComponent::block_dim_, GeneralDropoutComponent::continuous_, GeneralDropoutComponent::dim_, GeneralDropoutComponent::dropout_proportion_, kaldi::ExpectOneOrTwoTokens(), kaldi::nnet3::ExpectToken(), kaldi::PeekToken(), kaldi::ReadBasicType(), GeneralDropoutComponent::specaugment_max_proportion_, GeneralDropoutComponent::specaugment_max_regions_, RandomComponent::test_mode_, and GeneralDropoutComponent::time_period_.

1636  {
1637  ExpectOneOrTwoTokens(is, binary, "<GeneralDropoutComponent>", "<Dim>");
1638  ReadBasicType(is, binary, &dim_);
1639  ExpectToken(is, binary, "<BlockDim>");
1640  ReadBasicType(is, binary, &block_dim_);
1641  ExpectToken(is, binary, "<TimePeriod>");
1642  ReadBasicType(is, binary, &time_period_);
1643  ExpectToken(is, binary, "<DropoutProportion>");
1644  ReadBasicType(is, binary, &dropout_proportion_);
1645  if (PeekToken(is, binary) == 'S') {
1646  ExpectToken(is, binary, "<SpecAugmentMaxProportion>");
1648  if (PeekToken(is, binary) == 'S') {
1649  ExpectToken(is, binary, "<SpecAugmentMaxRegions>");
1651  } else {
1653  }
1654  } else {
1657  }
1658  if (PeekToken(is, binary) == 'T') {
1659  ExpectToken(is, binary, "<TestMode>");
1660  test_mode_ = true;
1661  } else {
1662  test_mode_ = false;
1663  }
1664  if (PeekToken(is, binary) == 'C') {
1665  ExpectToken(is, binary, "<Continuous>");
1666  continuous_ = true;
1667  } else {
1668  continuous_ = false;
1669  }
1670  ExpectToken(is, binary, "</GeneralDropoutComponent>");
1671 }
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)
int PeekToken(std::istream &is, bool binary)
PeekToken will return the first character of the next token, or -1 if end of file.
Definition: io-funcs.cc:170

◆ SetDropoutProportion()

void SetDropoutProportion ( BaseFloat  p)
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 889 of file nnet-general-component.h.

Referenced by SpecAugmentTimeMaskComponent::Info().

889 { return "GeneralDropoutComponent"; }

◆ Write()

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

Write component to stream.

Implements Component.

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

References GeneralDropoutComponent::block_dim_, GeneralDropoutComponent::continuous_, GeneralDropoutComponent::dim_, GeneralDropoutComponent::dropout_proportion_, GeneralDropoutComponent::specaugment_max_proportion_, GeneralDropoutComponent::specaugment_max_regions_, RandomComponent::test_mode_, GeneralDropoutComponent::time_period_, kaldi::WriteBasicType(), and kaldi::WriteToken().

1674  {
1675  WriteToken(os, binary, "<GeneralDropoutComponent>");
1676  WriteToken(os, binary, "<Dim>");
1677  WriteBasicType(os, binary, dim_);
1678  WriteToken(os, binary, "<BlockDim>");
1679  WriteBasicType(os, binary, block_dim_);
1680  WriteToken(os, binary, "<TimePeriod>");
1681  WriteBasicType(os, binary, time_period_);
1682  WriteToken(os, binary, "<DropoutProportion>");
1683  WriteBasicType(os, binary, dropout_proportion_);
1685  WriteToken(os, binary, "<SpecAugmentMaxProportion>");
1687  if (specaugment_max_regions_ != 1) {
1688  WriteToken(os, binary, "<SpecAugmentMaxRegions>");
1690  }
1691  }
1692  if (test_mode_)
1693  WriteToken(os, binary, "<TestMode>");
1694  if (continuous_)
1695  WriteToken(os, binary, "<Continuous>");
1696  WriteToken(os, binary, "</GeneralDropoutComponent>");
1697 }
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

◆ block_dim_

◆ continuous_

◆ dim_

◆ dropout_proportion_

◆ specaugment_max_proportion_

◆ specaugment_max_regions_

◆ time_period_


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