decodable-matrix.cc
Go to the documentation of this file.
1 // decoder/decodable-matrix.cc
2 
3 // Copyright 2018 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
21 
22 namespace kaldi {
23 
25  const TransitionModel &tm,
26  const MatrixBase<BaseFloat> &likes,
27  int32 frame_offset):
28  trans_model_(tm), likes_(&likes), likes_to_delete_(NULL),
29  frame_offset_(frame_offset) {
30  stride_ = likes.Stride();
31  raw_data_ = likes.Data() - (stride_ * frame_offset);
32 
33  if (likes.NumCols() != tm.NumPdfs())
34  KALDI_ERR << "Mismatch, matrix has "
35  << likes.NumCols() << " cols but transition-model has "
36  << tm.NumPdfs() << " pdf-ids.";
37 }
38 
40  const TransitionModel &tm, const Matrix<BaseFloat> *likes,
41  int32 frame_offset):
42  trans_model_(tm), likes_(likes), likes_to_delete_(likes),
43  frame_offset_(frame_offset) {
44  stride_ = likes->Stride();
45  raw_data_ = likes->Data() - (stride_ * frame_offset_);
46  if (likes->NumCols() != tm.NumPdfs())
47  KALDI_ERR << "Mismatch, matrix has "
48  << likes->NumCols() << " cols but transition-model has "
49  << tm.NumPdfs() << " pdf-ids.";
50 }
51 
52 
55 #ifdef KALDI_PARANOID
56  return (*likes_)(frame - frame_offset_, pdf_id);
57 #else
58  return raw_data_[frame * stride_ + pdf_id];
59 #endif
60 }
61 
63  return frame_offset_ + likes_->NumRows();
64 }
65 
67  KALDI_ASSERT(frame < NumFramesReady());
68  return (frame == NumFramesReady() - 1);
69 }
70 
71 // Indices are one-based! This is for compatibility with OpenFst.
74 }
75 
77  delete likes_to_delete_;
78 }
79 
80 
82  Matrix<BaseFloat> *loglikes, int32 frames_to_discard) {
83  if (loglikes->NumRows() == 0) return;
84  KALDI_ASSERT(loglikes->NumCols() == trans_model_.NumPdfs());
85  KALDI_ASSERT(frames_to_discard <= loglikes_.NumRows() &&
86  frames_to_discard >= 0);
87  if (frames_to_discard == loglikes_.NumRows()) {
88  loglikes_.Swap(loglikes);
89  loglikes->Resize(0, 0);
90  } else {
91  int32 old_rows_kept = loglikes_.NumRows() - frames_to_discard,
92  new_num_rows = old_rows_kept + loglikes->NumRows();
93  Matrix<BaseFloat> new_loglikes(new_num_rows, loglikes->NumCols());
94  new_loglikes.RowRange(0, old_rows_kept).CopyFromMat(
95  loglikes_.RowRange(frames_to_discard, old_rows_kept));
96  new_loglikes.RowRange(old_rows_kept, loglikes->NumRows()).CopyFromMat(
97  *loglikes);
98  loglikes_.Swap(&new_loglikes);
99  }
100  frame_offset_ += frames_to_discard;
101  stride_ = loglikes_.Stride();
102  raw_data_ = loglikes_.Data() - (frame_offset_ * stride_);
103 }
104 
105 
106 
107 } // end namespace kaldi.
const TransitionModel & trans_model_
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
virtual int32 NumIndices() const
Returns the number of states in the acoustic model (they will be indexed one-based, i.e.
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
int32 TransitionIdToPdfFast(int32 trans_id) const
Base class which provides matrix operations not involving resizing or allocation. ...
Definition: kaldi-matrix.h:49
const Real * Data() const
Gives pointer to raw data (const).
Definition: kaldi-matrix.h:79
virtual int32 NumFramesReady() const
The call NumFramesReady() will return the number of frames currently available for this decodable obj...
virtual bool IsLastFrame(int32 frame) const
Returns true if this is the last frame.
kaldi::int32 int32
const Matrix< BaseFloat > * likes_to_delete_
MatrixIndexT Stride() const
Stride (distance in memory between each row). Will be >= NumCols.
Definition: kaldi-matrix.h:70
DecodableMatrixMapped(const TransitionModel &tm, const MatrixBase< BaseFloat > &likes, int32 frame_offset=0)
int32 NumTransitionIds() const
Returns the total number of transition-ids (note, these are one-based).
#define KALDI_ERR
Definition: kaldi-error.h:147
SubMatrix< Real > RowRange(const MatrixIndexT row_offset, const MatrixIndexT num_rows) const
Definition: kaldi-matrix.h:209
#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
const MatrixBase< BaseFloat > * likes_
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 BaseFloat LogLikelihood(int32 frame, int32 tid)
Returns the log likelihood, which will be negated in the decoder.
void AcceptLoglikes(Matrix< BaseFloat > *loglikes, int32 frames_to_discard)