mlpack
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 {
24  DiscreteHMM = 0,
25  GaussianHMM,
26  GaussianMixtureModelHMM,
27  DiagonalGaussianMixtureModelHMM
28 };
29 
33 class HMMModel
34 {
35  private:
37  HMMType type;
43  HMM<gmm::GMM>* gmmHMM;
45  HMM<gmm::DiagonalGMM>* diagGMMHMM;
46 
47  public:
49  HMMModel(const HMMType type = HMMType::DiscreteHMM) :
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>();
62  else if (type == HMMType::DiagonalGaussianMixtureModelHMM)
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);
82  else if (type == HMMType::DiagonalGaussianMixtureModelHMM)
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);
126  else if (type == HMMType::DiagonalGaussianMixtureModelHMM)
127  diagGMMHMM = new HMM<gmm::DiagonalGMM>(*other.diagGMMHMM);
128 
129  return *this;
130  }
131 
134  {
135  if (this != &other)
136  {
137  type = other.type;
138  discreteHMM = other.discreteHMM;
139  gaussianHMM = other.gaussianHMM;
140  gmmHMM = other.gmmHMM;
141  diagGMMHMM = other.diagGMMHMM;
142 
143  other.type = HMMType::DiscreteHMM;
144  other.discreteHMM = new HMM<distribution::DiscreteDistribution>();
145  other.gaussianHMM = nullptr;
146  other.gmmHMM = nullptr;
147  other.diagGMMHMM = nullptr;
148  }
149  return *this;
150  }
151 
154  {
155  delete discreteHMM;
156  delete gaussianHMM;
157  delete gmmHMM;
158  delete diagGMMHMM;
159  }
160 
165  template<typename ActionType,
166  typename ExtraInfoType>
167  void PerformAction(ExtraInfoType* x)
168  {
169  if (type == HMMType::DiscreteHMM)
170  ActionType::Apply(*discreteHMM, x);
171  else if (type == HMMType::GaussianHMM)
172  ActionType::Apply(*gaussianHMM, x);
173  else if (type == HMMType::GaussianMixtureModelHMM)
174  ActionType::Apply(*gmmHMM, x);
175  else if (type == HMMType::DiagonalGaussianMixtureModelHMM)
176  ActionType::Apply(*diagGMMHMM, x);
177  }
178 
180  template<typename Archive>
181  void serialize(Archive& ar, const uint32_t /* version */)
182  {
183  ar(CEREAL_NVP(type));
184 
185  // If necessary, clean memory.
186  if (cereal::is_loading<Archive>())
187  {
188  delete discreteHMM;
189  delete gaussianHMM;
190  delete gmmHMM;
191  delete diagGMMHMM;
192 
193  discreteHMM = NULL;
194  gaussianHMM = NULL;
195  gmmHMM = NULL;
196  diagGMMHMM = NULL;
197  }
198 
199  if (type == HMMType::DiscreteHMM)
200  ar(CEREAL_POINTER(discreteHMM));
201  else if (type == HMMType::GaussianHMM)
202  ar(CEREAL_POINTER(gaussianHMM));
203  else if (type == HMMType::GaussianMixtureModelHMM)
204  ar(CEREAL_POINTER(gmmHMM));
205  else if (type == HMMType::DiagonalGaussianMixtureModelHMM)
206  ar(CEREAL_POINTER(diagGMMHMM));
207  }
208 
209  // Accessor method for type of HMM
210  HMMType Type() { return type; }
211 
232  HMM<distribution::GaussianDistribution>* GaussianHMM() { return gaussianHMM; }
233  HMM<gmm::GMM>* GMMHMM() { return gmmHMM; }
234  HMM<gmm::DiagonalGMM>* DiagGMMHMM() { return diagGMMHMM; }
235 };
236 
237 } // namespace hmm
238 } // namespace mlpack
239 
240 #endif
void serialize(Archive &ar, const uint32_t)
Serialize the model.
Definition: hmm_model.hpp:181
HMMModel(HMMModel &&other)
Take ownership of another model.
Definition: hmm_model.hpp:87
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
HMMModel & operator=(HMMModel &&other)
Move assignment operator.
Definition: hmm_model.hpp:133
HMMModel(const HMMModel &other)
Copy another model.
Definition: hmm_model.hpp:67
HMMModel(const HMMType type=HMMType::DiscreteHMM)
Construct a model of the given type.
Definition: hmm_model.hpp:49
HMM< distribution::DiscreteDistribution > * DiscreteHMM()
Accessor methods for discreteHMM, gaussianHMM, gmmHMM, and diagGMMHMM.
Definition: hmm_model.hpp:231
~HMMModel()
Clean memory.
Definition: hmm_model.hpp:153
A class that represents a Hidden Markov Model with an arbitrary type of emission distribution.
Definition: hmm.hpp:85
A serializable HMM model that also stores the type.
Definition: hmm_model.hpp:33
#define CEREAL_POINTER(T)
Cereal does not support the serialization of raw pointer.
Definition: pointer_wrapper.hpp:96
void PerformAction(ExtraInfoType *x)
Given a functor type, perform that functor with the optional extra info on the HMM.
Definition: hmm_model.hpp:167
HMMModel & operator=(const HMMModel &other)
Copy assignment operator.
Definition: hmm_model.hpp:102