OpenGM  2.3.x
Discrete Graphical Model Library
weights.hxx
Go to the documentation of this file.
1 #ifndef OPENGM_LEARNING_WEIGHTS
2 #define OPENGM_LEARNING_WEIGHTS
3 
4 #include <opengm/opengm.hxx>
5 
6 namespace opengm{
7 namespace learning{
8 
9  /*
10  template<class T>
11  class Weights {
12  public:
13 
14  typedef T ValueType;
15 
16  Weights(const size_t numberOfWeights=0)
17  : weights_(numberOfWeights)
18  {
19 
20  }
21 
22  ValueType getWeight(const size_t pi)const{
23  OPENGM_ASSERT_OP(pi,<,weights_.size());
24  return weights_[pi];
25  }
26 
27  void setWeight(const size_t pi,const ValueType value){
28  OPENGM_ASSERT_OP(pi,<,weights_.size());
29  weights_[pi] = value;
30  }
31 
32  const ValueType& operator[](const size_t pi)const{
33  return weights_[pi];
34  }
35 
36  ValueType& operator[](const size_t pi) {
37  return weights_[pi];
38  }
39 
40  size_t numberOfWeights()const{
41  return weights_.size();
42  }
43 
44  size_t size()const{
45  return weights_.size();
46  }
47 
48  private:
49 
50  std::vector<ValueType> weights_;
51  };
52  */
53  template<class T>
54  class Weights : public marray::Vector<T>
55  {
56  public:
57 
58  typedef T ValueType;
59 
60  Weights(const size_t numberOfWeights=0)
62  {
63 
64  }
65 
66  ValueType getWeight(const size_t pi)const{
67  OPENGM_ASSERT_OP(pi,<,this->size());
68  return (*this)[pi];
69  }
70 
71  void setWeight(const size_t pi,const ValueType value){
72  OPENGM_ASSERT_OP(pi,<,this->size());
73  (*this)[pi] = value;
74  }
75 
76 
77  size_t numberOfWeights()const{
78  return this->size();
79  }
80 
81  private:
82 
83  //std::vector<ValueType> weights_;
84  };
85 
86 
87  template<class T>
89  public:
91  NoRegularizer=-1,
92  L1Regularizer=1,
93  L2Regularizer=2
94  };
95 
96  WeightRegularizer(const int regularizationNorm, const double lambda=1.0)
97  : regularizationType_(),
98  lambda_(lambda){
99  if(regularizationNorm==-1){
100  regularizationType_ = NoRegularizer;
101  }
102  else if(regularizationNorm==1){
103  regularizationType_ = L1Regularizer;
104  }
105  else if(regularizationNorm==2){
106  regularizationType_ = L2Regularizer;
107  }
108  else{
109  throw opengm::RuntimeError("regularizationNorm must be -1 (NONE), 1 (L1) or 2 (L2)");
110  }
111  }
112  WeightRegularizer(const RegularizationType regularizationType=L2Regularizer, const double lambda=1.0)
113  : regularizationType_(regularizationType),
114  lambda_(lambda){
115 
116  }
117 
118  double lambda()const{
119  return lambda_;
120  }
121 
123  return regularizationType_;
124  }
125 
126  int regularizerNorm()const{
127  return static_cast<int>(regularizationType_);
128  }
129 
130  double evaluate(const Weights<T> & weights){
131  if(regularizationType_== NoRegularizer){
132  return 0.0;
133  }
134  else if(regularizationType_ == L1Regularizer){
135  double val = 0.0;
136  for(size_t wi=0; wi<weights.size(); ++wi){
137  val += std::abs(weights[wi]);
138  }
139  return val*lambda_;
140  }
141  else { //if(regularizationType_ == L2Regularizer){
142  double val = 0.0;
143  for(size_t wi=0; wi<weights.size(); ++wi){
144  val += std::pow(weights[wi], 2);
145  }
146  return val*lambda_;
147  }
148  }
149 
150  private:
151  RegularizationType regularizationType_;
152  double lambda_;
153  };
154 
155 
156  template<class T>
158  public:
159 
160  WeightConstraints(const size_t nWeights = 0)
161  : wLowerBounds_(nWeights,-1.0*std::numeric_limits<T>::infinity()),
162  wUpperBounds_(nWeights, 1.0*std::numeric_limits<T>::infinity()),
163  cLowerBounds_(),
164  cUpperBounds_(),
165  cOffset_(0),
166  cStart_(),
167  cSize_(),
168  cIndices_(),
169  cCoeff_(){
170 
171  }
172  template<class ITER_LB, class ITER_UB>
173  WeightConstraints(ITER_LB lbBegin, ITER_LB lbEnd, ITER_UB ubBegin)
174  : wLowerBounds_(lbBegin,lbEnd),
175  wUpperBounds_(ubBegin, ubBegin + std::distance(lbBegin, lbEnd)),
176  cLowerBounds_(),
177  cUpperBounds_(),
178  cOffset_(0),
179  cStart_(),
180  cSize_(),
181  cIndices_(),
182  cCoeff_()
183  {
184 
185  }
186  // query
187  size_t numberOfConstraints()const{
188  return cStart_.size();
189  }
190 
191  T weightLowerBound(const size_t wi)const{
192  return wLowerBounds_[wi];
193  }
194  T weightUpperBound(const size_t wi)const{
195  return wUpperBounds_[wi];
196  }
197 
198  const std::vector<T> & weightLowerBounds()const{
199  return wLowerBounds_;
200  }
201  const std::vector<T> & weightUpperBounds()const{
202  return wUpperBounds_;
203  }
204 
205 
206  size_t constraintSize(const size_t ci)const{
207  return cSize_[ci];
208  }
209  T constraintLowerBound(const size_t ci)const{
210  return cLowerBounds_[ci];
211  }
212  T constraintUpperBound(const size_t ci)const{
213  return cUpperBounds_[ci];
214  }
215 
216  const std::vector<size_t> & constraintSizes()const{
217  return cLowerBounds_;
218  }
219  const std::vector<T> & constraintLowerBounds()const{
220  return cLowerBounds_;
221  }
222  const std::vector<T> & constraintUpperBounds()const{
223  return cUpperBounds_;
224  }
225 
226  // modification
227  template<class ITER_LB>
228  void setLowerBounds(ITER_LB lbBegin, ITER_LB lbEnd){
229  wLowerBounds_.assign(lbBegin, lbEnd);
230  }
231 
232  template<class ITER_UB>
233  void setUpperBounds(ITER_UB ubBegin, ITER_UB ubEnd){
234  wUpperBounds_.assign(ubBegin, ubEnd);
235  }
236 
237  template<class ITER_INDICES, class ITER_COEFF>
238  void addConstraint(ITER_INDICES indicesBegin, ITER_INDICES indicesEnd, ITER_COEFF coeffBegin, const T lowerBound, const T upperBound){
239  // length of this constraint
240  const size_t cSize = std::distance(indicesBegin, indicesEnd);
241  // store length of constraint
242  cSize_.push_back(cSize);
243 
244  // store offset / index in 'cIndices_' and 'cCoeff_'
245  cStart_.push_back(cOffset_);
246 
247  // increment the cOffset_ for the next constraint which
248  // could be added by the user
249  cOffset_ +=cSize;
250 
251  // copy indices and coefficients
252  for( ;indicesBegin!=indicesEnd; ++indicesBegin,++coeffBegin){
253  cIndices_.push_back(*indicesBegin);
254  cCoeff_.push_back(*coeffBegin);
255  }
256  }
257 
258  private:
259  // w upper-lower bound
260  std::vector<T> wLowerBounds_;
261  std::vector<T> wUpperBounds_;
262  // constraints
263  std::vector<T> cLowerBounds_;
264  std::vector<T> cUpperBounds_;
265 
266  size_t cOffset_;
267  std::vector<size_t> cStart_;
268  std::vector<size_t> cSize_;
269  std::vector<size_t> cIndices_;
270  std::vector<T> cCoeff_;
271  };
272 
273 
274 } // namespace learning
275 } // namespace opengm
276 
277 
278 
279 
280 
281 
282 #endif /* OPENGM_LEARNING_WEIGHTS */
T constraintLowerBound(const size_t ci) const
Definition: weights.hxx:209
The OpenGM namespace.
Definition: config.hxx:43
size_t constraintSize(const size_t ci) const
Definition: weights.hxx:206
Runtime-flexible multi-dimensional views and arrays.
Definition: marray.hxx:20
WeightConstraints(const size_t nWeights=0)
Definition: weights.hxx:160
void addConstraint(ITER_INDICES indicesBegin, ITER_INDICES indicesEnd, ITER_COEFF coeffBegin, const T lowerBound, const T upperBound)
Definition: weights.hxx:238
WeightConstraints(ITER_LB lbBegin, ITER_LB lbEnd, ITER_UB ubBegin)
Definition: weights.hxx:173
const std::vector< T > & constraintLowerBounds() const
Definition: weights.hxx:219
T weightUpperBound(const size_t wi) const
Definition: weights.hxx:194
Weights(const size_t numberOfWeights=0)
Definition: weights.hxx:60
STL namespace.
void setUpperBounds(ITER_UB ubBegin, ITER_UB ubEnd)
Definition: weights.hxx:233
RegularizationType regularizationType() const
Definition: weights.hxx:122
WeightRegularizer(const int regularizationNorm, const double lambda=1.0)
Definition: weights.hxx:96
const std::vector< size_t > & constraintSizes() const
Definition: weights.hxx:216
One-dimensional Marray.
Definition: marray.hxx:50
double evaluate(const Weights< T > &weights)
Definition: weights.hxx:130
ValueType getWeight(const size_t pi) const
Definition: weights.hxx:66
const std::vector< T > & constraintUpperBounds() const
Definition: weights.hxx:222
T weightLowerBound(const size_t wi) const
Definition: weights.hxx:191
const std::vector< T > & weightLowerBounds() const
Definition: weights.hxx:198
void setWeight(const size_t pi, const ValueType value)
Definition: weights.hxx:71
#define OPENGM_ASSERT_OP(a, op, b)
runtime assertion
Definition: opengm.hxx:53
Vector(const allocator_type &=allocator_type())
Empty constructor.
Definition: marray.hxx:4637
WeightRegularizer(const RegularizationType regularizationType=L2Regularizer, const double lambda=1.0)
Definition: weights.hxx:112
void setLowerBounds(ITER_LB lbBegin, ITER_LB lbEnd)
Definition: weights.hxx:228
const std::vector< T > & weightUpperBounds() const
Definition: weights.hxx:201
size_t numberOfWeights() const
Definition: weights.hxx:77
OpenGM runtime error.
Definition: opengm.hxx:100
const size_t size() const
Get the number of data items.
Definition: marray.hxx:1698
T abs(const T &x)
Definition: opengm.hxx:111
T constraintUpperBound(const size_t ci) const
Definition: weights.hxx:212