decodable-sum.h
Go to the documentation of this file.
1 // decoder/decodable-sum.h
2 
3 // Copyright 2009-2011 Saarland University; Microsoft Corporation;
4 // Lukas Burget, Pawel Swietojanski
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #ifndef KALDI_DECODER_DECODABLE_SUM_H_
22 #define KALDI_DECODER_DECODABLE_SUM_H_
23 
24 #include <vector>
25 #include <utility>
26 
27 #include "base/kaldi-common.h"
28 #include "itf/decodable-itf.h"
29 
30 namespace kaldi {
31 
32 // The DecodableSum object is a very simple object that just sums
33 // scores over a number of Decodable objects. They must all have
34 // the same dimensions.
35 
37  public:
38  // Does not take ownership of pointers! They are just
39  // pointers because they are non-const.
41  DecodableInterface *d2, BaseFloat w2) {
42  decodables_.push_back(std::make_pair(d1, w1));
43  decodables_.push_back(std::make_pair(d2, w2));
44  CheckSizes();
45  }
46 
47  // Does not take ownership of pointers!
49  const std::vector<std::pair<DecodableInterface*, BaseFloat> > &decodables) :
50  decodables_(decodables) { CheckSizes(); }
51 
52  void CheckSizes() const {
53  KALDI_ASSERT(decodables_.size() >= 1
54  && decodables_[0].first != NULL);
55  for (size_t i = 1; i < decodables_.size(); i++)
56  KALDI_ASSERT(decodables_[i].first != NULL &&
57  decodables_[i].first->NumIndices() ==
58  decodables_[0].first->NumIndices());
59  }
60 
61  // Note, frames are numbered from zero. But state_index is numbered
62  // from one (this routine is called by FSTs).
63  virtual BaseFloat LogLikelihood(int32 frame, int32 state_index) {
64  BaseFloat sum = 0.0;
65  // int32 i=1;
66  for (std::vector<std::pair<DecodableInterface*, BaseFloat> >::iterator iter = decodables_.begin();
67  iter != decodables_.end();
68  ++iter) {
69  sum += iter->first->LogLikelihood(frame, state_index) * iter->second;
70  }
71  return sum;
72  }
73 
74  virtual int32 NumIndices() const { return decodables_[0].first->NumIndices(); }
75 
76  virtual bool IsLastFrame(int32 frame) const {
77  // We require all the decodables have the same #frames. We don't check this though.
78  return decodables_[0].first->IsLastFrame(frame);
79  }
80 
81  private:
82  std::vector<std::pair<DecodableInterface*, BaseFloat> > decodables_;
84 };
85 
87  public:
90  BaseFloat scale)
91  : DecodableSum(d1, w1, d2, w2), scale_(scale) {}
92 
93  DecodableSumScaled(const std::vector<std::pair<DecodableInterface*, BaseFloat> > &decodables,
94  BaseFloat scale)
95  : DecodableSum(decodables), scale_(scale) {}
96 
97  virtual BaseFloat LogLikelihood(int32 frame, int32 state_index) {
98  return scale_ * DecodableSum::LogLikelihood(frame, state_index);
99  }
100 
101  private:
104 };
105 
106 } // namespace kaldi
107 
108 #endif // KALDI_DECODER_DECODABLE_SUM_H_
109 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
DecodableInterface provides a link between the (acoustic-modeling and feature-processing) code and th...
Definition: decodable-itf.h:82
KALDI_DISALLOW_COPY_AND_ASSIGN(DecodableSum)
DecodableSum(DecodableInterface *d1, BaseFloat w1, DecodableInterface *d2, BaseFloat w2)
Definition: decodable-sum.h:40
kaldi::int32 int32
virtual BaseFloat LogLikelihood(int32 frame, int32 state_index)
Returns the log likelihood, which will be negated in the decoder.
Definition: decodable-sum.h:63
virtual BaseFloat LogLikelihood(int32 frame, int32 state_index)
Returns the log likelihood, which will be negated in the decoder.
Definition: decodable-sum.h:97
void CheckSizes() const
Definition: decodable-sum.h:52
virtual int32 NumIndices() const
Returns the number of states in the acoustic model (they will be indexed one-based, i.e.
Definition: decodable-sum.h:74
DecodableSumScaled(DecodableInterface *d1, BaseFloat w1, DecodableInterface *d2, BaseFloat w2, BaseFloat scale)
Definition: decodable-sum.h:88
virtual bool IsLastFrame(int32 frame) const
Returns true if this is the last frame.
Definition: decodable-sum.h:76
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::vector< std::pair< DecodableInterface *, BaseFloat > > decodables_
Definition: decodable-sum.h:82
DecodableSumScaled(const std::vector< std::pair< DecodableInterface *, BaseFloat > > &decodables, BaseFloat scale)
Definition: decodable-sum.h:93
DecodableSum(const std::vector< std::pair< DecodableInterface *, BaseFloat > > &decodables)
Definition: decodable-sum.h:48