[opengm] 107/386: Make sumOfExperts available from Python
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Wed Aug 31 08:35:13 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 6aba5d5e26d6307da55ebf66300f72aa6b204adc
Author: Carsten Haubold <carstenhaubold at googlemail.com>
Date: Wed Dec 17 17:04:42 2014 +0100
Make sumOfExperts available from Python
---
.../opengm/functions/learnable/sum_of_experts.hxx | 4 +-
include/opengm/python/numpyview.hxx | 15 ++++++
.../python/opengm/opengmcore/pyFunctionTypes.cxx | 61 ++++++++++++++++++----
3 files changed, 69 insertions(+), 11 deletions(-)
diff --git a/include/opengm/functions/learnable/sum_of_experts.hxx b/include/opengm/functions/learnable/sum_of_experts.hxx
index ffe676f..b6acb29 100644
--- a/include/opengm/functions/learnable/sum_of_experts.hxx
+++ b/include/opengm/functions/learnable/sum_of_experts.hxx
@@ -56,10 +56,10 @@ public:
T weightGradient(size_t,ITERATOR) const;
protected:
- const opengm::learning::Weights<T>* weights_;
+ const opengm::learning::Weights<T>* weights_;
std::vector<L> shape_;
std::vector<size_t> weightIDs_;
- std::vector<marray::Marray<T> > feat_;
+ std::vector<marray::Marray<T> > feat_;
friend class opengm::FunctionSerialization<opengm::functions::learnable::SumOfExperts<T, I, L> >;
};
diff --git a/include/opengm/python/numpyview.hxx b/include/opengm/python/numpyview.hxx
index bf0eb21..8aae5e8 100644
--- a/include/opengm/python/numpyview.hxx
+++ b/include/opengm/python/numpyview.hxx
@@ -174,6 +174,21 @@ public:
return view_.end();
}
+ marray::View< V, false > getSliceView(size_t dimension, size_t sliceIndex) {
+ // create base coordinate
+ std::vector<size_t> baseIterator(view_.dimension(), 0);
+ baseIterator[dimension] = sliceIndex;
+
+ // create expected shape
+ std::vector<size_t> shapeIterator(view_.shapeBegin(), view_.shapeEnd());
+ shapeIterator[dimension] = 1;
+
+ // return sub view of slice with reduced dimensions
+ marray::View< V, false > new_view = view_.view(baseIterator.begin(), shapeIterator.begin());
+ new_view.squeeze();
+ return new_view;
+ }
+
//boost::python::object arrayObject()const{
// return arrayObj_;
//};
diff --git a/src/interfaces/python/opengm/opengmcore/pyFunctionTypes.cxx b/src/interfaces/python/opengm/opengmcore/pyFunctionTypes.cxx
index 7106784..a727362 100644
--- a/src/interfaces/python/opengm/opengmcore/pyFunctionTypes.cxx
+++ b/src/interfaces/python/opengm/opengmcore/pyFunctionTypes.cxx
@@ -8,10 +8,10 @@
#include <map>
#include "nifty_iterator.hxx"
-#include <opengm/python/opengmpython.hxx>
-#include <opengm/python/converter.hxx>
-#include <opengm/python/numpyview.hxx>
-#include <opengm/python/pythonfunction.hxx>
+#include "opengm/python/opengmpython.hxx"
+#include "opengm/python/converter.hxx"
+#include "opengm/python/numpyview.hxx"
+#include "opengm/python/pythonfunction.hxx"
#include "copyhelper.hxx"
@@ -26,8 +26,9 @@
#include "opengm/functions/truncated_squared_difference.hxx"
#include "opengm/functions/sparsemarray.hxx"
-
-
+#include "opengm/functions/learnable/lpotts.hxx"
+#include "opengm/functions/learnable/lunary.hxx"
+#include "opengm/functions/learnable/sum_of_experts.hxx"
using namespace boost::python;
@@ -241,7 +242,6 @@ namespace pyfunction{
OPENGM_CHECK_OP(boost::python::len(weightIds), == ,boost::python::len(features) ,"weightIds must be as long as features");
-
FUNCTION * f = NULL;
typedef opengm::functions::learnable::FeaturesAndIndices<
opengm::python::GmValueType,
@@ -276,8 +276,36 @@ namespace pyfunction{
}
f = new FUNCTION(pyWeights, fiVec);
return f;
- }
+ }
+
+ template<class FUNCTION>
+ FUNCTION * sumOfExpertsConstructor(
+ boost::python::object pyShape,
+ opengm::python::PyWeights& pyWeights,
+ opengm::python::NumpyView<opengm::python::GmIndexType,1> weightIds,
+ opengm::python::NumpyView<opengm::python::GmValueType,3> features
+ ){
+ stl_input_iterator<int> begin(pyShape), end;
+ std::vector<opengm::python::GmLabelType> shape(begin, end);
+ std::vector<size_t> weightIdVec(weightIds.begin(), weightIds.end());
+ std::vector<marray::Marray<opengm::python::GmValueType> > featureVec;
+ for(size_t i = 0; i < features.shape(0); i++)
+ {
+ featureVec.push_back(marray::Marray<opengm::python::GmValueType>(features.getSliceView(0, i)));
+ }
+ FUNCTION * f = NULL;
+
+ OPENGM_CHECK_OP(weightIdVec.size(), ==, featureVec.size(),"wrong shapes");
+ if(weightIdVec.size() > 0)
+ {
+ OPENGM_CHECK_OP(shape[0], ==, featureVec[0].shape(0),"wrong feature array shapes");
+ OPENGM_CHECK_OP(shape[1], ==, featureVec[0].shape(1),"wrong feature array shapes");
+ }
+
+ f = new FUNCTION(shape, pyWeights, weightIdVec, featureVec);
+ return f;
+ }
////////////////////////////////////////
@@ -447,7 +475,9 @@ void export_functiontypes(){
typedef opengm::SparseFunction <ValueType,IndexType,LabelType> PySparseFunction;
typedef opengm::python::PythonFunction <ValueType,IndexType,LabelType> PyPythonFunction;
typedef opengm::functions::learnable::LPotts <ValueType,IndexType,LabelType> PyLPottsFunction;
- typedef opengm::functions::learnable::LUnary <ValueType,IndexType,LabelType> PyLUnaryFunction;
+ typedef opengm::functions::learnable::LUnary <ValueType,IndexType,LabelType> PyLUnaryFunction;
+ typedef opengm::functions::learnable::SumOfExperts <ValueType,IndexType,LabelType> PySumOfExpertsFunction;
+
// vector exporters
export_function_type_vector<PyExplicitFunction>("ExplicitFunctionVector");
@@ -746,6 +776,19 @@ void export_functiontypes(){
"todo"
)
;
+
+ FUNCTION_TYPE_EXPORTER_HELPER(PySumOfExpertsFunction,"SumOfExpertsFunction")
+ .def("__init__", make_constructor(&pyfunction::sumOfExpertsConstructor<PySumOfExpertsFunction> ,default_call_policies(),
+ (
+ boost::python::arg("shape"),
+ boost::python::arg("weight"),
+ boost::python::arg("weightIds"),
+ boost::python::arg("features")
+ )
+ ),
+ "todo"
+ )
+ ;
}
template void export_functiontypes<opengm::python::GmValueType,opengm::python::GmIndexType>();
--
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