[opengm] 189/386: batch function gen for lpotts

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Aug 31 08:37:38 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 93e10ccf069a00ca505fd876d1a53e37063590e8
Author: DerThorsten <thorsten.beier at iwr.uni-heidelberg.de>
Date:   Sun Dec 21 01:45:10 2014 +0100

     batch function gen for lpotts
---
 fubar/real_example_2.py                            |   6 +-
 .../python/opengm/learning/CMakeLists.txt          |   1 +
 src/interfaces/python/opengm/learning/learning.cxx |   6 +
 .../python/opengm/learning/pyLFunctionGen.cxx      | 148 +++++++++++++++++++++
 .../python/opengm/opengmcore/pyFunctionGen.cxx     |  57 ++++++++
 5 files changed, 215 insertions(+), 3 deletions(-)

diff --git a/fubar/real_example_2.py b/fubar/real_example_2.py
index 6b991f5..d565002 100644
--- a/fubar/real_example_2.py
+++ b/fubar/real_example_2.py
@@ -129,10 +129,10 @@ upperBounds = numpy.ones(nWeights)*2.0
 nTestPoints  =numpy.ones(nWeights).astype('uint64')*5
 
 # learner = learning.gridSearchLearner(dataset=dataset,lowerBounds=lowerBounds, upperBounds=upperBounds,nTestPoints=nTestPoints)
-#learner = learning.structMaxMarginLearner(dataset, 1.0, 0.001, 0)
-learner = learning.maxLikelihoodLearner(dataset)
+learner = learning.structMaxMarginLearner(dataset, 1.0, 0.001, 0)
+#learner = learning.maxLikelihoodLearner(dataset)
 
-learner.learn(infCls=opengm.inference.TrwsExternal, 
+learner.learn(infCls=opengm.inference.QpboExternal, 
               parameter=opengm.InfParam())
 
 for w in range(nWeights):
diff --git a/src/interfaces/python/opengm/learning/CMakeLists.txt b/src/interfaces/python/opengm/learning/CMakeLists.txt
index d50feb1..af13bd9 100644
--- a/src/interfaces/python/opengm/learning/CMakeLists.txt
+++ b/src/interfaces/python/opengm/learning/CMakeLists.txt
@@ -19,6 +19,7 @@ include_directories(
 #--------------------------------------------------------------
 set(PY_OPENGM_CORE_SOURCES
             learning.cxx
+            pyLFunctionGen.cxx
             pyWeights.cxx
             pyDataset.cxx
             pyLoss.cxx
diff --git a/src/interfaces/python/opengm/learning/learning.cxx b/src/interfaces/python/opengm/learning/learning.cxx
index fafc9ce..1c777ac 100644
--- a/src/interfaces/python/opengm/learning/learning.cxx
+++ b/src/interfaces/python/opengm/learning/learning.cxx
@@ -36,6 +36,9 @@ namespace opengm{
 
     template<class DATASET>
     void export_max_likelihood_learner(const std::string & clsName);
+
+    template<class GM_ADDER,class GM_MULT>  
+    void export_function_generator_lpotts();
 }
 
 
@@ -51,6 +54,9 @@ BOOST_PYTHON_MODULE_INIT(_learning) {
 
     opengm::export_weights();
 
+    // function exporter
+    opengm::export_function_generator_lpotts<op::GmAdder,op::GmMultiplier>();
+
     // export loss
     opengm::export_loss<op::GmAdder>();
 
diff --git a/src/interfaces/python/opengm/learning/pyLFunctionGen.cxx b/src/interfaces/python/opengm/learning/pyLFunctionGen.cxx
new file mode 100644
index 0000000..690e273
--- /dev/null
+++ b/src/interfaces/python/opengm/learning/pyLFunctionGen.cxx
@@ -0,0 +1,148 @@
+#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/graphicalmodel/weights.hxx"
+#include "opengm/functions/learnable/lpotts.hxx"
+#include "opengm/functions/learnable/lunary.hxx"
+#include "opengm/functions/learnable/lsum_of_experts.hxx"
+
+#include "../opengmcore/functionGenBase.hxx"
+
+
+namespace bp = boost::python;
+namespace op = opengm::python;
+namespace ol = opengm::learning;
+namespace ofl = opengm::functions::learnable;
+namespace opengm{
+
+
+
+    template<class GM_ADDER,class GM_MULT>
+    class LPottsFunctionGen :
+    public FunctionGeneratorBase<GM_ADDER,GM_MULT>
+    {
+    public:       
+        typedef typename GM_ADDER::ValueType ValueType;
+        typedef typename GM_ADDER::IndexType IndexType;
+        typedef typename GM_ADDER::LabelType LabelType;
+        typedef ol::Weights<ValueType> WeightType;
+        typedef  ofl::LPotts<ValueType, IndexType, LabelType> FType;
+
+        LPottsFunctionGen(
+            WeightType & weights,
+            const size_t numFunctions,
+            const size_t numLabels,
+            op::NumpyView<ValueType, 2> features,
+            op::NumpyView<IndexType, 1> weightIds,
+            const bool addConstFeature
+        ):
+        FunctionGeneratorBase<GM_ADDER,GM_MULT>(),
+        weights_(weights),
+        numFunctions_(numFunctions),
+        numLabels_(numLabels),
+        features_(features),
+        weightIds_(weightIds.begin(), weightIds.end()),
+        addConstFeature_(addConstFeature)
+        {
+            OPENGM_CHECK_OP(features.shape(0), == , numFunctions, "wrong shape");
+            OPENGM_CHECK_OP(features.shape(1)+int(addConstFeature), == , weightIds.shape(0), "wrong shape");
+        }
+ 
+
+        template<class GM>
+        std::vector< typename GM::FunctionIdentifier > * addFunctionsGeneric(GM & gm)const{
+
+            typedef typename GM::FunctionIdentifier Fid;
+            typedef std::vector<Fid> FidVector;
+            FidVector * fidVector = new FidVector(numFunctions_);
+
+            const size_t nFeat =features_.shape(1);
+            std::vector<ValueType> fFeat(nFeat+int(addConstFeature_));
+            for(size_t  i=0;i<numFunctions_;++i){
+                for(size_t f=0; f<nFeat; ++f){
+                    fFeat[f] = features_(i,f);
+                }
+                if(addConstFeature_){
+                    fFeat[nFeat] = 1.0;
+                }
+                const FType f(weights_, numLabels_, weightIds_, fFeat);
+                (*fidVector)[i] = gm.addFunction(f);
+            }   
+            return fidVector;
+        }
+
+        virtual std::vector< typename GM_ADDER::FunctionIdentifier > * addFunctions(GM_ADDER & gm)const{
+            return this-> template addFunctionsGeneric<GM_ADDER>(gm);
+        }
+        virtual std::vector< typename GM_MULT::FunctionIdentifier >  * addFunctions(GM_MULT & gm)const{
+            throw RuntimeError("Wrong Operator for Learning");
+            return NULL;
+        }
+    private:
+        WeightType & weights_;
+        size_t numFunctions_;
+        size_t numLabels_;
+        op::NumpyView<ValueType, 2>  features_;
+        std::vector<size_t>  weightIds_; 
+        bool addConstFeature_;
+    };
+
+
+    template<class GM_ADDER,class GM_MULT>
+    FunctionGeneratorBase<GM_ADDER,GM_MULT> * lpottsFunctionGen(
+        ol::Weights<typename GM_ADDER::ValueType> weights,
+        const size_t numFunctions,
+        const size_t numLabels,
+        opengm::python::NumpyView<typename GM_ADDER::ValueType,2> features,
+        opengm::python::NumpyView<typename GM_ADDER::IndexType,1> weightIds,
+        const bool addConstFeature
+    ){
+        FunctionGeneratorBase<GM_ADDER,GM_MULT> * ptr = 
+            new LPottsFunctionGen<GM_ADDER,GM_MULT>(weights,numFunctions,numLabels,features,weightIds, addConstFeature);
+        return ptr;
+    }
+
+
+
+
+
+
+
+
+
+
+
+
+    template<class GM_ADDER,class GM_MULT>  
+    void export_function_generator_lpotts(){
+        typedef LPottsFunctionGen<GM_ADDER, GM_MULT> FGen;
+
+         bp::def("_lpottsFunctionsGen",&lpottsFunctionGen<GM_ADDER,GM_MULT>,bp::return_value_policy<bp::manage_new_object>(),
+            (
+                bp::arg("weights"),
+                bp::arg("numFunctions"),
+                bp::arg("numLabels"),
+                bp::arg("features"),
+                bp::arg("weightIds"),
+                bp::arg("addConstFeature")
+            ),
+      "factory function to generate a lpotts function generator object which can be passed to ``gm.addFunctions(functionGenerator)``");
+
+    }
+
+
+
+
+
+
+
+
+
+}
+
+
+template void opengm::export_function_generator_lpotts<op::GmAdder,op::GmMultiplier>();
diff --git a/src/interfaces/python/opengm/opengmcore/pyFunctionGen.cxx b/src/interfaces/python/opengm/opengmcore/pyFunctionGen.cxx
index c9f9d3a..2d138f6 100644
--- a/src/interfaces/python/opengm/opengmcore/pyFunctionGen.cxx
+++ b/src/interfaces/python/opengm/opengmcore/pyFunctionGen.cxx
@@ -117,6 +117,63 @@ private:
 };
 
 
+template<class GM_ADDER,class GM_MULT,class FUNCTION_TYPE>
+class LPottsFunctionGen :
+public FunctionGeneratorBase<GM_ADDER,GM_MULT>
+{
+public:
+    typedef FUNCTION_TYPE FunctionType;
+    typedef typename FUNCTION_TYPE::ValueType ValueType;
+    typedef typename FUNCTION_TYPE::IndexType IndexType;
+    typedef typename FUNCTION_TYPE::LabelType LabelType;
+    LPottsFunctionGen(
+
+        size_t numberOfLabels,
+        opengm::python::NumpyView<LabelType,2> numLabels1Array,
+        opengm::python::NumpyView<LabelType,1> numLabels2Array,
+        opengm::python::NumpyView<ValueType,1> valEqualArray,
+        opengm::python::NumpyView<ValueType,1> valNotEqualArray
+    ):FunctionGeneratorBase<GM_ADDER,GM_MULT>(),
+    numLabels1Array_(numLabels1Array),
+    numLabels2Array_(numLabels2Array),
+    valEqualArray_(valEqualArray),
+    valNotEqualArray_(valNotEqualArray)
+    {
+        numFunctions_=std::max( 
+            std::max(numLabels1Array_.shape(0),numLabels2Array_.shape(0)) , 
+            std::max(valEqualArray_.shape(0),valNotEqualArray_.shape(0))
+        );
+    }  
+
+   template<class GM>
+   std::vector< typename GM::FunctionIdentifier > * addFunctionsGeneric(GM & gm)const{
+      std::vector< typename GM::FunctionIdentifier > * fidVector = new std::vector< typename GM::FunctionIdentifier > (numFunctions_);
+      for(size_t  i=0;i<numFunctions_;++i){
+         const LabelType numL1=i<numLabels1Array_.size() ? numLabels1Array_(i) : numLabels1Array_(numLabels1Array_.size()-1);
+         const LabelType numL2=i<numLabels2Array_.size() ? numLabels2Array_(i) : numLabels2Array_(numLabels2Array_.size()-1);
+         const ValueType veq=i<valEqualArray_.size() ? valEqualArray_(i) : valEqualArray_(valEqualArray_.size()-1);
+         const ValueType vneq=i<valNotEqualArray_.size() ? valNotEqualArray_(i) : valNotEqualArray_(valNotEqualArray_.size()-1);
+         (*fidVector)[i]=gm.addFunction(FunctionType(numL1,numL2,veq,vneq));
+      }
+      return fidVector;
+   }
+    
+   virtual std::vector< typename GM_ADDER::FunctionIdentifier > * addFunctions(GM_ADDER & gm)const{
+      return this-> template addFunctionsGeneric<GM_ADDER>(gm);
+   }
+   virtual std::vector< typename GM_MULT::FunctionIdentifier >  * addFunctions(GM_MULT & gm)const{
+      return this-> template addFunctionsGeneric<GM_MULT>(gm);
+   }
+private:
+   opengm::python::NumpyView<LabelType,1>  numLabels1Array_;
+   opengm::python::NumpyView<LabelType,1>  numLabels2Array_;
+   opengm::python::NumpyView<ValueType,1>  valEqualArray_;
+   opengm::python::NumpyView<ValueType,1>  valNotEqualArray_;
+   size_t numFunctions_;
+};
+
+
+
 template<class GM_ADDER,class GM_MULT,class FUNCTION>
 inline FunctionGeneratorBase<GM_ADDER,GM_MULT> * pottsFunctionGen(
     opengm::python::NumpyView<typename GM_ADDER::LabelType,1> numLabels1Array,

-- 
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