/usr/include/shark/Models/AbstractModel.h is in libshark-dev 3.1.3+ds1-2.
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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | //===========================================================================
/*!
*
*
* \brief base class for all models, as well as a specialized differentiable model
*
*
*
* \author T.Glasmachers, O. Krause
* \date 2010
*
*
* \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_MODELS_ABSTRACTMODEL_H
#define SHARK_MODELS_ABSTRACTMODEL_H
#include <shark/Core/Flags.h>
#include <shark/Core/IParameterizable.h>
#include <shark/Core/INameable.h>
#include <shark/Core/State.h>
#include <shark/Rng/Normal.h>
#include<shark/Data/Dataset.h>
namespace shark {
///\brief Base class for all Models
///
/// \par
/// A model is one of the three fundaments of supervised learning: model, error measure
/// and an optimization algorithm.
/// It is a concept of a function which performs a mapping \f$ x \rightarrow f_w(x)\f$.
/// In contrast to an error function it has two sets of parameters:
/// The first is the current point to map \f$x\f$, the others are the internal model parameters \f$w\f$
/// which define the mapping.
/// Often a model is used to find an optimal mapping for a problem, for example a function which
/// best fits the points of a given dataset. Therefore, AbstractModel does not only offer
/// the mapping itself, but also a set of special derivatives with respect to \f$ x \f$ and \f$ w \f$.
/// Most of the time, only the derivative with respect to \f$ w \f$ is needed, but in some special problems,
/// like finding optimal stimuli or stacking models, also the input derivative is needed.
///
///\par Models are optimized for batch processing. This means, that instead of only one data point at a time, it can
/// evaluate a big set of inputs at the same time, using optimized routines for this task.
///
/// \par
/// The derivatives are weighted, which means that the derivatives of every single output are added together
/// weighted by coefficients (see #weightedParameterDerivative). This is an optimization for the chain rule
/// which is very efficient to calculate most of the time.
///
/// \par
/// It is allowed to store intermediate values during #eval and use them to speed up calculation of
/// derivatives. Therefore it must be guaranteed that eval() is called before calculating derivatives.
/// This is no restriction, since typical error measures need the mapping itself and not only the derivative.
///
/// \par
/// Models have names and can be serialised
template<class InputTypeT, class OutputTypeT>
class AbstractModel : public IParameterizable, public INameable, public ISerializable
{
public:
/// \brief Defines the input type of the model.
typedef InputTypeT InputType;
/// \brief Defines the output type of the model.
typedef OutputTypeT OutputType;
typedef OutputType result_type;
/// \brief defines the batch type of the input type.
///
/// This could for example be std::vector<InputType> but for example for RealVector it could be RealMatrix
typedef typename Batch<InputType>::type BatchInputType;
/// \brief defines the batch type of the output type
typedef typename Batch<OutputType>::type BatchOutputType;
AbstractModel() { }
virtual ~AbstractModel() { }
enum Feature {
HAS_FIRST_PARAMETER_DERIVATIVE = 1,
HAS_SECOND_PARAMETER_DERIVATIVE = 2,
HAS_FIRST_INPUT_DERIVATIVE = 4,
HAS_SECOND_INPUT_DERIVATIVE = 8,
IS_SEQUENTIAL = 16
};
SHARK_FEATURE_INTERFACE;
/// \brief Returns true when the first parameter derivative is implemented.
bool hasFirstParameterDerivative()const{
return m_features & HAS_FIRST_PARAMETER_DERIVATIVE;
}
/// \brief Returns true when the second parameter derivative is implemented.
bool hasSecondParameterDerivative()const{
return m_features & HAS_SECOND_PARAMETER_DERIVATIVE;
}
/// \brief Returns true when the first input derivative is implemented.
bool hasFirstInputDerivative()const{
return m_features & HAS_FIRST_INPUT_DERIVATIVE;
}
/// \brief Returns true when the second parameter derivative is implemented.
bool hasSecondInputDerivative()const{
return m_features & HAS_SECOND_INPUT_DERIVATIVE;
}
bool isSequential()const{
return m_features & IS_SEQUENTIAL;
}
///\brief Creates an internal state of the model.
///
///The state is needed when the derivatives are to be
///calculated. Eval can store a state which is then reused to speed up
///the calculations of the derivatives. This also allows eval to be
///evaluated in parallel!
virtual boost::shared_ptr<State> createState() const
{
if (hasFirstParameterDerivative()
|| hasFirstInputDerivative()
|| hasSecondParameterDerivative()
|| hasSecondInputDerivative())
{
throw SHARKEXCEPTION("[AbstractModel::createState] createState must be overridden by models with derivatives");
}
return boost::shared_ptr<State>(new EmptyState());
}
/// \brief From ISerializable, reads a model from an archive.
virtual void read( InArchive & archive ){
m_features.read(archive);
RealVector p;
archive & p;
setParameterVector(p);
}
/// \brief writes a model to an archive
///
/// the default implementation just saves the parameters, not the structure!
virtual void write( OutArchive & archive ) const{
m_features.write(archive);
RealVector p = parameterVector();
archive & p;
}
/// \brief Standard interface for evaluating the response of the model to a batch of patterns.
///
/// \param patterns the inputs of the model
/// \param outputs the predictions or response of the model to every pattern
virtual void eval(BatchInputType const & patterns, BatchOutputType& outputs) const{
boost::shared_ptr<State> state = createState();
eval(patterns,outputs,*state);
}
/// \brief Standard interface for evaluating the response of the model to a batch of patterns.
///
/// \param patterns the inputs of the model
/// \param outputs the predictions or response of the model to every pattern
/// \param state intermediate results stored by eval which can be reused for derivative computation.
virtual void eval(BatchInputType const & patterns, BatchOutputType& outputs, State& state) const = 0;
/// \brief Standard interface for evaluating the response of the model to a single pattern.
///
/// \param pattern the input of the model
/// \param output the prediction or response of the model to the pattern
virtual void eval(InputType const & pattern, OutputType& output)const{
BatchInputType patternBatch=Batch<InputType>::createBatch(pattern);
get(patternBatch,0) = pattern;
BatchOutputType outputBatch;
eval(patternBatch,outputBatch);
output = get(outputBatch,0);
}
/// \brief Model evaluation as an operator for a whole dataset. This is a convenience function
///
/// \param patterns the input of the model
/// \returns the responses of the model
Data<OutputType> operator()(Data<InputType> const& patterns)const{
int batches = (int) patterns.numberOfBatches();
Data<OutputType> result(batches);
SHARK_PARALLEL_FOR(int i = 0; i < batches; ++i)
result.batch(i)= (*this)(patterns.batch(i));
return result;
//return transform(patterns,*this);//todo this leads to compiler errors.
}
/// \brief Model evaluation as an operator for a single pattern. This is a convenience function
///
/// \param pattern the input of the model
/// \returns the response of the model
OutputType operator()(InputType const & pattern)const{
OutputType output;
eval(pattern,output);
return output;
}
/// \brief Model evaluation as an operator for a single pattern. This is a convenience function
///
/// \param patterns the input of the model
/// \returns the response of the model
BatchOutputType operator()(BatchInputType const & patterns)const{
BatchOutputType output;
eval(patterns,output);
return output;
}
/// \brief calculates the weighted sum of derivatives w.r.t the parameters.
///
/// \param pattern the patterns to evaluate
/// \param coefficients the coefficients which are used to calculate the weighted sum for every pattern
/// \param state intermediate results stored by eval to speed up calculations of the derivatives
/// \param derivative the calculated derivative as sum over all derivates of all patterns
virtual void weightedParameterDerivative(
BatchInputType const & pattern,
BatchOutputType const & coefficients,
State const& state,
RealVector& derivative
)const{
SHARK_FEATURE_EXCEPTION(HAS_FIRST_PARAMETER_DERIVATIVE);
}
/// \brief calculates the weighted sum of derivatives w.r.t the parameters
///
/// \param pattern the patterns to evaluate
/// \param coefficients the coefficients which are used to calculate the weighted sum for every pattern
/// \param errorHessian the second derivative of the error function for every pattern
/// \param state intermediate results stored by eval to speed up calculations of the derivatives
/// \param derivative the calculated derivative as sum over all derivates of all patterns
/// \param hessian the calculated hessian as sum over all derivates of all patterns
virtual void weightedParameterDerivative(
BatchInputType const & pattern,
BatchOutputType const & coefficients,
Batch<RealMatrix>::type const & errorHessian,//maybe a batch of matrices is bad?,
State const& state,
RealVector& derivative,
RealMatrix& hessian
)const{
SHARK_FEATURE_EXCEPTION(HAS_SECOND_PARAMETER_DERIVATIVE);
}
///\brief calculates the weighted sum of derivatives w.r.t the inputs
///
/// \param pattern the patterns to evaluate
/// \param coefficients the coefficients which are used to calculate the weighted sum for every pattern
/// \param state intermediate results stored by eval to sped up calculations of the derivatives
/// \param derivative the calculated derivative for every pattern
virtual void weightedInputDerivative(
BatchInputType const & pattern,
BatchOutputType const & coefficients,
State const& state,
BatchInputType& derivative
)const{
SHARK_FEATURE_EXCEPTION(HAS_FIRST_INPUT_DERIVATIVE);
}
///\brief calculates the weighted sum of derivatives w.r.t the inputs
///
/// \param pattern the pattern to evaluate
/// \param coefficients the coefficients which are used to calculate the weighted sum
/// \param errorHessian the second derivative of the error function for every pattern
/// \param state intermediate results stored by eval to sped up calculations of the derivatives
/// \param derivative the calculated derivative for every pattern
/// \param hessian the calculated hessian for every pattern
virtual void weightedInputDerivative(
BatchInputType const & pattern,
BatchOutputType const & coefficients,
typename Batch<RealMatrix>::type const & errorHessian,
State const& state,
RealMatrix& derivative,
Batch<RealMatrix>::type& hessian
)const{
SHARK_FEATURE_EXCEPTION(HAS_SECOND_INPUT_DERIVATIVE);
}
///\brief calculates weighted input and parameter derivative at the same time
///
/// Sometimes, both derivatives are needed at the same time. But sometimes, when calculating the
/// weighted parameter derivative, the input derivative can be calculated for free. This is for example true for
/// the feed-forward neural networks. However, there exists the obvious default implementation to just calculate
/// the derivatives one after another.
/// \param patterns the patterns to evaluate
/// \param coefficients the coefficients which are used to calculate the weighted sum
/// \param state intermediate results stored by eval to sped up calculations of the derivatives
/// \param parameterDerivative the calculated parameter derivative as sum over all derivates of all patterns
/// \param inputDerivative the calculated derivative for every pattern
virtual void weightedDerivatives(
BatchInputType const & patterns,
BatchOutputType const & coefficients,
State const& state,
RealVector& parameterDerivative,
BatchInputType& inputDerivative
)const{
weightedParameterDerivative(patterns,coefficients,state,parameterDerivative);
weightedInputDerivative(patterns,coefficients,state,inputDerivative);
}
};
/**
* \ingroup shark_globals
*
* @{
*/
/// \brief Initialize model parameters normally distributed.
///
/// \param model: model to be initialized
/// \param s: variance of mean-free normal distribution
template <class InputType, class OutputType>
void initRandomNormal(AbstractModel<InputType, OutputType>& model, double s)
{
Normal<> gauss(Rng::globalRng,0, s);
RealVector weights(model.numberOfParameters());
std::generate(weights.begin(), weights.end(), gauss);
model.setParameterVector(weights);
}
/// \brief Initialize model parameters uniformly at random.
///
/// \param model: model to be initialized
/// \param l: lower bound of initialization interval
/// \param h: upper bound of initialization interval
template <class InputType, class OutputType>
void initRandomUniform(AbstractModel<InputType, OutputType>& model, double l, double h)
{
Uniform<> uni(Rng::globalRng,l, h);
RealVector weights(model.numberOfParameters());
std::generate(weights.begin(), weights.end(), uni);
model.setParameterVector(weights);
}
/** @}*/
}
#endif
|