[opengm] 04/386: add test and learnable potts function
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Wed Aug 31 08:34:58 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 ada59eb9f4ab36dfae8b959d16d815116a125b7b
Author: Joerg Kappes <jkappes at goedel.(none)>
Date: Fri May 16 14:31:32 2014 +0200
add test and learnable potts function
---
include/opengm/functions/learnable/lpotts.hxx | 223 +++++++++++++++++++++++++
include/opengm/functions/learnablefunction.hxx | 35 ++--
src/unittest/test_learnable_functions.cxx | 57 ++++++-
3 files changed, 295 insertions(+), 20 deletions(-)
diff --git a/include/opengm/functions/learnable/lpotts.hxx b/include/opengm/functions/learnable/lpotts.hxx
new file mode 100644
index 0000000..bb548cd
--- /dev/null
+++ b/include/opengm/functions/learnable/lpotts.hxx
@@ -0,0 +1,223 @@
+#pragma once
+#ifndef OPENGM_LEARNABLE_POTTS_FUNCTION_HXX
+#define OPENGM_LEARNABLE_POTTS_FUNCTION_HXX
+
+#include <algorithm>
+#include <vector>
+#include <cmath>
+
+#include "opengm/opengm.hxx"
+#include "opengm/functions/function_registration.hxx"
+#include "opengm/functions/function_properties_base.hxx"
+
+namespace opengm {
+namespace functions {
+namespace learnable {
+
+/// Learnable feature function for two variables
+///
+/// f(x) = w * A(x) * feat
+/// - w = parameter vector
+/// - feat = feature vector
+/// - A = assignment matrix variant to the labeling x
+///
+/// derive from this class and implement the function
+/// paramaterGradient(i,x)= A(x)_{i,*}*feat
+///
+/// \ingroup functions
+template<class T, class I = size_t, class L = size_t>
+class LPotts
+: public FunctionBase<LearnableFeatureFunction<T, I, L>, T, I, L>
+{
+public:
+ typedef T ValueType;
+ typedef L LabelType;
+ typedef I IndexType;
+
+ LPotts(
+ const Parameters<T,I>& parameters,
+ const L numLabels,
+ const std::vector<size_t>& parameterIDs,
+ const std::vector<T>& feat
+ );
+ L shape(const size_t) const;
+ size_t size() const;
+ size_t dimension() const;
+ template<class ITERATOR> T operator()(ITERATOR) const;
+
+ // parameters
+ size_t numberOfParameters()const
+ {return parameterIDs_.size();}
+ I parameterIndex(const size_t paramNumber) const
+ {return parameterIDs_[paramNumber];} //dummy
+ template<class ITERATOR>
+ T paramaterGradient(size_t,ITERATOR) const;
+
+protected:
+ const Parameters<T,I> * parameters_;
+ const L numLabels_;
+ const std::vector<size_t> parameterIDs_;
+ const std::vector<T> feat_;
+
+
+ //friend class FunctionSerialization<opengm::functions::learnable::LPotts<T, I, L> > ;
+};
+ /*
+/// FunctionSerialization
+template<class T, class I, class L>
+class FunctionSerialization<opengm::functions::learnable::LPotts<T, I, L> > {
+public:
+ typedef typename opengm::functions::learnable::LPotts<T, I, L>::ValueType ValueType;
+
+ static size_t indexSequenceSize(const opengm::functions::learnable::LPotts<T, I, L>&);
+ static size_t valueSequenceSize(const opengm::functions::learnable::LPotts<T, I, L>&);
+ template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR>
+ static void serialize(const opengm::functions::learnable::LPotts<T, I, L>&, INDEX_OUTPUT_ITERATOR, VALUE_OUTPUT_ITERATOR);
+ template<class INDEX_INPUT_ITERATOR, class VALUE_INPUT_ITERATOR>
+ static void deserialize( INDEX_INPUT_ITERATOR, VALUE_INPUT_ITERATOR, opengm::functions::learnable::LPotts<T, I, L>&);
+};
+
+template<class T, class I, class L>
+struct FunctionRegistration<opengm::functions::learnable::LPotts<T, I, L> > {
+ enum ID {
+ Id = opengm::FUNCTION_TYPE_ID_OFFSET + 100 + 65
+ };
+};
+ */
+
+template <class T, class I, class L>
+inline
+LPotts<T, I, L>::LPotts
+(
+ const Parameters<T,I>& parameters,
+ const L numLabels,
+ const std::vector<size_t>& parameterIDs,
+ const std::vector<T>& feat
+ )
+ : parameters_(¶meters), numLabels_(numLabels), parameterIDs_(parameterIDs),feat_(feat)
+{
+ OPENGM_ASSERT( parameterIDs_.size()==feat_.size() );
+}
+
+
+template <class T, class I, class L>
+template <class ITERATOR>
+inline T
+LPotts<T, I, L>::paramaterGradient
+(
+ size_t parameterNumber,
+ ITERATOR begin
+) const {
+ OPENGM_ASSERT(parameterNumber< numberOfParameters());
+ if( *(begin) != *(begin+1) )
+ return (*this).feat_[parameterNumber];
+ return 0;
+}
+
+template <class T, class I, class L>
+template <class ITERATOR>
+inline T
+LPotts<T, I, L>::operator()
+(
+ ITERATOR begin
+) const {
+ T val = 0;
+ for(size_t i=0;i<numberOfParameters();++i){
+ val += parameters_->getParameter(i) * paramaterGradient(i,begin);
+ }
+ return val;
+}
+
+
+template <class T, class I, class L>
+inline L
+LPotts<T, I, L>::shape
+(
+ const size_t i
+) const {
+ return numLabels_;
+}
+
+template <class T, class I, class L>
+inline size_t
+LPotts<T, I, L>::dimension() const {
+ return 2;
+}
+
+template <class T, class I, class L>
+inline size_t
+LPotts<T, I, L>::size() const {
+ return numLabels_*numLabels_;
+}
+
+ /*
+template<class T, class I, class L>
+inline size_t
+FunctionSerialization<opengm::functions::learnable::LPotts<T, I, L> >::indexSequenceSize
+(
+ const opengm::functions::learnable::LPotts<T, I, L> & src
+) {
+ return 2+parameterIDs_.size();
+}
+
+template<class T, class I, class L>
+inline size_t
+FunctionSerialization<opengm::functions::learnable::LPotts<T, I, L> >::valueSequenceSize
+(
+ const opengm::functions::learnable::LPotts<T, I, L> & src
+) {
+ return feat_.size();
+}
+
+template<class T, class I, class L>
+template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
+inline void
+FunctionSerialization<opengm::functions::learnable::LPotts<T, I, L> >::serialize
+(
+ const opengm::functions::learnable::LPotts<T, I, L> & src,
+ INDEX_OUTPUT_ITERATOR indexOutIterator,
+ VALUE_OUTPUT_ITERATOR valueOutIterator
+) {
+ *indexOutIterator = src.numLabels_;
+ ++indexOutIterator;
+ *indexOutIterator = src.feat_.size();
+ ++indexOutIterator;
+ for(size_t i=0; i<parameterIDs_.size();++i){
+ *indexOutIterator = src.parameterIndex(i);
+ ++indexOutIterator;
+ }
+ for(size_t i=0; i<feat_.size();++i){
+ *valueOutIterator = feat_[i];
+ ++valueOutIterator;
+ }
+}
+
+template<class T, class I, class L>
+template<class INDEX_INPUT_ITERATOR, class VALUE_INPUT_ITERATOR >
+inline void
+FunctionSerialization<opengm::functions::learnable::LPotts<T, I, L> >::deserialize
+(
+ INDEX_INPUT_ITERATOR indexInIterator,
+ VALUE_INPUT_ITERATOR valueInIterator,
+ opengm::functions::learnable::LPotts<T, I, L> & dst
+) {
+ const size_t numL=*indexInIterator;
+ ++ indexInIterator;
+ const size_t numW=*indexInIterator;
+ std::vector<T> feat(numW);
+ std::vector<size_t> id(numW);
+ for(size_t i=0; i<numW;++i){
+ feat[i]=*valueInIterator;
+ id[i]=*indexIterator;
+ ++indexIterator;
+ ++valueInIterator;
+ }
+ // dst=LPottsFunction<T, I, L>(shape1, shape2, param1, param2);
+}
+*/
+
+} // namespace learnable
+} // namespace functions
+} // namespace opengm
+
+#endif // #ifndef OPENGM_LEARNABLE_FUNCTION_HXX
diff --git a/include/opengm/functions/learnablefunction.hxx b/include/opengm/functions/learnablefunction.hxx
index 2a80a15..394c011 100644
--- a/include/opengm/functions/learnablefunction.hxx
+++ b/include/opengm/functions/learnablefunction.hxx
@@ -33,29 +33,29 @@ public:
typedef I IndexType;
LearnableFeatureFunction(
- const Parameters<ValueType,IndexType>& parameters,
- const std::vector<LabelType>& shape,
+ const Parameters<T,I>& parameters,
+ const std::vector<L>& shape,
const std::vector<size_t>& parameterIDs,
- const std::vector<ValueType>& feat
+ const std::vector<T>& feat
);
- LabelType shape(const size_t) const;
+ L shape(const size_t) const;
size_t size() const;
size_t dimension() const;
- template<class ITERATOR> ValueType operator()(ITERATOR) const;
+ template<class ITERATOR> T operator()(ITERATOR) const;
// parameters
size_t numberOfParameters()const
{return parameterIDs_.size();}
- IndexType parameterIndex(const size_t paramNumber) const
+ I parameterIndex(const size_t paramNumber) const
{return parameterIDs_[paramNumber];} //dummy
template<class ITERATOR>
- ValueType paramaterGradient(size_t,ITERATOR) const;
+ T paramaterGradient(size_t,ITERATOR) const;
-private:
- const Parameters<ValueType,IndexType> * parameters_;
- const std::vector<LabelType> shape_;
- const std::vector<ValueType> parameterIDs_;
- const std::vector<ValueType> feat_;
+protected:
+ const Parameters<T,I> * parameters_;
+ const std::vector<L> shape_;
+ const std::vector<size_t> parameterIDs_;
+ const std::vector<T> feat_;
friend class FunctionSerialization<LearnableFeatureFunction<T, I, L> > ;
@@ -73,10 +73,10 @@ template <class T, class I, class L>
inline
LearnableFeatureFunction<T, I, L>::LearnableFeatureFunction
(
- const Parameters<ValueType,IndexType>& parameters,
- const std::vector<LabelType>& shape,
+ const Parameters<T,I>& parameters,
+ const std::vector<L>& shape,
const std::vector<size_t>& parameterIDs,
- const std::vector<ValueType>& feat
+ const std::vector<T>& feat
)
: parameters_(¶meters), shape_(shape), parameterIDs_(parameterIDs),feat_(feat)
{}
@@ -101,7 +101,7 @@ LearnableFeatureFunction<T, I, L>::operator()
(
ITERATOR begin
) const {
- ValueType val = 0;
+ T val = 0;
for(size_t i=0;i<numberOfParameters();++i){
val += parameters_->getParameter(i) * paramaterGradient(i,begin);
}
@@ -133,6 +133,9 @@ LearnableFeatureFunction<T, I, L>::size() const {
}
+
+
+
} // namespace opengm
#endif // #ifndef OPENGM_LEARNABLE_FUNCTION_HXX
diff --git a/src/unittest/test_learnable_functions.cxx b/src/unittest/test_learnable_functions.cxx
index 4fc46e1..90ec460 100644
--- a/src/unittest/test_learnable_functions.cxx
+++ b/src/unittest/test_learnable_functions.cxx
@@ -1,13 +1,61 @@
#include <vector>
#include "opengm/functions/learnablefunction.hxx"
+#include "opengm/functions/learnable/lpotts.hxx"
#include <opengm/unittests/test.hxx>
template<class T>
struct LearnableFunctionsTest {
+ typedef size_t LabelType;
+ typedef size_t IndexType;
+ typedef T ValueType;
- void run(){
- std::cout << "OK" << std::endl;
+ void testLearnableFeatureFunction(){
+ std::cout << " * Learnable Feature Function ..." << std::flush;
+ // parameter
+ const size_t numparam = 1;
+ opengm::Parameters<ValueType,IndexType> param(numparam);
+ param.setParameter(0,5.0);
+
+ std::vector<LabelType> shape(2,3);
+ std::vector<size_t> pIds(1,0);
+ std::vector<ValueType> feat(1,1);
+ // function
+ opengm::LearnableFeatureFunction<ValueType,IndexType,LabelType> lfunc(param,shape,pIds,feat);
+
+ LabelType l[] ={0,0};
+ for(l[0]=0;l[0]<shape[0];++l[0]){
+ for(l[1]=0;l[1]<shape[1];++l[1]){
+ OPENGM_TEST(lfunc(l)==0);
+ }
+ }
+ std::cout << "OK" << std::endl;
+ }
+
+ void testLPotts(){
+ std::cout << " * LearnablePotts ..." << std::flush;
+ // parameter
+ const size_t numparam = 1;
+ opengm::Parameters<ValueType,IndexType> param(numparam);
+ param.setParameter(0,5.0);
+
+ LabelType numL = 3;
+ std::vector<size_t> pIds(1,0);
+ std::vector<ValueType> feat(1,1);
+ // function
+ opengm::functions::learnable::LPotts<ValueType,IndexType,LabelType> lfunc(param,numL,pIds,feat);
+
+ LabelType l[] ={0,0};
+ for(l[0]=0;l[0]<numL;++l[0]){
+ for(l[1]=0;l[1]<numL;++l[1]){
+ if(l[0]==l[1]){
+ OPENGM_TEST_EQUAL_TOLERANCE(lfunc(l),0, 0.0001);
+ }else{
+ OPENGM_TEST_EQUAL_TOLERANCE(lfunc(l),5.0, 0.0001);
+ }
+ }
+ }
+ std::cout << "OK" << std::endl;
}
};
@@ -16,8 +64,9 @@ struct LearnableFunctionsTest {
int main() {
std::cout << "Learnable Functions test... " << std::endl;
{
- LearnableFunctionsTest<int >t;
- t.run();
+ LearnableFunctionsTest<double>t;
+ t.testLearnableFeatureFunction();
+ t.testLPotts();
}
std::cout << "done.." << std::endl;
return 0;
--
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