VectorRandomizer Class Reference

Randomizes elements of a vector according to a mask. More...

#include <nnet-randomizer.h>

Collaboration diagram for VectorRandomizer:

Public Member Functions

 VectorRandomizer ()
 
 VectorRandomizer (const NnetDataRandomizerOptions &conf)
 
void Init (const NnetDataRandomizerOptions &conf)
 Set the randomizer parameters (size) More...
 
void AddData (const Vector< BaseFloat > &v)
 Add data to randomization buffer. More...
 
bool IsFull ()
 Returns true, when capacity is full. More...
 
int32 NumFrames ()
 Number of frames stored inside the Randomizer. More...
 
void Randomize (const std::vector< int32 > &mask)
 Randomize matrix row-order using mask. More...
 
bool Done ()
 Returns true, if no more data for another mini-batch (after current one) More...
 
void Next ()
 Sets cursor to next mini-batch. More...
 
const Vector< BaseFloat > & Value ()
 Returns matrix-window with next mini-batch. More...
 

Private Attributes

Vector< BaseFloatdata_
 
Vector< BaseFloatminibatch_
 
int32 data_begin_
 A cursor, pointing to the 'row' where the next mini-batch begins,. More...
 
int32 data_end_
 A cursor, pointing to the 'row' after the end of data,. More...
 
NnetDataRandomizerOptions conf_
 

Detailed Description

Randomizes elements of a vector according to a mask.

Definition at line 148 of file nnet-randomizer.h.

Constructor & Destructor Documentation

◆ VectorRandomizer() [1/2]

VectorRandomizer ( )
inline

Definition at line 150 of file nnet-randomizer.h.

150  :
151  data_begin_(0),
152  data_end_(0)
153  { }
int32 data_end_
A cursor, pointing to the &#39;row&#39; after the end of data,.
int32 data_begin_
A cursor, pointing to the &#39;row&#39; where the next mini-batch begins,.

◆ VectorRandomizer() [2/2]

VectorRandomizer ( const NnetDataRandomizerOptions conf)
inlineexplicit

Definition at line 155 of file nnet-randomizer.h.

155  :
156  data_begin_(0),
157  data_end_(0)
158  {
159  Init(conf);
160  }
int32 data_end_
A cursor, pointing to the &#39;row&#39; after the end of data,.
int32 data_begin_
A cursor, pointing to the &#39;row&#39; where the next mini-batch begins,.
void Init(const NnetDataRandomizerOptions &conf)
Set the randomizer parameters (size)

Member Function Documentation

◆ AddData()

void AddData ( const Vector< BaseFloat > &  v)

Add data to randomization buffer.

Definition at line 114 of file nnet-randomizer.cc.

References data_, VectorBase< Real >::Dim(), and KALDI_ASSERT.

Referenced by main(), and UnitTestVectorRandomizer().

114  {
115  // pre-allocate before 1st use
116  if (data_.Dim() == 0) {
117  data_.Resize(conf_.randomizer_size);
118  }
119  // optionally put previous left-over to front
120  if (data_begin_ > 0) {
121  KALDI_ASSERT(data_begin_ <= data_end_); // sanity check
122  int32 leftover = data_end_ - data_begin_;
123  KALDI_ASSERT(leftover < data_begin_); // no overlap
124  if (leftover > 0) {
125  data_.Range(0, leftover).CopyFromVec(data_.Range(data_begin_, leftover));
126  }
127  data_begin_ = 0;
128  data_end_ = leftover;
129  data_.Range(leftover, data_.Dim()-leftover).SetZero(); // zeroing the rest
130  }
131  // extend the buffer if necessary
132  if (data_.Dim() < data_end_ + v.Dim()) {
133  Vector<BaseFloat> data_aux(data_);
134  data_.Resize(data_end_ + v.Dim() + 1000); // +1000 row surplus
135  data_.Range(0, data_aux.Dim()).CopyFromVec(data_aux);
136  }
137  // copy the data
138  data_.Range(data_end_, v.Dim()).CopyFromVec(v);
139  data_end_ += v.Dim();
140 }
int32 data_end_
A cursor, pointing to the &#39;row&#39; after the end of data,.
NnetDataRandomizerOptions conf_
kaldi::int32 int32
int32 data_begin_
A cursor, pointing to the &#39;row&#39; where the next mini-batch begins,.
int32 randomizer_size
Maximum number of samples we have in memory,.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Done()

bool Done ( )
inline

Returns true, if no more data for another mini-batch (after current one)

Definition at line 184 of file nnet-randomizer.h.

Referenced by UnitTestVectorRandomizer().

184  {
186  }
int32 data_end_
A cursor, pointing to the &#39;row&#39; after the end of data,.
NnetDataRandomizerOptions conf_
int32 data_begin_
A cursor, pointing to the &#39;row&#39; where the next mini-batch begins,.

◆ Init()

void Init ( const NnetDataRandomizerOptions conf)
inline

Set the randomizer parameters (size)

Definition at line 163 of file nnet-randomizer.h.

Referenced by UnitTestVectorRandomizer().

163  {
164  conf_ = conf;
165  }
NnetDataRandomizerOptions conf_

◆ IsFull()

bool IsFull ( )
inline

Returns true, when capacity is full.

Definition at line 171 of file nnet-randomizer.h.

Referenced by UnitTestVectorRandomizer().

171  {
172  return ((data_begin_ == 0) && (data_end_ > conf_.randomizer_size ));
173  }
int32 data_end_
A cursor, pointing to the &#39;row&#39; after the end of data,.
NnetDataRandomizerOptions conf_
int32 data_begin_
A cursor, pointing to the &#39;row&#39; where the next mini-batch begins,.
int32 randomizer_size
Maximum number of samples we have in memory,.

◆ Next()

void Next ( )

Sets cursor to next mini-batch.

Definition at line 154 of file nnet-randomizer.cc.

Referenced by main(), and UnitTestVectorRandomizer().

154  {
156 }
NnetDataRandomizerOptions conf_
int32 data_begin_
A cursor, pointing to the &#39;row&#39; where the next mini-batch begins,.

◆ NumFrames()

int32 NumFrames ( )
inline

Number of frames stored inside the Randomizer.

Definition at line 176 of file nnet-randomizer.h.

References kaldi::cu::Randomize().

Referenced by UnitTestVectorRandomizer().

176  {
177  return data_end_;
178  }
int32 data_end_
A cursor, pointing to the &#39;row&#39; after the end of data,.

◆ Randomize()

void Randomize ( const std::vector< int32 > &  mask)

Randomize matrix row-order using mask.

Definition at line 142 of file nnet-randomizer.cc.

References data_, rnnlm::i, and KALDI_ASSERT.

Referenced by main(), and UnitTestVectorRandomizer().

142  {
144  KALDI_ASSERT(data_end_ > 0);
145  KALDI_ASSERT(data_end_ == mask.size());
146  // Use auxiliary buffer for unshuffled data
147  Vector<BaseFloat> data_aux(data_);
148  // randomize the data, mask is used to index elements in source vector
149  for (int32 i = 0; i < mask.size(); i++) {
150  data_(i) = data_aux(mask.at(i));
151  }
152 }
int32 data_end_
A cursor, pointing to the &#39;row&#39; after the end of data,.
kaldi::int32 int32
int32 data_begin_
A cursor, pointing to the &#39;row&#39; where the next mini-batch begins,.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Value()

const Vector< BaseFloat > & Value ( )

Returns matrix-window with next mini-batch.

Definition at line 158 of file nnet-randomizer.cc.

References data_, KALDI_ASSERT, and kaldi::kUndefined.

Referenced by main(), and UnitTestVectorRandomizer().

158  {
159  // make sure we have data for next minibatch,
161  // prepare the mini-batch buffer,
163  minibatch_.CopyFromVec(data_.Range(data_begin_, conf_.minibatch_size));
164  return minibatch_;
165 }
int32 data_end_
A cursor, pointing to the &#39;row&#39; after the end of data,.
NnetDataRandomizerOptions conf_
int32 data_begin_
A cursor, pointing to the &#39;row&#39; where the next mini-batch begins,.
Vector< BaseFloat > minibatch_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

Member Data Documentation

◆ conf_

Definition at line 203 of file nnet-randomizer.h.

◆ data_

Vector<BaseFloat> data_
private

Definition at line 195 of file nnet-randomizer.h.

◆ data_begin_

int32 data_begin_
private

A cursor, pointing to the 'row' where the next mini-batch begins,.

Definition at line 199 of file nnet-randomizer.h.

◆ data_end_

int32 data_end_
private

A cursor, pointing to the 'row' after the end of data,.

Definition at line 201 of file nnet-randomizer.h.

◆ minibatch_

Vector<BaseFloat> minibatch_
private

Definition at line 196 of file nnet-randomizer.h.


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