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"
19 
20 namespace mlpack {
21 namespace hmm {
22 
23 enum HMMType : char
24 {
29 };
30 
34 class HMMModel
35 {
36  private:
38  HMMType type;
44  HMM<gmm::GMM>* gmmHMM;
46  HMM<gmm::DiagonalGMM>* diagGMMHMM;
47 
48  public:
51  type(type),
52  discreteHMM(NULL),
53  gaussianHMM(NULL),
54  gmmHMM(NULL),
55  diagGMMHMM(NULL)
56  {
57  if (type == HMMType::DiscreteHMM)
58  discreteHMM = new HMM<distribution::DiscreteDistribution>();
59  else if (type == HMMType::GaussianHMM)
60  gaussianHMM = new HMM<distribution::GaussianDistribution>();
61  else if (type == HMMType::GaussianMixtureModelHMM)
62  gmmHMM = new HMM<gmm::GMM>();
64  diagGMMHMM = new HMM<gmm::DiagonalGMM>();
65  }
66 
68  HMMModel(const HMMModel& other) :
69  type(other.type),
70  discreteHMM(NULL),
71  gaussianHMM(NULL),
72  gmmHMM(NULL),
73  diagGMMHMM(NULL)
74  {
75  if (type == HMMType::DiscreteHMM)
76  discreteHMM =
77  new HMM<distribution::DiscreteDistribution>(*other.discreteHMM);
78  else if (type == HMMType::GaussianHMM)
79  gaussianHMM =
80  new HMM<distribution::GaussianDistribution>(*other.gaussianHMM);
81  else if (type == HMMType::GaussianMixtureModelHMM)
82  gmmHMM = new HMM<gmm::GMM>(*other.gmmHMM);
84  diagGMMHMM = new HMM<gmm::DiagonalGMM>(*other.diagGMMHMM);
85  }
86 
88  HMMModel(HMMModel&& other) :
89  type(other.type),
90  discreteHMM(other.discreteHMM),
91  gaussianHMM(other.gaussianHMM),
92  gmmHMM(other.gmmHMM),
93  diagGMMHMM(other.diagGMMHMM)
94  {
95  other.type = HMMType::DiscreteHMM;
96  other.discreteHMM = new HMM<distribution::DiscreteDistribution>();
97  other.gaussianHMM = NULL;
98  other.gmmHMM = NULL;
99  other.diagGMMHMM = NULL;
100  }
101 
103  HMMModel& operator=(const HMMModel& other)
104  {
105  if (this == &other)
106  return *this;
107 
108  delete discreteHMM;
109  delete gaussianHMM;
110  delete gmmHMM;
111  delete diagGMMHMM;
112 
113  discreteHMM = NULL;
114  gaussianHMM = NULL;
115  gmmHMM = NULL;
116  diagGMMHMM = NULL;
117 
118  type = other.type;
119  if (type == HMMType::DiscreteHMM)
120  discreteHMM =
121  new HMM<distribution::DiscreteDistribution>(*other.discreteHMM);
122  else if (type == HMMType::GaussianHMM)
123  gaussianHMM =
124  new HMM<distribution::GaussianDistribution>(*other.gaussianHMM);
125  else if (type == HMMType::GaussianMixtureModelHMM)
126  gmmHMM = new HMM<gmm::GMM>(*other.gmmHMM);
128  diagGMMHMM = new HMM<gmm::DiagonalGMM>(*other.diagGMMHMM);
129 
130  return *this;
131  }
132 
135  {
136  if (this != &other)
137  {
138  type = other.type;
139  discreteHMM = other.discreteHMM;
140  gaussianHMM = other.gaussianHMM;
141  gmmHMM = other.gmmHMM;
142  diagGMMHMM = other.diagGMMHMM;
143 
144  other.type = HMMType::DiscreteHMM;
145  other.discreteHMM = new HMM<distribution::DiscreteDistribution>();
146  other.gaussianHMM = nullptr;
147  other.gmmHMM = nullptr;
148  other.diagGMMHMM = nullptr;
149  }
150  return *this;
151  }
152 
155  {
156  delete discreteHMM;
157  delete gaussianHMM;
158  delete gmmHMM;
159  delete diagGMMHMM;
160  }
161 
166  template<typename ActionType,
167  typename ExtraInfoType>
168  void PerformAction(util::Params& params, ExtraInfoType* x)
169  {
170  if (type == HMMType::DiscreteHMM)
171  ActionType::Apply(params, *discreteHMM, x);
172  else if (type == HMMType::GaussianHMM)
173  ActionType::Apply(params, *gaussianHMM, x);
174  else if (type == HMMType::GaussianMixtureModelHMM)
175  ActionType::Apply(params, *gmmHMM, x);
177  ActionType::Apply(params, *diagGMMHMM, x);
178  }
179 
181  template<typename Archive>
182  void serialize(Archive& ar, const uint32_t /* version */)
183  {
184  ar(CEREAL_NVP(type));
185 
186  // If necessary, clean memory.
187  if (cereal::is_loading<Archive>())
188  {
189  delete discreteHMM;
190  delete gaussianHMM;
191  delete gmmHMM;
192  delete diagGMMHMM;
193 
194  discreteHMM = NULL;
195  gaussianHMM = NULL;
196  gmmHMM = NULL;
197  diagGMMHMM = NULL;
198  }
199 
200  if (type == HMMType::DiscreteHMM)
201  ar(CEREAL_POINTER(discreteHMM));
202  else if (type == HMMType::GaussianHMM)
203  ar(CEREAL_POINTER(gaussianHMM));
204  else if (type == HMMType::GaussianMixtureModelHMM)
205  ar(CEREAL_POINTER(gmmHMM));
207  ar(CEREAL_POINTER(diagGMMHMM));
208  }
209 
210  // Accessor method for type of HMM
211  HMMType Type() { return type; }
212 
234  HMM<gmm::GMM>* GMMHMM() { return gmmHMM; }
235  HMM<gmm::DiagonalGMM>* DiagGMMHMM() { return diagGMMHMM; }
236 };
237 
238 } // namespace hmm
239 } // namespace mlpack
240 
241 #endif
HMMModel & operator=(HMMModel &&other)
Move assignment operator.
Definition: hmm_model.hpp:134
HMM< distribution::DiscreteDistribution > * DiscreteHMM()
Accessor methods for discreteHMM, gaussianHMM, gmmHMM, and diagGMMHMM.
Definition: hmm_model.hpp:232
void PerformAction(util::Params &params, ExtraInfoType *x)
Given a functor type, perform that functor with the optional extra info on the HMM.
Definition: hmm_model.hpp:168
Linear algebra utility functions, generally performed on matrices or vectors.
void serialize(Archive &ar, const uint32_t)
Serialize the model.
Definition: hmm_model.hpp:182
~HMMModel()
Clean memory.
Definition: hmm_model.hpp:154
HMM< gmm::GMM > * GMMHMM()
Definition: hmm_model.hpp:234
HMMModel(HMMModel &&other)
Take ownership of another model.
Definition: hmm_model.hpp:88
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:34
#define CEREAL_POINTER(T)
Cereal does not support the serialization of raw pointer.
HMMModel(const HMMType type=HMMType::DiscreteHMM)
Construct a model of the given type.
Definition: hmm_model.hpp:50
The Params class holds all information about the parameters passed to a specific binding.
Definition: params.hpp:20
HMM< gmm::DiagonalGMM > * DiagGMMHMM()
Definition: hmm_model.hpp:235
HMMModel(const HMMModel &other)
Copy another model.
Definition: hmm_model.hpp:68
HMM< distribution::GaussianDistribution > * GaussianHMM()
Definition: hmm_model.hpp:233
HMMModel & operator=(const HMMModel &other)
Copy assignment operator.
Definition: hmm_model.hpp:103