All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DecodableNnetSimpleLooped Class Reference

#include <decodable-simple-looped.h>

Collaboration diagram for DecodableNnetSimpleLooped:

Public Member Functions

 DecodableNnetSimpleLooped (const DecodableNnetSimpleLoopedInfo &info, const MatrixBase< BaseFloat > &feats, const VectorBase< BaseFloat > *ivector=NULL, const MatrixBase< BaseFloat > *online_ivectors=NULL, int32 online_ivector_period=1)
 This constructor takes features as input, and you can either supply a single iVector input, estimated in batch-mode ('ivector'), or 'online' iVectors ('online_ivectors' and 'online_ivector_period', or none at all. More...
 
int32 NumFrames () const
 
int32 OutputDim () const
 
void GetOutputForFrame (int32 subsampled_frame, VectorBase< BaseFloat > *output)
 
BaseFloat GetOutput (int32 subsampled_frame, int32 pdf_id)
 

Private Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (DecodableNnetSimpleLooped)
 
void AdvanceChunk ()
 
void AdvanceChunkInternal (const MatrixBase< BaseFloat > &input_feats, const VectorBase< BaseFloat > &ivector)
 
void GetCurrentIvector (int32 input_frame, Vector< BaseFloat > *ivector)
 
int32 GetIvectorDim () const
 

Private Attributes

const
DecodableNnetSimpleLoopedInfo
info_
 
NnetComputer computer_
 
const MatrixBase< BaseFloat > & feats_
 
int32 num_subsampled_frames_
 
const VectorBase< BaseFloat > * ivector_
 
const MatrixBase< BaseFloat > * online_ivector_feats_
 
int32 online_ivector_period_
 
Matrix< BaseFloatcurrent_log_post_
 
int32 num_chunks_computed_
 
int32 current_log_post_subsampled_offset_
 

Detailed Description

Definition at line 161 of file decodable-simple-looped.h.

Constructor & Destructor Documentation

DecodableNnetSimpleLooped ( const DecodableNnetSimpleLoopedInfo info,
const MatrixBase< BaseFloat > &  feats,
const VectorBase< BaseFloat > *  ivector = NULL,
const MatrixBase< BaseFloat > *  online_ivectors = NULL,
int32  online_ivector_period = 1 
)

This constructor takes features as input, and you can either supply a single iVector input, estimated in batch-mode ('ivector'), or 'online' iVectors ('online_ivectors' and 'online_ivector_period', or none at all.

Note: it stores references to all arguments to the constructor, so don't delete them till this goes out of scope.

Parameters
[in]infoThis helper class contains all the static pre-computed information this class needs, and contains a pointer to the neural net.
[in]featsThe input feature matrix.
[in]ivectorIf you are using iVectors estimated in batch mode, a pointer to the iVector, else NULL.
[in]ivectorIf you are using iVectors estimated in batch mode, a pointer to the iVector, else NULL.
[in]online_ivectorsIf you are using iVectors estimated 'online' a pointer to the iVectors, else NULL.
[in]online_ivector_periodIf you are using iVectors estimated 'online' (i.e. if online_ivectors != NULL) gives the periodicity (in frames) with which the iVectors are estimated.

Definition at line 95 of file decodable-simple-looped.cc.

References DecodableNnetSimpleLooped::feats_, NnetSimpleLoopedComputationOptions::frame_subsampling_factor, DecodableNnetSimpleLooped::info_, KALDI_ASSERT, DecodableNnetSimpleLooped::num_subsampled_frames_, MatrixBase< Real >::NumRows(), and DecodableNnetSimpleLoopedInfo::opts.

100  :
101  info_(info),
103  info_.nnet, NULL), // NULL is 'nnet_to_update'
104  feats_(feats),
105  ivector_(ivector), online_ivector_feats_(online_ivectors),
106  online_ivector_period_(online_ivector_period),
112  KALDI_ASSERT(!(ivector != NULL && online_ivectors != NULL));
113  KALDI_ASSERT(!(online_ivectors != NULL && online_ivector_period <= 0 &&
114  "You need to set the --online-ivector-period option!"));
115 }
const VectorBase< BaseFloat > * ivector_
const MatrixBase< BaseFloat > * online_ivector_feats_
const NnetSimpleLoopedComputationOptions & opts
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
const DecodableNnetSimpleLoopedInfo & info_

Member Function Documentation

void AdvanceChunk ( )
private

Definition at line 139 of file decodable-simple-looped.cc.

References NnetComputer::AcceptInput(), NnetSimpleLoopedComputationOptions::acoustic_scale, CuMatrixBase< Real >::AddVecToRows(), DecodableNnetSimpleLooped::computer_, MatrixBase< Real >::CopyFromMat(), MatrixBase< Real >::CopyRowsFromVec(), DecodableNnetSimpleLooped::current_log_post_, DecodableNnetSimpleLooped::current_log_post_subsampled_offset_, VectorBase< Real >::Dim(), CuVectorBase< Real >::Dim(), DecodableNnetSimpleLooped::feats_, NnetSimpleLoopedComputationOptions::frame_subsampling_factor, DecodableNnetSimpleLoopedInfo::frames_left_context, DecodableNnetSimpleLoopedInfo::frames_per_chunk, DecodableNnetSimpleLoopedInfo::frames_right_context, DecodableNnetSimpleLooped::GetCurrentIvector(), NnetComputer::GetOutputDestructive(), DecodableNnetSimpleLoopedInfo::has_ivectors, DecodableNnetSimpleLooped::info_, ComputationRequest::inputs, KALDI_ASSERT, kaldi::kUndefined, DecodableNnetSimpleLoopedInfo::log_priors, DecodableNnetSimpleLooped::num_chunks_computed_, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), DecodableNnetSimpleLoopedInfo::opts, DecodableNnetSimpleLoopedInfo::output_dim, DecodableNnetSimpleLoopedInfo::request1, DecodableNnetSimpleLoopedInfo::request2, Matrix< Real >::Resize(), MatrixBase< Real >::Row(), CuMatrixBase< Real >::Row(), NnetComputer::Run(), CuMatrixBase< Real >::Scale(), and Matrix< Real >::Swap().

Referenced by DecodableNnetSimpleLooped::GetOutput(), and DecodableNnetSimpleLooped::GetOutputForFrame().

139  {
140  int32 begin_input_frame, end_input_frame;
141  if (num_chunks_computed_ == 0) {
142  begin_input_frame = -info_.frames_left_context;
143  // note: end is last plus one.
144  end_input_frame = info_.frames_per_chunk + info_.frames_right_context;
145  } else {
146  begin_input_frame = num_chunks_computed_ * info_.frames_per_chunk +
148  end_input_frame = begin_input_frame + info_.frames_per_chunk;
149  }
150  CuMatrix<BaseFloat> feats_chunk(end_input_frame - begin_input_frame,
152 
153  int32 num_features = feats_.NumRows();
154  if (begin_input_frame >= 0 && end_input_frame <= num_features) {
155  SubMatrix<BaseFloat> this_feats(feats_,
156  begin_input_frame,
157  end_input_frame - begin_input_frame,
158  0, feats_.NumCols());
159  feats_chunk.CopyFromMat(this_feats);
160  } else {
161  Matrix<BaseFloat> this_feats(end_input_frame - begin_input_frame,
162  feats_.NumCols());
163  for (int32 r = begin_input_frame; r < end_input_frame; r++) {
164  int32 input_frame = r;
165  if (input_frame < 0) input_frame = 0;
166  if (input_frame >= num_features) input_frame = num_features - 1;
167  this_feats.Row(r - begin_input_frame).CopyFromVec(
168  feats_.Row(input_frame));
169  }
170  feats_chunk.CopyFromMat(this_feats);
171  }
172  computer_.AcceptInput("input", &feats_chunk);
173 
174  if (info_.has_ivectors) {
175  KALDI_ASSERT(info_.request1.inputs.size() == 2);
176  // all but the 1st chunk should have 1 iVector, but no need
177  // to assume this.
178  int32 num_ivectors = (num_chunks_computed_ == 0 ?
179  info_.request1.inputs[1].indexes.size() :
180  info_.request2.inputs[1].indexes.size());
181  KALDI_ASSERT(num_ivectors > 0);
182 
183  Vector<BaseFloat> ivector;
184  // we just get the iVector from the last input frame we needed...
185  // we don't bother trying to be 'accurate' in getting the iVectors
186  // for their 'correct' frames, because in general using the
187  // iVector from as large 't' as possible will be better.
188  GetCurrentIvector(end_input_frame, &ivector);
189  Matrix<BaseFloat> ivectors(num_ivectors,
190  ivector.Dim());
191  ivectors.CopyRowsFromVec(ivector);
192  CuMatrix<BaseFloat> cu_ivectors(ivectors);
193  computer_.AcceptInput("ivector", &cu_ivectors);
194  }
195  computer_.Run();
196 
197  {
198  // Note: it's possible in theory that if you had weird recurrence that went
199  // directly from the output, the call to GetOutputDestructive() would cause
200  // a crash on the next chunk. If that happens, GetOutput() should be used
201  // instead of GetOutputDestructive(). But we don't anticipate this will
202  // happen in practice.
203  CuMatrix<BaseFloat> output;
204  computer_.GetOutputDestructive("output", &output);
205 
206  if (info_.log_priors.Dim() != 0) {
207  // subtract log-prior (divide by prior)
208  output.AddVecToRows(-1.0, info_.log_priors);
209  }
210  // apply the acoustic scale
211  output.Scale(info_.opts.acoustic_scale);
213  current_log_post_.Swap(&output);
214  }
218 
220 
222  (num_chunks_computed_ - 1) *
224 }
void GetCurrentIvector(int32 input_frame, Vector< BaseFloat > *ivector)
std::vector< IoSpecification > inputs
void Swap(Matrix< Real > *other)
Swaps the contents of *this and *other. Shallow swap.
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
const NnetSimpleLoopedComputationOptions & opts
void AcceptInput(const std::string &node_name, CuMatrix< BaseFloat > *input)
e.g.
MatrixIndexT Dim() const
Dimensions.
Definition: cu-vector.h:67
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
void GetOutputDestructive(const std::string &output_name, CuMatrix< BaseFloat > *output)
const DecodableNnetSimpleLoopedInfo & info_
void Run()
This does either the forward or backward computation, depending when it is called (in a typical compu...
void AdvanceChunkInternal ( const MatrixBase< BaseFloat > &  input_feats,
const VectorBase< BaseFloat > &  ivector 
)
private
void GetCurrentIvector ( int32  input_frame,
Vector< BaseFloat > *  ivector 
)
private

Definition at line 227 of file decodable-simple-looped.cc.

References DecodableNnetSimpleLoopedInfo::has_ivectors, DecodableNnetSimpleLooped::info_, DecodableNnetSimpleLooped::ivector_, KALDI_ASSERT, KALDI_ERR, MatrixBase< Real >::NumRows(), DecodableNnetSimpleLooped::online_ivector_feats_, DecodableNnetSimpleLooped::online_ivector_period_, and MatrixBase< Real >::Row().

Referenced by DecodableNnetSimpleLooped::AdvanceChunk().

228  {
229  if (!info_.has_ivectors)
230  return;
231  if (ivector_ != NULL) {
232  *ivector = *ivector_;
233  return;
234  } else if (online_ivector_feats_ == NULL) {
235  KALDI_ERR << "Neural net expects iVectors but none provided.";
236  }
238  int32 ivector_frame = input_frame / online_ivector_period_;
239  KALDI_ASSERT(ivector_frame >= 0);
240  if (ivector_frame >= online_ivector_feats_->NumRows())
241  ivector_frame = online_ivector_feats_->NumRows() - 1;
242  KALDI_ASSERT(ivector_frame >= 0 && "ivector matrix cannot be empty.");
243  *ivector = online_ivector_feats_->Row(ivector_frame);
244 }
const VectorBase< BaseFloat > * ivector_
const MatrixBase< BaseFloat > * online_ivector_feats_
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
#define KALDI_ERR
Definition: kaldi-error.h:127
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
const DecodableNnetSimpleLoopedInfo & info_
int32 GetIvectorDim ( ) const
private

Definition at line 129 of file decodable-simple-looped.cc.

References VectorBase< Real >::Dim(), DecodableNnetSimpleLooped::ivector_, MatrixBase< Real >::NumCols(), and DecodableNnetSimpleLooped::online_ivector_feats_.

129  {
130  if (ivector_ != NULL)
131  return ivector_->Dim();
132  else if (online_ivector_feats_ != NULL)
133  return online_ivector_feats_->NumCols();
134  else
135  return 0;
136 }
const VectorBase< BaseFloat > * ivector_
const MatrixBase< BaseFloat > * online_ivector_feats_
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62
BaseFloat GetOutput ( int32  subsampled_frame,
int32  pdf_id 
)
inline

Definition at line 208 of file decodable-simple-looped.h.

References DecodableNnetSimpleLooped::AdvanceChunk(), DecodableNnetSimpleLooped::current_log_post_, DecodableNnetSimpleLooped::current_log_post_subsampled_offset_, KALDI_ASSERT, and MatrixBase< Real >::NumRows().

Referenced by DecodableAmNnetSimpleLooped::LogLikelihood().

208  {
210  "Frames must be accessed in order.");
211  while (subsampled_frame >= current_log_post_subsampled_offset_ +
213  AdvanceChunk();
214  return current_log_post_(subsampled_frame -
216  pdf_id);
217  }
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void GetOutputForFrame ( int32  subsampled_frame,
VectorBase< BaseFloat > *  output 
)

Definition at line 118 of file decodable-simple-looped.cc.

References DecodableNnetSimpleLooped::AdvanceChunk(), VectorBase< Real >::CopyFromVec(), DecodableNnetSimpleLooped::current_log_post_, DecodableNnetSimpleLooped::current_log_post_subsampled_offset_, KALDI_ASSERT, MatrixBase< Real >::NumRows(), and MatrixBase< Real >::Row().

Referenced by kaldi::nnet3::TestNnetDecodable().

119  {
121  "Frames must be accessed in order.");
122  while (subsampled_frame >= current_log_post_subsampled_offset_ +
124  AdvanceChunk();
126  subsampled_frame - current_log_post_subsampled_offset_));
127 }
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
KALDI_DISALLOW_COPY_AND_ASSIGN ( DecodableNnetSimpleLooped  )
private
int32 OutputDim ( ) const
inline

Member Data Documentation

NnetComputer computer_
private

Definition at line 237 of file decodable-simple-looped.h.

Referenced by DecodableNnetSimpleLooped::AdvanceChunk().

int32 current_log_post_subsampled_offset_
private
int32 num_chunks_computed_
private

Definition at line 259 of file decodable-simple-looped.h.

Referenced by DecodableNnetSimpleLooped::AdvanceChunk().

int32 num_subsampled_frames_
private
const MatrixBase<BaseFloat>* online_ivector_feats_
private
int32 online_ivector_period_
private

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