[opengm] 190/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 6d1d0054b25a134139f79c9f733c6efdc09e160a
Author: DerThorsten <thorsten.beier at iwr.uni-heidelberg.de>
Date:   Sun Dec 21 01:52:50 2014 +0100

     batch function gen for lpotts
---
 src/interfaces/python/opengm/learning/learning.cxx |   4 +-
 .../python/opengm/learning/pyLFunctionGen.cxx      | 106 ++++++++++++++++++++-
 2 files changed, 105 insertions(+), 5 deletions(-)

diff --git a/src/interfaces/python/opengm/learning/learning.cxx b/src/interfaces/python/opengm/learning/learning.cxx
index 1c777ac..f0e4dda 100644
--- a/src/interfaces/python/opengm/learning/learning.cxx
+++ b/src/interfaces/python/opengm/learning/learning.cxx
@@ -38,7 +38,7 @@ namespace opengm{
     void export_max_likelihood_learner(const std::string & clsName);
 
     template<class GM_ADDER,class GM_MULT>  
-    void export_function_generator_lpotts();
+    void export_lfunction_generator();
 }
 
 
@@ -55,7 +55,7 @@ BOOST_PYTHON_MODULE_INIT(_learning) {
     opengm::export_weights();
 
     // function exporter
-    opengm::export_function_generator_lpotts<op::GmAdder,op::GmMultiplier>();
+    opengm::export_lfunction_generator<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
index 690e273..0014fa8 100644
--- a/src/interfaces/python/opengm/learning/pyLFunctionGen.cxx
+++ b/src/interfaces/python/opengm/learning/pyLFunctionGen.cxx
@@ -92,6 +92,93 @@ namespace opengm{
     };
 
 
+
+    template<class GM_ADDER,class GM_MULT>
+    class LUnaryFunctionGen :
+    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;
+
+        LUnaryFunctionGen(
+            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> * lunaryFunctionGen(
+        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 LUnaryFunctionGen<GM_ADDER,GM_MULT>(weights,numFunctions,numLabels,features,weightIds, addConstFeature);
+        return ptr;
+    }
+
+
     template<class GM_ADDER,class GM_MULT>
     FunctionGeneratorBase<GM_ADDER,GM_MULT> * lpottsFunctionGen(
         ol::Weights<typename GM_ADDER::ValueType> weights,
@@ -118,7 +205,7 @@ namespace opengm{
 
 
     template<class GM_ADDER,class GM_MULT>  
-    void export_function_generator_lpotts(){
+    void export_lfunction_generator(){
         typedef LPottsFunctionGen<GM_ADDER, GM_MULT> FGen;
 
          bp::def("_lpottsFunctionsGen",&lpottsFunctionGen<GM_ADDER,GM_MULT>,bp::return_value_policy<bp::manage_new_object>(),
@@ -130,7 +217,20 @@ namespace opengm{
                 bp::arg("weightIds"),
                 bp::arg("addConstFeature")
             ),
-      "factory function to generate a lpotts function generator object which can be passed to ``gm.addFunctions(functionGenerator)``");
+            "factory function to generate a lpotts function generator object which can be passed to ``gm.addFunctions(functionGenerator)``"
+        );
+
+         bp::def("_lunaryFunctionsGen",&lunaryFunctionGen<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 lunary function generator object which can be passed to ``gm.addFunctions(functionGenerator)``"
+        );
 
     }
 
@@ -145,4 +245,4 @@ namespace opengm{
 }
 
 
-template void opengm::export_function_generator_lpotts<op::GmAdder,op::GmMultiplier>();
+template void opengm::export_lfunction_generator<op::GmAdder,op::GmMultiplier>();

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