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 DecodableNnetSimpleLoopedInfoinfo_
 
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()

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 96 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.

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

Member Function Documentation

◆ AdvanceChunk()

void AdvanceChunk ( )
private

Definition at line 140 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::GetOutputForFrame().

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

◆ AdvanceChunkInternal()

void AdvanceChunkInternal ( const MatrixBase< BaseFloat > &  input_feats,
const VectorBase< BaseFloat > &  ivector 
)
private

◆ GetCurrentIvector()

void GetCurrentIvector ( int32  input_frame,
Vector< BaseFloat > *  ivector 
)
private

Definition at line 228 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().

229  {
230  if (!info_.has_ivectors)
231  return;
232  if (ivector_ != NULL) {
233  *ivector = *ivector_;
234  return;
235  } else if (online_ivector_feats_ == NULL) {
236  KALDI_ERR << "Neural net expects iVectors but none provided.";
237  }
239  int32 ivector_frame = input_frame / online_ivector_period_;
240  KALDI_ASSERT(ivector_frame >= 0);
241  if (ivector_frame >= online_ivector_feats_->NumRows())
242  ivector_frame = online_ivector_feats_->NumRows() - 1;
243  KALDI_ASSERT(ivector_frame >= 0 && "ivector matrix cannot be empty.");
244  *ivector = online_ivector_feats_->Row(ivector_frame);
245 }
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:185
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
const DecodableNnetSimpleLoopedInfo & info_

◆ GetIvectorDim()

int32 GetIvectorDim ( ) const
private

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

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

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

◆ GetOutput()

BaseFloat GetOutput ( int32  subsampled_frame,
int32  pdf_id 
)
inline

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

References KALDI_ASSERT, and KALDI_DISALLOW_COPY_AND_ASSIGN.

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  }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61

◆ GetOutputForFrame()

void GetOutputForFrame ( int32  subsampled_frame,
VectorBase< BaseFloat > *  output 
)

Definition at line 119 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().

120  {
122  "Frames must be accessed in order.");
123  while (subsampled_frame >= current_log_post_subsampled_offset_ +
125  AdvanceChunk();
127  subsampled_frame - current_log_post_subsampled_offset_));
128 }
void CopyFromVec(const VectorBase< Real > &v)
Copy data from another vector (must match own size).
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:185
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( DecodableNnetSimpleLooped  )
private

◆ NumFrames()

int32 NumFrames ( ) const
inline

◆ OutputDim()

int32 OutputDim ( ) const
inline

Member Data Documentation

◆ computer_

NnetComputer computer_
private

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

Referenced by DecodableNnetSimpleLooped::AdvanceChunk().

◆ current_log_post_

◆ current_log_post_subsampled_offset_

int32 current_log_post_subsampled_offset_
private

◆ feats_

◆ info_

◆ ivector_

◆ num_chunks_computed_

int32 num_chunks_computed_
private

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

Referenced by DecodableNnetSimpleLooped::AdvanceChunk().

◆ num_subsampled_frames_

int32 num_subsampled_frames_
private

◆ online_ivector_feats_

const MatrixBase<BaseFloat>* online_ivector_feats_
private

◆ online_ivector_period_

int32 online_ivector_period_
private

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