[opengm] 108/386: improved python learning
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 8cd242d531e9c67a2b5254de25b342c618451b3d
Author: DerThorsten <thorsten.beier at iwr.uni-heidelberg.de>
Date: Wed Dec 17 17:27:32 2014 +0100
improved python learning
---
fubar/example1.py | 1 -
src/interfaces/python/opengm/learning/__init__.py | 34 ++++++++++++++++++----
.../python/opengm/learning/pyGridSearchLearner.cxx | 16 ++++++++++
3 files changed, 45 insertions(+), 6 deletions(-)
diff --git a/fubar/example1.py b/fubar/example1.py
index 6c4aeb6..038399b 100644
--- a/fubar/example1.py
+++ b/fubar/example1.py
@@ -6,4 +6,3 @@ import opengm.learning
datasetA = opengm.learning.createDataset(loss='hamming')
datasetB = opengm.learning.createDataset(loss='generalized-hamming')
-print datasetA, datasetB
diff --git a/src/interfaces/python/opengm/learning/__init__.py b/src/interfaces/python/opengm/learning/__init__.py
index 384f0a3..b71b5b4 100644
--- a/src/interfaces/python/opengm/learning/__init__.py
+++ b/src/interfaces/python/opengm/learning/__init__.py
@@ -1,10 +1,9 @@
from _learning import *
+import numpy
+import struct
-
-
-
-
-
+DatasetWithHammingLoss.lossType = 'hamming'
+DatasetWithGeneralizedHammingLoss.lossType = 'generalized-hamming'
def createDataset(loss='hamming', numInstances=0):
@@ -19,3 +18,28 @@ def createDataset(loss='hamming', numInstances=0):
else:
raise RuntimeError("loss must be 'hamming' /'h' or 'generalized-hamming'/'gh' ")
+
+
+
+def gridSearchLearner(dataset, lowerBounds, upperBounds, nTestPoints):
+
+ if dataset.__class__.lossType == 'hamming':
+ learnerCls = GridSearch_HammingLoss
+ leanerParamCls = GridSearch_HammingLossParameter
+ elif dataset.__class__.lossType == 'generalized-hamming':
+ learnerCls = GridSearch_GeneralizedHammingLoss
+ leanerParamCls = GridSearch_GeneralizedHammingLossParameter
+
+ nr = numpy.require
+
+ sizeT_type = 'uint64'
+
+ if struct.calcsize("P") * 8 == 32:
+ sizeT_type = 'uint32'
+
+ param = leanerParamCls(nr(lowerBounds,dtype='float64'), nr(lowerBounds,dtype='float64'),
+ nr(lowerBounds,dtype=sizeT_type))
+
+ learner = learnerCls(dataset, param)
+
+ return learner
diff --git a/src/interfaces/python/opengm/learning/pyGridSearchLearner.cxx b/src/interfaces/python/opengm/learning/pyGridSearchLearner.cxx
index e8f9cfb..74946f1 100644
--- a/src/interfaces/python/opengm/learning/pyGridSearchLearner.cxx
+++ b/src/interfaces/python/opengm/learning/pyGridSearchLearner.cxx
@@ -14,6 +14,21 @@ namespace ol = opengm::learning;
namespace opengm{
+
+ template<class PARAM>
+ PARAM * pyGridSearchParamConstructor(
+ op::NumpyView<double> lowerBound,
+ op::NumpyView<double> upperBound,
+ op::NumpyView<size_t> nTestPoints
+ ){
+ PARAM * p = new PARAM();
+ p->parameterUpperbound_.assign(lowerBound.begin(), lowerBound.end());
+ p->parameterLowerbound_.assign(upperBound.begin(), upperBound.end());
+ p->testingPoints_.assign(nTestPoints.begin(), nTestPoints.end());
+ return p;
+ }
+
+
template<class LEARNER>
void pyLearnDummy(LEARNER & learner){
@@ -35,6 +50,7 @@ namespace opengm{
bp::class_<PyLearnerParam>(paramClsName.c_str(), bp::init<>())
+ .def("__init__", make_constructor(&pyGridSearchParamConstructor<PyLearnerParam> ,boost::python::default_call_policies()))
;
bp::class_<PyLearner>( clsName.c_str(), bp::init<DatasetType &, const PyLearnerParam &>() )
--
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