[opengm] 231/386: added weight constraints and weight regularizer to dataset

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Aug 31 08:37:55 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 e78a33bd4d6f6e08a11ed1e51e72ad704ca1fc8c
Author: DerThorsten <thorsten.beier at iwr.uni-heidelberg.de>
Date:   Tue Jan 13 18:54:30 2015 +0100

    added weight constraints and weight regularizer to dataset
---
 include/opengm/graphicalmodel/weights.hxx          |  21 +++-
 include/opengm/learning/dataset/dataset.hxx        |  39 ++++++-
 .../opengm/learning/dataset/editabledataset.hxx    | 123 ++++++++++++++-------
 3 files changed, 136 insertions(+), 47 deletions(-)

diff --git a/include/opengm/graphicalmodel/weights.hxx b/include/opengm/graphicalmodel/weights.hxx
index 3f5c209..4438237 100644
--- a/include/opengm/graphicalmodel/weights.hxx
+++ b/include/opengm/graphicalmodel/weights.hxx
@@ -85,7 +85,7 @@ namespace learning{
 
 
     template<class T>
-    class WeightLoss{
+    class WeightRegularizer{
     public:
         enum RegularizationType{
             NoRegularizer=-1,
@@ -93,14 +93,23 @@ namespace learning{
             L2Regularizer=2
         };
 
-        WeightLoss(const int regularizationNorm=2, const double lambda=1.0)
+        WeightRegularizer(const int regularizationNorm, const double lambda=1.0)
         :   regularizationType_(),
             lambda_(lambda){
-                if(regularizationNorm==-1){
-                    regularizationType_ = NoRegularizer;
-                }
+            if(regularizationNorm==-1){
+                regularizationType_ = NoRegularizer;
+            }
+            else if(regularizationNorm==1){
+                regularizationType_ = L1Regularizer;
+            }
+            else if(regularizationNorm==2){
+                regularizationType_ = L2Regularizer;
+            }
+            else{
+                throw opengm::RuntimeError("regularizationNorm must be -1 (NONE), 1 (L1) or 2 (L2)");
+            }
         }
-        WeightLoss(const RegularizationType regularizationType=L2Regularizer, const double lambda=1.0)
+        WeightRegularizer(const RegularizationType regularizationType=L2Regularizer, const double lambda=1.0)
         :   regularizationType_(regularizationType),
             lambda_(lambda){
 
diff --git a/include/opengm/learning/dataset/dataset.hxx b/include/opengm/learning/dataset/dataset.hxx
index accf927..142a6fe 100644
--- a/include/opengm/learning/dataset/dataset.hxx
+++ b/include/opengm/learning/dataset/dataset.hxx
@@ -55,7 +55,12 @@ namespace opengm {
         typedef typename GM::ValueType   ValueType;
         typedef typename GM::IndexType   IndexType;
         typedef typename GM::LabelType   LabelType;
+
+
         typedef opengm::learning::Weights<ValueType> Weights;
+        typedef opengm::learning::WeightConstraints<ValueType> WeightConstraintsType;
+        typedef opengm::learning::WeightRegularizer<ValueType> WeightRegularizerType;
+
 
         bool                          lockModel(const size_t i)               { ++count_[i]; }
         bool                          unlockModel(const size_t i)             { OPENGM_ASSERT(count_[i]>0); --count_[i]; }
@@ -77,7 +82,11 @@ namespace opengm {
         ValueType                     getLoss(const typename INF::Parameter& para, const size_t i) const;
         ValueType                     getLoss(std::vector<LabelType> conf , const size_t i) const;
 
-        Dataset(size_t numInstances=0);
+        Dataset(size_t numInstances);
+
+        Dataset(const Weights & weights = Weights(),const WeightConstraintsType & weightConstraints = WeightConstraintsType(),
+                const WeightRegularizerType & weightRegularizer = WeightRegularizerType(),size_t numInstances=0);
+
         //void loadAll(std::string path,std::string prefix); 
 
         friend class DatasetSerialization;
@@ -94,6 +103,9 @@ namespace opengm {
         std::vector<LossParameterType> lossParams_;
         std::vector<std::vector<LabelType> > gts_;
         Weights weights_;
+        WeightConstraintsType weightConstraints_;
+        WeightRegularizerType weightRegularizer_;
+
 
         void buildModelWithLoss(size_t i);
     };
@@ -105,13 +117,34 @@ namespace opengm {
         isCached_(std::vector<bool>(numInstances)),
         gms_(std::vector<GM>(numInstances)),
         gmsWithLoss_(std::vector<GMWITHLOSS>(numInstances)),
+        lossParams_(std::vector<LossParameterType>(numInstances)),
         gts_(std::vector<std::vector<LabelType> >(numInstances)),
-        weights_(Weights(0)),
-        lossParams_(std::vector<LossParameterType>(numInstances))
+        weights_(0),
+        weightConstraints_(),
+        weightRegularizer_()
     {
     }
 
     template<class GM, class LOSS, class LOSS_GM>
+    Dataset<GM, LOSS, LOSS_GM>::Dataset(
+        const Weights & weights, 
+        const WeightConstraintsType & weightConstraints,
+        const WeightRegularizerType & weightRegularizer,
+        size_t numInstances
+    ):  count_(std::vector<size_t>(numInstances)),
+        isCached_(std::vector<bool>(numInstances)),
+        gms_(std::vector<GM>(numInstances)),
+        gmsWithLoss_(std::vector<GMWITHLOSS>(numInstances)),
+        lossParams_(std::vector<LossParameterType>(numInstances)),
+        gts_(std::vector<std::vector<LabelType> >(numInstances)),
+        weights_(weights),
+        weightConstraints_(weightConstraints),
+        weightRegularizer_(weightRegularizer)
+    {
+    }
+
+
+    template<class GM, class LOSS, class LOSS_GM>
     template<class INF>
     typename GM::ValueType Dataset<GM, LOSS, LOSS_GM>::getTotalLoss(const typename INF::Parameter& para) const {
         ValueType sum=0;
diff --git a/include/opengm/learning/dataset/editabledataset.hxx b/include/opengm/learning/dataset/editabledataset.hxx
index 66f9e10..178e0e0 100644
--- a/include/opengm/learning/dataset/editabledataset.hxx
+++ b/include/opengm/learning/dataset/editabledataset.hxx
@@ -10,7 +10,7 @@
 #include "../loss/noloss.hxx"
 
 namespace opengm {
-   namespace datasets{
+namespace datasets{
 
     // template< typename Weights >
     // struct LinkWeights{
@@ -25,42 +25,69 @@ namespace opengm {
     //     }
     // };
 
-      template<class GM, class LOSS, class LOSS_GM = DefaultLossGm<GM> >
-      class EditableDataset : public Dataset<GM, LOSS, LOSS_GM>{
-      public:
-         typedef GM                     GMType;
-         typedef typename Dataset<GM, LOSS, LOSS_GM>::GMWITHLOSS   GMWITHLOSS;
-         typedef LOSS                   LossType;
-         typedef typename LOSS::Parameter LossParameterType;
-         typedef typename GM::ValueType ValueType;
-         typedef typename GM::IndexType IndexType;
-         typedef typename GM::LabelType LabelType;
-         typedef opengm::learning::Weights<ValueType> Weights;
-         typedef std::vector<LabelType> GTVector;
-
-         EditableDataset(size_t numInstances=0) : Dataset<GM, LOSS,LOSS_GM>(numInstances) {}
-         EditableDataset(std::vector<GM>& gms, std::vector<GTVector >& gts, std::vector<LossParameterType>& lossParams);
-
-         void setInstance(const size_t i, const GM& gm, const GTVector& gt, const LossParameterType& p=LossParameterType());
-         void setGT(const size_t i, const GTVector& gt);
-         void pushBackInstance(const GM& gm, const GTVector& gt, const LossParameterType& p=LossParameterType());
-         void setWeights(Weights& w);
-      };
+    template<class GM, class LOSS, class LOSS_GM = DefaultLossGm<GM> >
+    class EditableDataset : public Dataset<GM, LOSS, LOSS_GM>{
+    public:
+        typedef GM                     GMType;
+        typedef typename Dataset<GM, LOSS, LOSS_GM>::GMWITHLOSS   GMWITHLOSS;
+        typedef LOSS                   LossType;
+        typedef typename LOSS::Parameter LossParameterType;
+        typedef typename GM::ValueType ValueType;
+        typedef typename GM::IndexType IndexType;
+        typedef typename GM::LabelType LabelType;
+
+        typedef opengm::learning::Weights<ValueType> Weights;
+        typedef opengm::learning::WeightConstraints<ValueType> WeightConstraintsType;
+        typedef opengm::learning::WeightRegularizer<ValueType> WeightRegularizerType;
+
+        typedef std::vector<LabelType> GTVector;
+
+        EditableDataset(size_t numInstances) : Dataset<GM, LOSS,LOSS_GM>(numInstances) {}
+        EditableDataset(std::vector<GM>& gms, std::vector<GTVector >& gts, std::vector<LossParameterType>& lossParams);
+
+        EditableDataset(const Weights & weights = Weights(),const WeightConstraintsType & weightConstraints = WeightConstraintsType(),
+                        const WeightRegularizerType & weightRegularizer = WeightRegularizerType(),size_t numInstances=0)
+        :   Dataset<GM, LOSS, LOSS_GM>(weights, weightConstraints, weightRegularizer, numInstances){
+
+        }
+
+
+        void setInstance(const size_t i, const GM& gm, const GTVector& gt, const LossParameterType& p=LossParameterType());
+        void setGT(const size_t i, const GTVector& gt);
+        void pushBackInstance(const GM& gm, const GTVector& gt, const LossParameterType& p=LossParameterType());
+        void setWeights(Weights& w);
+
+
+        void setWeightConstraints(const WeightConstraintsType & weightConstraints);
+
+        void setWeightRegularizer(const WeightRegularizerType & weightRegularizer);
+    };
 
     template<class GM, class LOSS, class LOSS_GM>
-    EditableDataset<GM, LOSS, LOSS_GM>::EditableDataset(std::vector<GM>& gms,
-                                               std::vector<GTVector >& gts,
-                                               std::vector<LossParameterType>& lossParams)
-        : Dataset<GM, LOSS, LOSS_GM>(gms.size())
+    EditableDataset<GM, LOSS, LOSS_GM>::EditableDataset(
+        std::vector<GM>& gms,
+        std::vector<GTVector >& gts,
+        std::vector<LossParameterType>& lossParams
+    )
+    :   Dataset<GM, LOSS, LOSS_GM>(gms.size())
     {
         for(size_t i=0; i<gms.size(); ++i){
-            setInstance(i, gms[i], gts[i], lossParams[i]);
-            this->buildModelWithLoss(i);
-        }
+        setInstance(i, gms[i], gts[i], lossParams[i]);
+        this->buildModelWithLoss(i);
     }
+    }
+
+
+
+
 
     template<class GM, class LOSS, class LOSS_GM>
-    void EditableDataset<GM, LOSS, LOSS_GM>::setInstance(const size_t i, const GM& gm, const GTVector& gt, const LossParameterType& p) {
+    void EditableDataset<GM, LOSS, LOSS_GM>::setInstance(
+        const size_t i, 
+        const GM& gm, 
+        const GTVector& gt,
+        const LossParameterType& p
+    ) {
         OPENGM_CHECK_OP(i, <, this->gms_.size(),"");
         OPENGM_CHECK_OP(i, <, this->gts_.size(),"");
         OPENGM_CHECK_OP(i, <, this->lossParams_.size(),"");
@@ -74,14 +101,21 @@ namespace opengm {
     }
 
     template<class GM, class LOSS, class LOSS_GM>
-    void EditableDataset<GM, LOSS, LOSS_GM>::setGT(const size_t i, const GTVector& gt) {
+    inline void EditableDataset<GM, LOSS, LOSS_GM>::setGT(
+        const size_t i, 
+        const GTVector& gt
+    ) {
         OPENGM_CHECK_OP(i, <, this->gts_.size(),"");
         this->gts_[i] = gt;
         this->buildModelWithLoss(i);
     }
 
     template<class GM, class LOSS, class LOSS_GM>
-    void EditableDataset<GM, LOSS, LOSS_GM>::pushBackInstance(const GM& gm, const GTVector& gt, const LossParameterType& p) {
+    void EditableDataset<GM, LOSS, LOSS_GM>::pushBackInstance(
+        const GM& gm, 
+        const GTVector& gt, 
+        const LossParameterType& p
+    ) {
         this->gms_.push_back(gm);
         this->gts_.push_back(gt);
         this->lossParams_.push_back(p);
@@ -95,15 +129,28 @@ namespace opengm {
     }
 
     template<class GM, class LOSS, class LOSS_GM>
-    void EditableDataset<GM, LOSS, LOSS_GM>::setWeights(Weights& w) {
+    inline void EditableDataset<GM, LOSS, LOSS_GM>::setWeights(
+        Weights& w
+    ) {
         this->weights_ = w;
-        // LinkWeights<Weights> LinkFunctor(w);
-        // for(size_t i=0; i<this->gms_.size(); ++i){
-        //     (this->gms_[i])[0].callFunctor(LinkFunctor);
-        // }
     }
 
-   } // namespace datasets
+    template<class GM, class LOSS, class LOSS_GM>
+    inline void EditableDataset<GM, LOSS, LOSS_GM>::setWeightConstraints(
+        const WeightConstraintsType & weightConstraints
+    ){
+        this->weightConstraints_ = weightConstraints;
+    }
+
+    template<class GM, class LOSS, class LOSS_GM>
+    inline void EditableDataset<GM, LOSS, LOSS_GM>::setWeightRegularizer(
+        const WeightRegularizerType & weightRegularizer
+    ){
+        this->weightRegularizer_ = weightRegularizer;
+    }
+
+
+} // namespace datasets
 } // namespace opengm
 
 #endif 

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