[opengm] 124/386: Add visitor that defines the learn methods for each Learner and all Inference methods.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Aug 31 08:35:46 UTC 2016


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to branch debian/master
in repository opengm.

commit af44b6c3d4b7fb440c68c44b7d1523f471cf4f56
Author: Carsten Haubold <carstenhaubold at googlemail.com>
Date:   Thu Dec 18 15:37:54 2014 +0100

    Add visitor that defines the learn methods for each Learner and all Inference methods.
---
 src/interfaces/python/opengm/learning/helper.hxx   | 53 ++++++++++++++++++++++
 .../python/opengm/learning/pyGridSearchLearner.cxx | 25 +---------
 .../opengm/learning/pyStructMaxMarginLearner.cxx   | 24 ++--------
 3 files changed, 58 insertions(+), 44 deletions(-)

diff --git a/src/interfaces/python/opengm/learning/helper.hxx b/src/interfaces/python/opengm/learning/helper.hxx
new file mode 100644
index 0000000..87bc676
--- /dev/null
+++ b/src/interfaces/python/opengm/learning/helper.hxx
@@ -0,0 +1,53 @@
+#ifndef HELPER_HXX
+#define HELPER_HXX
+
+#include <boost/python.hpp>
+#include <boost/python/module.hpp>
+#include <opengm/python/opengmpython.hxx>
+#include <opengm/python/converter.hxx>
+#include <opengm/python/numpyview.hxx>
+
+#include <opengm/inference/icm.hxx>
+#include <opengm/learning/gridsearch-learning.hxx>
+#include <opengm/inference/messagepassing/messagepassing.hxx>
+
+namespace opengm{
+
+template<class LEARNER>
+class LearnerInferenceSuite: public boost::python::def_visitor<LearnerInferenceSuite<LEARNER> >{
+public:
+   friend class boost::python::def_visitor_access;
+
+   LearnerInferenceSuite(){
+
+   }
+
+   template<class INF>
+   static void pyLearnWithInf(LEARNER & learner, const typename INF::Parameter & param)
+   {
+       learner. template learn<INF>(param);
+   }
+
+   template <class classT>
+   void visit(classT& c) const{
+       // SOME INFERENCE METHODS
+       typedef typename LEARNER::GMType GMType;
+       typedef typename LEARNER::Parameter PyLearnerParam;
+       typedef typename LEARNER::DatasetType DatasetType;
+       typedef opengm::Minimizer ACC;
+
+       typedef opengm::ICM<GMType, ACC> IcmInf;
+       typedef opengm::BeliefPropagationUpdateRules<GMType, ACC> UpdateRulesType;
+       typedef opengm::MessagePassing<GMType, ACC, UpdateRulesType, opengm::MaxDistance> BpInf;
+
+      c
+          .def("_learn",&pyLearnWithInf<IcmInf>)
+          .def("_learn",&pyLearnWithInf<BpInf>)
+      ;
+   }
+};
+
+} // namespace opengm
+
+#endif // HELPER_HXX
+
diff --git a/src/interfaces/python/opengm/learning/pyGridSearchLearner.cxx b/src/interfaces/python/opengm/learning/pyGridSearchLearner.cxx
index d86c2b6..20cf28d 100644
--- a/src/interfaces/python/opengm/learning/pyGridSearchLearner.cxx
+++ b/src/interfaces/python/opengm/learning/pyGridSearchLearner.cxx
@@ -4,9 +4,7 @@
 #include <opengm/python/converter.hxx>
 #include <opengm/python/numpyview.hxx>
 
-#include <opengm/inference/icm.hxx>
-#include <opengm/learning/gridsearch-learning.hxx>
-#include <opengm/inference/messagepassing/messagepassing.hxx>
+#include "helper.hxx"
 
 namespace bp = boost::python;
 namespace op = opengm::python;
@@ -37,17 +35,10 @@ namespace opengm{
         return l;
     }
 
-    template<class LEARNER, class INF>
-    void pyLearnWithInf(LEARNER & learner, const typename INF::Parameter & param){
-        learner. template learn<INF>(param);
-    }
-
     template<class DATASET>
     void export_grid_search_learner(const std::string & clsName){
         typedef learning::GridSearchLearner<DATASET> PyLearner;
         typedef typename PyLearner::Parameter PyLearnerParam;
-        typedef typename  PyLearner::GMType GMType;
-        typedef typename PyLearner::DatasetType DatasetType;
 
         const std::string paramClsName = clsName + std::string("Parameter");
 
@@ -56,21 +47,9 @@ namespace opengm{
             .def("__init__", make_constructor(&pyGridSearchParamConstructor<PyLearnerParam> ,boost::python::default_call_policies()))
         ;
 
-
-
-        // SOME INFERENCE METHODS
-        typedef typename  PyLearner::GMType GMType;
-        typedef opengm::Minimizer ACC;
-
-        typedef opengm::ICM<GMType, ACC> IcmInf;
-        typedef opengm::BeliefPropagationUpdateRules<GMType, ACC> UpdateRulesType;
-        typedef opengm::MessagePassing<GMType, ACC, UpdateRulesType, opengm::MaxDistance> BpInf;
-
         bp::class_<PyLearner>( clsName.c_str(), bp::no_init )
         .def("__init__", make_constructor(&pyGridSearchConstructor<PyLearner> ,boost::python::default_call_policies()))
-        .def("_learn",&pyLearnWithInf<PyLearner, IcmInf>)
-        .def("_learn",&pyLearnWithInf<PyLearner, BpInf>)
-
+        .def(LearnerInferenceSuite<PyLearner>())
         ;
     }
 
diff --git a/src/interfaces/python/opengm/learning/pyStructMaxMarginLearner.cxx b/src/interfaces/python/opengm/learning/pyStructMaxMarginLearner.cxx
index 9e00396..f710220 100644
--- a/src/interfaces/python/opengm/learning/pyStructMaxMarginLearner.cxx
+++ b/src/interfaces/python/opengm/learning/pyStructMaxMarginLearner.cxx
@@ -9,9 +9,7 @@
 #include <opengm/inference/icm.hxx>
 #include <opengm/learning/struct-max-margin.hxx>
 
-#include <opengm/inference/icm.hxx>
-#include <opengm/learning/gridsearch-learning.hxx>
-#include <opengm/inference/messagepassing/messagepassing.hxx>
+#include "helper.hxx"
 
 namespace bp = boost::python;
 namespace op = opengm::python;
@@ -33,36 +31,20 @@ namespace opengm{
         return p;
     }
 
-    template<class LEARNER, class INF>
-    void pyLearnWithInf(LEARNER & learner, const typename INF::Parameter & param){
-        learner. template learn<INF>(param);
-    }
-
     template<class DATASET, class OPTIMIZER>
     void export_struct_max_margin_bundle_learner(const std::string & clsName){
         typedef learning::StructMaxMargin<DATASET, OPTIMIZER> PyLearner;
         typedef typename PyLearner::Parameter PyLearnerParam;
-        typedef typename PyLearner::GMType GMType;
         typedef typename PyLearner::DatasetType DatasetType;
 
         const std::string paramClsName = clsName + std::string("Parameter");
 
-
         bp::class_<PyLearnerParam>(paramClsName.c_str(), bp::init<>())
             .def("__init__", make_constructor(&pyStructMaxMarginBundleParamConstructor<PyLearnerParam> ,boost::python::default_call_policies()))
         ;
 
-        // SOME INFERENCE METHODS
-        typedef typename  PyLearner::GMType GMType;
-        typedef opengm::Minimizer ACC;
-
-        typedef opengm::ICM<GMType, ACC> IcmInf;
-        typedef opengm::BeliefPropagationUpdateRules<GMType, ACC> UpdateRulesType;
-        typedef opengm::MessagePassing<GMType, ACC, UpdateRulesType, opengm::MaxDistance> BpInf;
-
-        bp::class_<PyLearner>( clsName.c_str(), bp::init<DatasetType &, const PyLearnerParam &>() )
-            .def("_learn",&pyLearnWithInf<PyLearner, IcmInf>)
-            .def("_learn",&pyLearnWithInf<PyLearner, BpInf>)
+        boost::python::class_<PyLearner>( clsName.c_str(), boost::python::init<DatasetType &, const PyLearnerParam &>() )
+            .def(LearnerInferenceSuite<PyLearner>())
         ;
     }
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/opengm.git



More information about the debian-science-commits mailing list