hmm_model.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_HMM_HMM_MODEL_HPP
13 #define MLPACK_METHODS_HMM_HMM_MODEL_HPP
14 
15 #include "hmm.hpp"
18 
19 namespace mlpack {
20 namespace hmm {
21 
22 enum HMMType : char
23 {
28 };
29 
33 class HMMModel
34 {
35  private:
37  HMMType type;
43  HMM<gmm::GMM>* gmmHMM;
45  HMM<gmm::DiagonalGMM>* diagGMMHMM;
46 
47  public:
50  type(type),
51  discreteHMM(NULL),
52  gaussianHMM(NULL),
53  gmmHMM(NULL),
54  diagGMMHMM(NULL)
55  {
56  if (type == HMMType::DiscreteHMM)
57  discreteHMM = new HMM<distribution::DiscreteDistribution>();
58  else if (type == HMMType::GaussianHMM)
59  gaussianHMM = new HMM<distribution::GaussianDistribution>();
60  else if (type == HMMType::GaussianMixtureModelHMM)
61  gmmHMM = new HMM<gmm::GMM>();
63  diagGMMHMM = new HMM<gmm::DiagonalGMM>();
64  }
65 
67  HMMModel(const HMMModel& other) :
68  type(other.type),
69  discreteHMM(NULL),
70  gaussianHMM(NULL),
71  gmmHMM(NULL),
72  diagGMMHMM(NULL)
73  {
74  if (type == HMMType::DiscreteHMM)
75  discreteHMM =
76  new HMM<distribution::DiscreteDistribution>(*other.discreteHMM);
77  else if (type == HMMType::GaussianHMM)
78  gaussianHMM =
79  new HMM<distribution::GaussianDistribution>(*other.gaussianHMM);
80  else if (type == HMMType::GaussianMixtureModelHMM)
81  gmmHMM = new HMM<gmm::GMM>(*other.gmmHMM);
83  diagGMMHMM = new HMM<gmm::DiagonalGMM>(*other.diagGMMHMM);
84  }
85 
87  HMMModel(HMMModel&& other) :
88  type(other.type),
89  discreteHMM(other.discreteHMM),
90  gaussianHMM(other.gaussianHMM),
91  gmmHMM(other.gmmHMM),
92  diagGMMHMM(other.diagGMMHMM)
93  {
94  other.type = HMMType::DiscreteHMM;
95  other.discreteHMM = new HMM<distribution::DiscreteDistribution>();
96  other.gaussianHMM = NULL;
97  other.gmmHMM = NULL;
98  other.diagGMMHMM = NULL;
99  }
100 
102  HMMModel& operator=(const HMMModel& other)
103  {
104  if (this == &other)
105  return *this;
106 
107  delete discreteHMM;
108  delete gaussianHMM;
109  delete gmmHMM;
110  delete diagGMMHMM;
111 
112  discreteHMM = NULL;
113  gaussianHMM = NULL;
114  gmmHMM = NULL;
115  diagGMMHMM = NULL;
116 
117  type = other.type;
118  if (type == HMMType::DiscreteHMM)
119  discreteHMM =
120  new HMM<distribution::DiscreteDistribution>(*other.discreteHMM);
121  else if (type == HMMType::GaussianHMM)
122  gaussianHMM =
123  new HMM<distribution::GaussianDistribution>(*other.gaussianHMM);
124  else if (type == HMMType::GaussianMixtureModelHMM)
125  gmmHMM = new HMM<gmm::GMM>(*other.gmmHMM);
127  diagGMMHMM = new HMM<gmm::DiagonalGMM>(*other.diagGMMHMM);
128 
129  return *this;
130  }
131 
134  {
135  delete discreteHMM;
136  delete gaussianHMM;
137  delete gmmHMM;
138  delete diagGMMHMM;
139  }
140 
145  template<typename ActionType,
146  typename ExtraInfoType>
147  void PerformAction(ExtraInfoType* x)
148  {
149  if (type == HMMType::DiscreteHMM)
150  ActionType::Apply(*discreteHMM, x);
151  else if (type == HMMType::GaussianHMM)
152  ActionType::Apply(*gaussianHMM, x);
153  else if (type == HMMType::GaussianMixtureModelHMM)
154  ActionType::Apply(*gmmHMM, x);
156  ActionType::Apply(*diagGMMHMM, x);
157  }
158 
160  template<typename Archive>
161  void serialize(Archive& ar, const unsigned int version)
162  {
163  ar & BOOST_SERIALIZATION_NVP(type);
164 
165  // If necessary, clean memory.
166  if (Archive::is_loading::value)
167  {
168  delete discreteHMM;
169  delete gaussianHMM;
170  delete gmmHMM;
171  delete diagGMMHMM;
172 
173  discreteHMM = NULL;
174  gaussianHMM = NULL;
175  gmmHMM = NULL;
176  diagGMMHMM = NULL;
177  }
178 
179  if (type == HMMType::DiscreteHMM)
180  ar & BOOST_SERIALIZATION_NVP(discreteHMM);
181  else if (type == HMMType::GaussianHMM)
182  ar & BOOST_SERIALIZATION_NVP(gaussianHMM);
183  else if (type == HMMType::GaussianMixtureModelHMM)
184  ar & BOOST_SERIALIZATION_NVP(gmmHMM);
185 
186  // Backward compatibility: new versions of HMM has a Diagonal GMM type.
187  if (version > 0)
188  {
190  ar & BOOST_SERIALIZATION_NVP(diagGMMHMM);
191  }
192  }
193 
194  // Accessor method for type of HMM
195  HMMType Type() { return type; }
196 
218  HMM<gmm::GMM>* GMMHMM() { return gmmHMM; }
219  HMM<gmm::DiagonalGMM>* DiagGMMHMM() { return diagGMMHMM; }
220 };
221 
222 } // namespace hmm
223 } // namespace mlpack
224 
227 
228 #endif
HMM< distribution::DiscreteDistribution > * DiscreteHMM()
Accessor methods for discreteHMM, gaussianHMM, gmmHMM, and diagGMMHMM.
Definition: hmm_model.hpp:216
.hpp
Definition: add_to_po.hpp:21
void PerformAction(ExtraInfoType *x)
Given a functor type, perform that functor with the optional extra info on the HMM.
Definition: hmm_model.hpp:147
~HMMModel()
Clean memory.
Definition: hmm_model.hpp:133
HMM< gmm::GMM > * GMMHMM()
Definition: hmm_model.hpp:218
HMMModel(HMMModel &&other)
Take ownership of another model.
Definition: hmm_model.hpp:87
A class that represents a Hidden Markov Model with an arbitrary type of emission distribution.
Definition: hmm.hpp:85
BOOST_CLASS_VERSION(mlpack::hmm::HMMModel, 1)
Set the serialization version of the HMMModel class.
A serializable HMM model that also stores the type.
Definition: hmm_model.hpp:33
void serialize(Archive &ar, const unsigned int version)
Serialize the model.
Definition: hmm_model.hpp:161
HMMModel(const HMMType type=HMMType::DiscreteHMM)
Construct a model of the given type.
Definition: hmm_model.hpp:49
HMM< gmm::DiagonalGMM > * DiagGMMHMM()
Definition: hmm_model.hpp:219
HMMModel(const HMMModel &other)
Copy another model.
Definition: hmm_model.hpp:67
HMM< distribution::GaussianDistribution > * GaussianHMM()
Definition: hmm_model.hpp:217
HMMModel & operator=(const HMMModel &other)
Copy assignment operator.
Definition: hmm_model.hpp:102