mlpack 3.4.2
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
19namespace mlpack {
20namespace hmm {
21
22enum HMMType : char
23{
28};
29
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)
58 else if (type == HMMType::GaussianHMM)
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
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
A serializable HMM model that also stores the type.
Definition: hmm_model.hpp:34
HMM< gmm::GMM > * GMMHMM()
Definition: hmm_model.hpp:218
HMMModel(const HMMType type=HMMType::DiscreteHMM)
Construct a model of the given type.
Definition: hmm_model.hpp:49
~HMMModel()
Clean memory.
Definition: hmm_model.hpp:133
HMM< distribution::GaussianDistribution > * GaussianHMM()
Definition: hmm_model.hpp:217
HMMModel(HMMModel &&other)
Take ownership of another model.
Definition: hmm_model.hpp:87
void serialize(Archive &ar, const unsigned int version)
Serialize the model.
Definition: hmm_model.hpp:161
void PerformAction(ExtraInfoType *x)
Given a functor type, perform that functor with the optional extra info on the HMM.
Definition: hmm_model.hpp:147
HMM< distribution::DiscreteDistribution > * DiscreteHMM()
Accessor methods for discreteHMM, gaussianHMM, gmmHMM, and diagGMMHMM.
Definition: hmm_model.hpp:216
HMMModel(const HMMModel &other)
Copy another model.
Definition: hmm_model.hpp:67
HMM< gmm::DiagonalGMM > * DiagGMMHMM()
Definition: hmm_model.hpp:219
HMMModel & operator=(const HMMModel &other)
Copy assignment operator.
Definition: hmm_model.hpp:102
A class that represents a Hidden Markov Model with an arbitrary type of emission distribution.
Definition: hmm.hpp:86
BOOST_CLASS_VERSION(mlpack::hmm::HMMModel, 1)
Set the serialization version of the HMMModel class.
@ DiagonalGaussianMixtureModelHMM
Definition: hmm_model.hpp:27
@ GaussianMixtureModelHMM
Definition: hmm_model.hpp:26
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1