/usr/include/shark/ObjectiveFunctions/Regularizer.h is in libshark-dev 3.0.1+ds1-2ubuntu1.
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 | //===========================================================================
/*!
*
*
* \brief Regularizer
*
*
*
* \author T. Glasmachers
* \date 2010-2011
*
*
* \par Copyright 1995-2015 Shark Development Team
*
* <BR><HR>
* This file is part of Shark.
* <http://image.diku.dk/shark/>
*
* Shark is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Shark is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Shark. If not, see <http://www.gnu.org/licenses/>.
*
*/
//===========================================================================
#ifndef SHARK_OBJECTIVEFUNCTIONS_REGULARIZER_H
#define SHARK_OBJECTIVEFUNCTIONS_REGULARIZER_H
#include <shark/ObjectiveFunctions/AbstractObjectiveFunction.h>
namespace shark {
///
/// \brief One-norm of the input as an objective function
///
/// \par
/// The OneNormRegularizer is intended to be used together with other
/// objective functions within a CombinedObjectiveFunction, in order to
/// obtain a more smooth and more sparse solution.
///
class OneNormRegularizer : public SingleObjectiveFunction
{
public:
/// Constructor
OneNormRegularizer(std::size_t numVariables = 0):m_numberOfVariables(numVariables)
{
m_features|=HAS_FIRST_DERIVATIVE;
m_features|=HAS_SECOND_DERIVATIVE;
}
/// \brief From INameable: return the class name.
std::string name() const
{ return "OneNormRegularizer"; }
std::size_t numberOfVariables()const{
return m_numberOfVariables;
}
bool hasScalableDimensionality()const{
return true;
}
void setNumberOfVariables( std::size_t numberOfVariables ){
m_numberOfVariables = numberOfVariables;
}
void setMask(const RealVector& mask){
m_mask = mask;
}
const RealVector& mask()const{
return m_mask;
}
/// Evaluates the objective function.
double eval( RealVector const& input ) const{
if(m_mask.empty()){
return norm_1(input);
}
else
{
return norm_1(input * m_mask);
}
}
/// Evaluates the objective function
/// and calculates its gradient.
double evalDerivative( RealVector const& input, FirstOrderDerivative & derivative ) const {
std::size_t ic = input.size();
derivative.resize(ic);
if(m_mask.empty()){
for (std::size_t i = 0; i != ic; i++){
derivative(i) = boost::math::sign(input(i));
}
}
else
{
SIZE_CHECK(m_mask.size() == input.size());
for (std::size_t i=0; i != ic; i++){
derivative(i) = m_mask(i)*boost::math::sign(input(i));
}
}
return eval(input);
}
double evalDerivative( RealVector const& input, SecondOrderDerivative & derivative ) const {
std::size_t ic = input.size();
derivative.gradient.resize(ic);
derivative.hessian.resize(ic,ic);
derivative.hessian.clear();
if(m_mask.empty()){
for (std::size_t i=0; i != ic; i++){
derivative.gradient(i) = boost::math::sign(input(i));
}
}
else
{
SIZE_CHECK(m_mask.size() == input.size());
for (std::size_t i=0; i != ic; i++){
derivative.gradient(i) = m_mask(i)*boost::math::sign(input(i));
}
}
return eval(input);
}
private:
RealVector m_mask;
std::size_t m_numberOfVariables;
};
///
/// \brief Two-norm of the input as an objective function
///
/// \par
/// The TwoNormRegularizer is intended to be used together with other
/// objective functions within a CombinedObjectiveFunction, in order to
/// obtain a more smooth solution.
///
class TwoNormRegularizer : public AbstractObjectiveFunction<RealVector, double>
{
public:
typedef RealVector SearchPointType;
typedef double ResultType;
typedef AbstractObjectiveFunction<RealVector, double> super;
/// Constructor
TwoNormRegularizer(std::size_t numVariables = 0):m_numberOfVariables(numVariables)
{
m_features|=HAS_FIRST_DERIVATIVE;
m_features|=HAS_SECOND_DERIVATIVE;
}
/// \brief From INameable: return the class name.
std::string name() const
{ return "TwoNormRegularizer"; }
std::size_t numberOfVariables()const{
return m_numberOfVariables;
}
bool hasScalableDimensionality()const{
return true;
}
void setNumberOfVariables( std::size_t numberOfVariables ){
m_numberOfVariables = numberOfVariables;
}
void setMask(const RealVector& mask){
m_mask = mask;
}
const RealVector& mask()const{
return m_mask;
}
/// Evaluates the objective function.
virtual double eval( RealVector const& input ) const
{
if(m_mask.empty()){
return 0.5*norm_sqr(input);
}
else{
return 0.5 * sum(m_mask*sqr(input));
}
}
/// Evaluates the objective function
/// and calculates its gradient.
virtual double evalDerivative( RealVector const& input, FirstOrderDerivative & derivative ) const {
if(m_mask.empty()){
derivative = input;
}
else{
derivative = m_mask*input;
}
return eval(input);
}
/// Evaluates the objective function
/// and calculates its gradient and
/// its Hessian.
virtual ResultType evalDerivative( const SearchPointType & input, SecondOrderDerivative & derivative )const {
derivative.gradient = input;
derivative.hessian = RealIdentityMatrix(input.size());
return 0.5 * norm_sqr(input);
}
private:
std::size_t m_numberOfVariables;
RealVector m_mask;
};
}
#endif // SHARK_CORE_REGULARIZER_H
|