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