All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 ( )
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 'row' after the end of data,.
int32 data_begin_
A cursor, pointing to the 'row' where the next mini-batch begins,.
VectorRandomizer ( const NnetDataRandomizerOptions conf)
inlineexplicit

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

References VectorRandomizer::Init().

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

Member Function Documentation

void AddData ( const Vector< BaseFloat > &  v)

Add data to randomization buffer.

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

References VectorRandomizer::conf_, VectorRandomizer::data_, VectorRandomizer::data_begin_, VectorRandomizer::data_end_, VectorBase< Real >::Dim(), KALDI_ASSERT, NnetDataRandomizerOptions::randomizer_size, VectorBase< Real >::Range(), and Vector< Real >::Resize().

Referenced by main(), and UnitTestVectorRandomizer().

112  {
113  // pre-allocate before 1st use
114  if (data_.Dim() == 0) {
116  }
117  // optionally put previous left-over to front
118  if (data_begin_ > 0) {
119  KALDI_ASSERT(data_begin_ <= data_end_); // sanity check
120  int32 leftover = data_end_ - data_begin_;
121  KALDI_ASSERT(leftover < data_begin_); // no overlap
122  if (leftover > 0) {
123  data_.Range(0, leftover).CopyFromVec(data_.Range(data_begin_, leftover));
124  }
125  data_begin_ = 0;
126  data_end_ = leftover;
127  data_.Range(leftover, data_.Dim()-leftover).SetZero(); // zeroing the rest
128  }
129  // extend the buffer if necessary
130  if (data_.Dim() < data_end_ + v.Dim()) {
131  Vector<BaseFloat> data_aux(data_);
132  data_.Resize(data_end_ + v.Dim() + 1000); // +1000 row surplus
133  data_.Range(0, data_aux.Dim()).CopyFromVec(data_aux);
134  }
135  // copy the data
136  data_.Range(data_end_, v.Dim()).CopyFromVec(v);
137  data_end_ += v.Dim();
138 }
int32 data_end_
A cursor, pointing to the 'row' after the end of data,.
NnetDataRandomizerOptions conf_
int32 data_begin_
A cursor, pointing to the 'row' where the next mini-batch begins,.
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
int32 randomizer_size
Maximum number of samples we have in memory,.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62
SubVector< Real > Range(const MatrixIndexT o, const MatrixIndexT l)
Returns a sub-vector of a vector (a range of elements).
Definition: kaldi-vector.h:92
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.

References VectorRandomizer::conf_, VectorRandomizer::data_begin_, VectorRandomizer::data_end_, and NnetDataRandomizerOptions::minibatch_size.

Referenced by UnitTestVectorRandomizer().

184  {
186  }
int32 data_end_
A cursor, pointing to the 'row' after the end of data,.
NnetDataRandomizerOptions conf_
int32 data_begin_
A cursor, pointing to the 'row' where the next mini-batch begins,.
void Init ( const NnetDataRandomizerOptions conf)
inline

Set the randomizer parameters (size)

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

References VectorRandomizer::conf_.

Referenced by UnitTestVectorRandomizer(), and VectorRandomizer::VectorRandomizer().

163  {
164  conf_ = conf;
165  }
NnetDataRandomizerOptions conf_
bool IsFull ( )
inline

Returns true, when capacity is full.

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

References VectorRandomizer::conf_, VectorRandomizer::data_begin_, VectorRandomizer::data_end_, and NnetDataRandomizerOptions::randomizer_size.

Referenced by UnitTestVectorRandomizer().

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

Sets cursor to next mini-batch.

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

References VectorRandomizer::conf_, VectorRandomizer::data_begin_, and NnetDataRandomizerOptions::minibatch_size.

Referenced by main(), and UnitTestVectorRandomizer().

152  {
154 }
NnetDataRandomizerOptions conf_
int32 data_begin_
A cursor, pointing to the 'row' where the next mini-batch begins,.
int32 NumFrames ( )
inline

Number of frames stored inside the Randomizer.

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

References VectorRandomizer::data_end_.

Referenced by UnitTestVectorRandomizer().

176  {
177  return data_end_;
178  }
int32 data_end_
A cursor, pointing to the 'row' after the end of data,.
void Randomize ( const std::vector< int32 > &  mask)

Randomize matrix row-order using mask.

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

References VectorRandomizer::data_, VectorRandomizer::data_begin_, VectorRandomizer::data_end_, rnnlm::i, and KALDI_ASSERT.

Referenced by main(), and UnitTestVectorRandomizer().

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

Returns matrix-window with next mini-batch.

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

References VectorRandomizer::conf_, VectorBase< Real >::CopyFromVec(), VectorRandomizer::data_, VectorRandomizer::data_begin_, VectorRandomizer::data_end_, KALDI_ASSERT, kaldi::kUndefined, VectorRandomizer::minibatch_, NnetDataRandomizerOptions::minibatch_size, VectorBase< Real >::Range(), and Vector< Real >::Resize().

Referenced by main(), and UnitTestVectorRandomizer().

156  {
157  // make sure we have data for next minibatch,
159  // prepare the mini-batch buffer,
162  return minibatch_;
163 }
int32 data_end_
A cursor, pointing to the 'row' after the end of data,.
NnetDataRandomizerOptions conf_
int32 data_begin_
A cursor, pointing to the 'row' where the next mini-batch begins,.
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
Vector< BaseFloat > minibatch_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
SubVector< Real > Range(const MatrixIndexT o, const MatrixIndexT l)
Returns a sub-vector of a vector (a range of elements).
Definition: kaldi-vector.h:92

Member Data Documentation

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.

Referenced by VectorRandomizer::AddData(), VectorRandomizer::Done(), VectorRandomizer::IsFull(), VectorRandomizer::Next(), VectorRandomizer::Randomize(), and VectorRandomizer::Value().

int32 data_end_
private
Vector<BaseFloat> minibatch_
private

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

Referenced by VectorRandomizer::Value().


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