decodable-sum.h 3.7 KB

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