mlpack  master
hmm_model.hpp
Go to the documentation of this file.
1 
7 #ifndef MLPACK_METHODS_HMM_HMM_MODEL_HPP
8 #define MLPACK_METHODS_HMM_HMM_MODEL_HPP
9 
10 #include "hmm.hpp"
12 
13 namespace mlpack {
14 namespace hmm {
15 
16 enum HMMType : char
17 {
21 };
22 
26 class HMMModel
27 {
28  private:
37 
38  public:
41  type(HMMType::DiscreteHMM),
42  discreteHMM(new HMM<distribution::DiscreteDistribution>()),
43  gaussianHMM(NULL),
44  gmmHMM(NULL)
45  {
46  // Nothing to do.
47  }
48 
50  HMMModel(const HMMType type) :
51  type(type),
52  discreteHMM(NULL),
53  gaussianHMM(NULL),
54  gmmHMM(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  }
63 
65  HMMModel(const HMMModel& other) :
66  type(other.type),
67  discreteHMM(NULL),
68  gaussianHMM(NULL),
69  gmmHMM(NULL)
70  {
71  if (type == HMMType::DiscreteHMM)
72  discreteHMM =
74  else if (type == HMMType::GaussianHMM)
75  gaussianHMM =
77  else if (type == HMMType::GaussianMixtureModelHMM)
78  gmmHMM = new HMM<gmm::GMM>(*other.gmmHMM);
79  }
80 
82  HMMModel(HMMModel&& other) :
83  type(other.type),
84  discreteHMM(other.discreteHMM),
85  gaussianHMM(other.gaussianHMM),
86  gmmHMM(other.gmmHMM)
87  {
88  other.type = HMMType::DiscreteHMM;
89  other.discreteHMM = new HMM<distribution::DiscreteDistribution>();
90  other.gaussianHMM = NULL;
91  other.gmmHMM = NULL;
92  }
93 
95  HMMModel& operator=(const HMMModel& other)
96  {
97  delete discreteHMM;
98  delete gaussianHMM;
99  delete gmmHMM;
100 
101  discreteHMM = NULL;
102  gaussianHMM = NULL;
103  gmmHMM = NULL;
104 
105  type = other.type;
106  if (type == HMMType::DiscreteHMM)
107  discreteHMM =
109  else if (type == HMMType::GaussianHMM)
110  gaussianHMM =
112  else if (type == HMMType::GaussianMixtureModelHMM)
113  gmmHMM = new HMM<gmm::GMM>(*other.gmmHMM);
114 
115  return *this;
116  }
117 
120  {
121  delete discreteHMM;
122  delete gaussianHMM;
123  delete gmmHMM;
124  }
125 
130  template<typename ActionType,
131  typename ExtraInfoType>
132  void PerformAction(ExtraInfoType* x)
133  {
134  if (type == HMMType::DiscreteHMM)
135  ActionType::Apply(*discreteHMM, x);
136  else if (type == HMMType::GaussianHMM)
137  ActionType::Apply(*gaussianHMM, x);
138  else if (type == HMMType::GaussianMixtureModelHMM)
139  ActionType::Apply(*gmmHMM, x);
140  }
141 
143  template<typename Archive>
144  void Serialize(Archive& ar, const unsigned int /* version */)
145  {
146  ar & data::CreateNVP(type, "type");
147 
148  // If necessary, clean memory.
149  if (Archive::is_loading::value)
150  {
151  delete discreteHMM;
152  delete gaussianHMM;
153  delete gmmHMM;
154 
155  discreteHMM = NULL;
156  gaussianHMM = NULL;
157  gmmHMM = NULL;
158  }
159 
160  if (type == HMMType::DiscreteHMM)
161  ar & data::CreateNVP(discreteHMM, "discreteHMM");
162  else if (type == HMMType::GaussianHMM)
163  ar & data::CreateNVP(gaussianHMM, "gaussianHMM");
164  else if (type == HMMType::GaussianMixtureModelHMM)
165  ar & data::CreateNVP(gmmHMM, "gmmHMM");
166  }
167 };
168 
169 } // namespace hmm
170 } // namespace mlpack
171 
172 #endif
HMMModel(HMMModel &&other)
Take ownership of another model.
Definition: hmm_model.hpp:82
HMMType type
The type of the HMM.
Definition: hmm_model.hpp:30
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
FirstShim< T > CreateNVP(T &t, const std::string &name, typename std::enable_if_t< HasSerialize< T >::value > *=0)
Call this function to produce a name-value pair; this is similar to BOOST_SERIALIZATION_NVP(), but should be used for types that have a Serialize() function (or contain a type that has a Serialize() function) instead of a serialize() function.
HMMModel(const HMMModel &other)
Copy another model.
Definition: hmm_model.hpp:65
~HMMModel()
Clean memory.
Definition: hmm_model.hpp:119
HMM< distribution::GaussianDistribution > * gaussianHMM
Not used if type is not GaussianHMM.
Definition: hmm_model.hpp:34
HMM< distribution::DiscreteDistribution > * discreteHMM
Not used if type is not DiscreteHMM.
Definition: hmm_model.hpp:32
A serializable HMM model that also stores the type.
Definition: hmm_model.hpp:26
void Serialize(Archive &ar, const unsigned int)
Serialize the model.
Definition: hmm_model.hpp:144
HMM< gmm::GMM > * gmmHMM
Not used if type is not GaussianMixtureModelHMM.
Definition: hmm_model.hpp:36
HMMModel()
Construct an uninitialized model.
Definition: hmm_model.hpp:40
void PerformAction(ExtraInfoType *x)
Given a functor type, perform that functor with the optional extra info on the HMM.
Definition: hmm_model.hpp:132
HMMModel(const HMMType type)
Construct a model of the given type.
Definition: hmm_model.hpp:50
HMMModel & operator=(const HMMModel &other)
Copy assignment operator.
Definition: hmm_model.hpp:95