[mlpack-git] master: Add (mostly empty) Serialize() methods to classes related to GMM. (8ee48b4)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Mon Jul 13 04:04:40 EDT 2015


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/8b2ca720828224607c70d2b539c43aecf8f4ec32...b4659b668021db631b3c8a48e3d735b513706fdc

>---------------------------------------------------------------

commit 8ee48b40ddeb5051813531e72a53d4ce0517a46a
Author: Ryan Curtin <ryan at ratml.org>
Date:   Sun Jul 12 13:30:35 2015 +0000

    Add (mostly empty) Serialize() methods to classes related to GMM.


>---------------------------------------------------------------

8ee48b40ddeb5051813531e72a53d4ce0517a46a
 src/mlpack/methods/gmm/diagonal_constraint.hpp         |  8 ++++++--
 src/mlpack/methods/gmm/eigenvalue_ratio_constraint.hpp | 14 +++++++++++---
 src/mlpack/methods/gmm/em_fit.hpp                      |  8 ++++++--
 src/mlpack/methods/gmm/em_fit_impl.hpp                 | 18 ++++++++++++++++--
 src/mlpack/methods/gmm/no_constraint.hpp               |  8 ++++++--
 .../methods/gmm/positive_definite_constraint.hpp       |  8 ++++++--
 6 files changed, 51 insertions(+), 13 deletions(-)

diff --git a/src/mlpack/methods/gmm/diagonal_constraint.hpp b/src/mlpack/methods/gmm/diagonal_constraint.hpp
index 911185a..e5620a5 100644
--- a/src/mlpack/methods/gmm/diagonal_constraint.hpp
+++ b/src/mlpack/methods/gmm/diagonal_constraint.hpp
@@ -25,9 +25,13 @@ class DiagonalConstraint
     arma::vec diagonal = covariance.diag();
     covariance = arma::diagmat(diagonal);
   }
+
+  //! Serialize the constraint (which holds nothing, so, nothing to do).
+  template<typename Archive>
+  static void Serialize(Archive& /* ar */, const unsigned int /* version */) { }
 };
 
-}; // namespace gmm
-}; // namespace mlpack
+} // namespace gmm
+} // namespace mlpack
 
 #endif
diff --git a/src/mlpack/methods/gmm/eigenvalue_ratio_constraint.hpp b/src/mlpack/methods/gmm/eigenvalue_ratio_constraint.hpp
index 6eefe34..5c5b2b0 100644
--- a/src/mlpack/methods/gmm/eigenvalue_ratio_constraint.hpp
+++ b/src/mlpack/methods/gmm/eigenvalue_ratio_constraint.hpp
@@ -16,7 +16,8 @@ namespace gmm {
  * Given a vector of eigenvalue ratios, ensure that the covariance matrix always
  * has those eigenvalue ratios.  When you create this object, make sure that the
  * vector of ratios that you pass does not go out of scope, because this object
- * holds a reference to that vector instead of copying it.
+ * holds a reference to that vector instead of copying it.  (This doesn't apply
+ * if you are deserializing the object from a file.)
  */
 class EigenvalueRatioConstraint
 {
@@ -28,7 +29,7 @@ class EigenvalueRatioConstraint
    * be 1.  In addition, all other elements should be less than or equal to 1.
    */
   EigenvalueRatioConstraint(const arma::vec& ratios) :
-      ratios(ratios)
+      ratios(ratios.memptr(), ratios.n_rows, ratios.n_cols, false) // Alias.
   {
     // Check validity of ratios.
     if (std::abs(ratios[0] - 1.0) > 1e-20)
@@ -69,9 +70,16 @@ class EigenvalueRatioConstraint
     covariance = eigenvectors * arma::diagmat(eigenvalues) * eigenvectors.t();
   }
 
+  //! Serialize the constraint.
+  template<typename Archive>
+  void Serialize(Archive& ar, const unsigned int /* version */)
+  {
+    ar & data::CreateNVP(ratios, "ratios");
+  }
+
  private:
   //! Ratios for eigenvalues.
-  const arma::vec& ratios;
+  const arma::vec ratios;
 };
 
 }; // namespace gmm
diff --git a/src/mlpack/methods/gmm/em_fit.hpp b/src/mlpack/methods/gmm/em_fit.hpp
index e480c0b..5396e03 100644
--- a/src/mlpack/methods/gmm/em_fit.hpp
+++ b/src/mlpack/methods/gmm/em_fit.hpp
@@ -122,6 +122,10 @@ class EMFit
   //! Modify the tolerance for the convergence of the EM algorithm.
   double& Tolerance() { return tolerance; }
 
+  //! Serialize the fitter.
+  template<typename Archive>
+  void Serialize(Archive& ar, const unsigned int version);
+
  private:
   /**
    * Run the clusterer, and then turn the cluster assignments into Gaussians.
@@ -162,8 +166,8 @@ class EMFit
   CovarianceConstraintPolicy constraint;
 };
 
-}; // namespace gmm
-}; // namespace mlpack
+} // namespace gmm
+} // namespace mlpack
 
 // Include implementation.
 #include "em_fit_impl.hpp"
diff --git a/src/mlpack/methods/gmm/em_fit_impl.hpp b/src/mlpack/methods/gmm/em_fit_impl.hpp
index dead473..032fa61 100644
--- a/src/mlpack/methods/gmm/em_fit_impl.hpp
+++ b/src/mlpack/methods/gmm/em_fit_impl.hpp
@@ -296,7 +296,21 @@ double EMFit<InitialClusteringType, CovarianceConstraintPolicy>::LogLikelihood(
   return logLikelihood;
 }
 
-}; // namespace gmm
-}; // namespace mlpack
+template<typename InitialClusteringType, typename CovarianceConstraintPolicy>
+template<typename Archive>
+void EMFit<InitialClusteringType, CovarianceConstraintPolicy>::Serialize(
+    Archive& ar,
+    const unsigned int /* version */)
+{
+  using data::CreateNVP;
+
+  ar & CreateNVP(maxIterations, "maxIterations");
+  ar & CreateNVP(tolerance, "tolerance");
+  ar & CreateNVP(clusterer, "clusterer");
+  ar & CreateNVP(constraint, "constraint");
+}
+
+} // namespace gmm
+} // namespace mlpack
 
 #endif
diff --git a/src/mlpack/methods/gmm/no_constraint.hpp b/src/mlpack/methods/gmm/no_constraint.hpp
index 027ead3..6708fbf 100644
--- a/src/mlpack/methods/gmm/no_constraint.hpp
+++ b/src/mlpack/methods/gmm/no_constraint.hpp
@@ -22,9 +22,13 @@ class NoConstraint
  public:
   //! Do nothing, and do not modify the covariance matrix.
   static void ApplyConstraint(const arma::mat& /* covariance */) { }
+
+  //! Serialize the object (nothing to do).
+  template<typename Archive>
+  static void Serialize(Archive& /* ar */, const unsigned int /* version */) { }
 };
 
-}; // namespace gmm
-}; // namespace mlpack
+} // namespace gmm
+} // namespace mlpack
 
 #endif
diff --git a/src/mlpack/methods/gmm/positive_definite_constraint.hpp b/src/mlpack/methods/gmm/positive_definite_constraint.hpp
index c87937c..45882bb 100644
--- a/src/mlpack/methods/gmm/positive_definite_constraint.hpp
+++ b/src/mlpack/methods/gmm/positive_definite_constraint.hpp
@@ -39,10 +39,14 @@ class PositiveDefiniteConstraint
       }
     }
   }
+
+  //! Serialize the constraint (which stores nothing, so, nothing to do).
+  template<typename Archive>
+  static void Serialize(Archive& /* ar */, const unsigned int /* version */) { }
 };
 
-}; // namespace gmm
-}; // namespace mlpack
+} // namespace gmm
+} // namespace mlpack
 
 #endif
 



More information about the mlpack-git mailing list