OnlineLdaInput Class Reference

#include <online-feat-input.h>

Inheritance diagram for OnlineLdaInput:
Collaboration diagram for OnlineLdaInput:

Public Member Functions

 OnlineLdaInput (OnlineFeatInputItf *input, const Matrix< BaseFloat > &transform, int32 left_context, int32 right_context)
 
virtual bool Compute (Matrix< BaseFloat > *output)
 
virtual int32 Dim () const
 
- Public Member Functions inherited from OnlineFeatInputItf
virtual ~OnlineFeatInputItf ()
 

Private Member Functions

void TransformToOutput (const MatrixBase< BaseFloat > &spliced_feats, Matrix< BaseFloat > *output)
 
void ComputeNextRemainder (const MatrixBase< BaseFloat > &input)
 
 KALDI_DISALLOW_COPY_AND_ASSIGN (OnlineLdaInput)
 

Static Private Member Functions

static void SpliceFrames (const MatrixBase< BaseFloat > &input1, const MatrixBase< BaseFloat > &input2, const MatrixBase< BaseFloat > &input3, int32 context_window, Matrix< BaseFloat > *output)
 

Private Attributes

OnlineFeatInputItfinput_
 
const int32 input_dim_
 
const int32 left_context_
 
const int32 right_context_
 
Matrix< BaseFloatlinear_transform_
 
Vector< BaseFloatoffset_
 
Matrix< BaseFloatremainder_
 

Detailed Description

Definition at line 186 of file online-feat-input.h.

Constructor & Destructor Documentation

◆ OnlineLdaInput()

OnlineLdaInput ( OnlineFeatInputItf input,
const Matrix< BaseFloat > &  transform,
int32  left_context,
int32  right_context 
)

Definition at line 191 of file online-feat-input.cc.

References MatrixBase< Real >::CopyFromMat(), OnlineLdaInput::input_dim_, KALDI_ERR, OnlineLdaInput::linear_transform_, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), OnlineLdaInput::offset_, MatrixBase< Real >::Range(), and Matrix< Real >::Resize().

194  :
195  input_(input), input_dim_(input->Dim()),
196  left_context_(left_context), right_context_(right_context) {
197 
198  int32 tot_context = left_context + 1 + right_context;
199  if (transform.NumCols() == input_dim_ * tot_context) {
200  linear_transform_ = transform;
201  // and offset_ stays empty.
202  } else if (transform.NumCols() == input_dim_ * tot_context + 1) {
203  linear_transform_.Resize(transform.NumRows(), transform.NumCols() - 1);
204  linear_transform_.CopyFromMat(transform.Range(0, transform.NumRows(),
205  0, transform.NumCols() - 1));
206  offset_.Resize(transform.NumRows());
207  offset_.CopyColFromMat(transform, transform.NumCols() - 1);
208  } else {
209  KALDI_ERR << "Invalid parameters supplied to OnlineLdaInput";
210  }
211 }
OnlineFeatInputItf * input_
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
kaldi::int32 int32
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
#define KALDI_ERR
Definition: kaldi-error.h:147
Vector< BaseFloat > offset_
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
SubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Return a sub-part of matrix.
Definition: kaldi-matrix.h:202
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).
Matrix< BaseFloat > linear_transform_

Member Function Documentation

◆ Compute()

bool Compute ( Matrix< BaseFloat > *  output)
virtual

Implements OnlineFeatInputItf.

Definition at line 259 of file online-feat-input.cc.

References OnlineFeatInputItf::Compute(), OnlineLdaInput::ComputeNextRemainder(), rnnlm::i, OnlineLdaInput::input_, OnlineLdaInput::input_dim_, KALDI_ASSERT, OnlineLdaInput::left_context_, OnlineLdaInput::linear_transform_, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), OnlineLdaInput::remainder_, Matrix< Real >::Resize(), OnlineLdaInput::right_context_, MatrixBase< Real >::Row(), OnlineLdaInput::SpliceFrames(), and OnlineLdaInput::TransformToOutput().

259  {
260  KALDI_ASSERT(output->NumRows() > 0 &&
261  output->NumCols() == linear_transform_.NumRows());
262  // If output->NumRows() == 0, it corresponds to a request for zero frames,
263  // which makes no sense.
264 
265  // We request the same number of frames of data that we were requested.
266  Matrix<BaseFloat> input(output->NumRows(), input_dim_);
267  bool ans = input_->Compute(&input);
268  // If we got no input (timed out) and we're not at the end, we return
269  // empty output.
270 
271  if (input.NumRows() == 0 && ans) {
272  output->Resize(0, 0);
273  return ans;
274  } else if (input.NumRows() == 0 && !ans) {
275  // The end of the input stream, but no input this time.
276  if (remainder_.NumRows() == 0) {
277  output->Resize(0, 0);
278  return ans;
279  }
280  }
281 
282  // If this is the first segment of the utterance, we put in the
283  // initial duplicates of the first frame, numbered "left_context".
284  if (remainder_.NumRows() == 0 && input.NumRows() != 0 && left_context_ != 0) {
286  for (int32 i = 0; i < left_context_; i++)
287  remainder_.Row(i).CopyFromVec(input.Row(0));
288  }
289 
290  // If this is the last segment, we put in the final duplicates of the
291  // last frame, numbered "right_context".
292  Matrix<BaseFloat> tail;
293  if (!ans && right_context_ > 0) {
294  tail.Resize(right_context_, input_dim_);
295  for (int32 i = 0; i < right_context_; i++) {
296  if (input.NumRows() > 0)
297  tail.Row(i).CopyFromVec(input.Row(input.NumRows() - 1));
298  else
299  tail.Row(i).CopyFromVec(remainder_.Row(remainder_.NumRows() - 1));
300  }
301  }
302 
303  Matrix<BaseFloat> spliced_feats;
304  int32 context_window = left_context_ + 1 + right_context_;
305  // The next line is a call to a member function.
306  SpliceFrames(remainder_, input, tail, context_window, &spliced_feats);
307  TransformToOutput(spliced_feats, output);
308  ComputeNextRemainder(input);
309  return ans;
310 }
OnlineFeatInputItf * input_
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
kaldi::int32 int32
static void SpliceFrames(const MatrixBase< BaseFloat > &input1, const MatrixBase< BaseFloat > &input2, const MatrixBase< BaseFloat > &input3, int32 context_window, Matrix< BaseFloat > *output)
Matrix< BaseFloat > remainder_
void ComputeNextRemainder(const MatrixBase< BaseFloat > &input)
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
void TransformToOutput(const MatrixBase< BaseFloat > &spliced_feats, Matrix< BaseFloat > *output)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
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).
virtual bool Compute(Matrix< BaseFloat > *output)=0
Matrix< BaseFloat > linear_transform_

◆ ComputeNextRemainder()

void ComputeNextRemainder ( const MatrixBase< BaseFloat > &  input)
private

Definition at line 312 of file online-feat-input.cc.

References VectorBase< Real >::CopyFromVec(), rnnlm::i, OnlineLdaInput::input_dim_, OnlineLdaInput::left_context_, MatrixBase< Real >::NumRows(), OnlineLdaInput::remainder_, Matrix< Real >::Resize(), OnlineLdaInput::right_context_, and MatrixBase< Real >::Row().

Referenced by OnlineLdaInput::Compute().

312  {
313  // The size of the remainder that we propagate to the next frame is
314  // context_window - 1, if available.
315  int32 context_window = left_context_ + 1 + right_context_;
316  int32 next_remainder_len = std::min(context_window - 1,
317  remainder_.NumRows() + input.NumRows());
318  if (next_remainder_len == 0) {
319  remainder_.Resize(0, 0);
320  return;
321  }
322  Matrix<BaseFloat> next_remainder(next_remainder_len, input_dim_);
323  int32 rsize = remainder_.NumRows(), isize = input.NumRows();
324  for (int32 i = 0; i < next_remainder_len; i++) {
325  SubVector<BaseFloat> dest(next_remainder, i);
326  int32 t = (rsize + isize) - next_remainder_len + i;
327  // Here, t is an offset into a numbering of the frames where we first have
328  // the old "remainder" frames, then the regular frames.
329  if (t < rsize) dest.CopyFromVec(remainder_.Row(t));
330  else dest.CopyFromVec(input.Row(t - rsize));
331  }
332  remainder_ = next_remainder;
333 }
kaldi::int32 int32
Matrix< BaseFloat > remainder_
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
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).

◆ Dim()

virtual int32 Dim ( ) const
inlinevirtual

Implements OnlineFeatInputItf.

Definition at line 195 of file online-feat-input.h.

References kaldi::SpliceFrames().

195 { return linear_transform_.NumRows(); }
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
Matrix< BaseFloat > linear_transform_

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( OnlineLdaInput  )
private

◆ SpliceFrames()

void SpliceFrames ( const MatrixBase< BaseFloat > &  input1,
const MatrixBase< BaseFloat > &  input2,
const MatrixBase< BaseFloat > &  input3,
int32  context_window,
Matrix< BaseFloat > *  output 
)
staticprivate

Definition at line 214 of file online-feat-input.cc.

References VectorBase< Real >::CopyFromVec(), KALDI_ASSERT, MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), Matrix< Real >::Resize(), and MatrixBase< Real >::Row().

Referenced by OnlineLdaInput::Compute().

218  {
219  KALDI_ASSERT(context_window > 0);
220  const int32 size1 = input1.NumRows(), size2 = input2.NumRows(),
221  size3 = input3.NumRows();
222  int32 num_frames_in = size1 + size2 + size3,
223  num_frames_out = num_frames_in - (context_window - 1),
224  dim = std::max(input1.NumCols(), std::max(input2.NumCols(), input3.NumCols()));
225  // do std::max in case one or more of the input matrices is empty.
226 
227  if (num_frames_out <= 0) {
228  output->Resize(0, 0);
229  return;
230  }
231  output->Resize(num_frames_out, dim * context_window);
232  for (int32 t_out = 0; t_out < num_frames_out; t_out++) {
233  for (int32 pos = 0; pos < context_window; pos++) {
234  int32 t_in = t_out + pos;
235  SubVector<BaseFloat> vec_out(output->Row(t_out), pos * dim, dim);
236  if (t_in < size1)
237  vec_out.CopyFromVec(input1.Row(t_in));
238  else if (t_in < size1 + size2)
239  vec_out.CopyFromVec(input2.Row(t_in - size1));
240  else
241  vec_out.CopyFromVec(input3.Row(t_in - size1 - size2));
242  }
243  }
244 }
kaldi::int32 int32
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
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).

◆ TransformToOutput()

void TransformToOutput ( const MatrixBase< BaseFloat > &  spliced_feats,
Matrix< BaseFloat > *  output 
)
private

Definition at line 246 of file online-feat-input.cc.

References MatrixBase< Real >::AddMatMat(), MatrixBase< Real >::AddVecToRows(), kaldi::kNoTrans, kaldi::kTrans, OnlineLdaInput::linear_transform_, MatrixBase< Real >::NumRows(), OnlineLdaInput::offset_, and Matrix< Real >::Resize().

Referenced by OnlineLdaInput::Compute().

247  {
248  if (spliced_feats.NumRows() == 0) {
249  output->Resize(0, 0);
250  } else {
251  output->Resize(spliced_feats.NumRows(), linear_transform_.NumRows());
252  output->AddMatMat(1.0, spliced_feats, kNoTrans,
253  linear_transform_, kTrans, 0.0);
254  if (offset_.Dim() != 0)
255  output->AddVecToRows(1.0, offset_);
256  }
257 }
void AddVecToRows(const Real alpha, const VectorBase< OtherReal > &v)
[each row of *this] += alpha * v
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
Vector< BaseFloat > offset_
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
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).
Matrix< BaseFloat > linear_transform_

Member Data Documentation

◆ input_

OnlineFeatInputItf* input_
private

Definition at line 215 of file online-feat-input.h.

Referenced by OnlineCacheInput::Compute(), and OnlineLdaInput::Compute().

◆ input_dim_

◆ left_context_

const int32 left_context_
private

◆ linear_transform_

◆ offset_

Vector<BaseFloat> offset_
private

◆ remainder_

Matrix<BaseFloat> remainder_
private

◆ right_context_

const int32 right_context_
private

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