Linear Prediction with optional activation function. More...
#include <shark/Models/LinearModel.h>
Public Types | |
typedef blas::vector< typename InputType::value_type, typename InputType::device_type > | VectorType |
typedef blas::matrix< typename InputType::value_type, blas::row_major, typename InputType::device_type > | MatrixType |
typedef base_type::BatchInputType | BatchInputType |
typedef base_type::BatchOutputType | BatchOutputType |
typedef base_type::ParameterVectorType | ParameterVectorType |
Public Types inherited from shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType > | |
enum | Feature { HAS_FIRST_PARAMETER_DERIVATIVE = 1 , HAS_FIRST_INPUT_DERIVATIVE = 4 } |
typedef InputTypeT | InputType |
Defines the input type of the model. | |
typedef OutputTypeT | OutputType |
Defines the output type of the model. | |
typedef AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType > | ModelBaseType |
Defines the BaseType used by the model (this type). Useful for creating derived models. | |
typedef Batch< InputType >::type | BatchInputType |
defines the batch type of the input type. | |
typedef Batch< OutputType >::type | BatchOutputType |
defines the batch type of the output type | |
typedef TypedFlags< Feature > | Features |
typedef TypedFeatureNotAvailableException< Feature > | FeatureNotAvailableException |
Public Types inherited from shark::IParameterizable< VectorType > | |
typedef VectorType | ParameterVectorType |
Public Member Functions | |
LinearModel () | |
CDefault Constructor; use setStructure later. | |
LinearModel (Shape const &inputs, Shape const &outputs=1, bool offset=false) | |
Constructor creating a model with given dimensionalities and optional offset term. | |
std::string | name () const |
From INameable: return the class name. | |
LinearModel (MatrixType const &matrix, VectorType const &offset=VectorType()) | |
Construction from matrix (and vector) | |
bool | hasOffset () const |
check for the presence of an offset term | |
Shape | inputShape () const |
Returns the expected shape of the input. | |
Shape | outputShape () const |
Returns the shape of the output. | |
ParameterVectorType | parameterVector () const |
obtain the parameter vector | |
void | setParameterVector (ParameterVectorType const &newParameters) |
overwrite the parameter vector | |
size_t | numberOfParameters () const |
return the number of parameter | |
void | setStructure (Shape const &inputs, Shape const &outputs=1, bool offset=false) |
overwrite structure and parameters | |
void | setStructure (MatrixType const &matrix, VectorType const &offset=VectorType()) |
overwrite structure and parameters | |
MatrixType const & | matrix () const |
return a copy of the matrix in dense format | |
MatrixType & | matrix () |
VectorType const & | offset () const |
return the offset | |
VectorType & | offset () |
ActivationFunction const & | activationFunction () const |
Returns the activation function. | |
ActivationFunction & | activationFunction () |
Returns the activation function. | |
boost::shared_ptr< State > | createState () const |
Creates an internal state of the model. | |
void | eval (BatchInputType const &inputs, BatchOutputType &outputs) const |
Evaluate the model: output = matrix * input + offset. | |
void | eval (InputType const &input, VectorType &output) const |
void | eval (BatchInputType const &inputs, BatchOutputType &outputs, State &state) const |
Evaluate the model: output = matrix * input + offset. | |
void | weightedParameterDerivative (BatchInputType const &patterns, BatchOutputType const &outputs, BatchOutputType const &coefficients, State const &state, ParameterVectorType &gradient) const |
Calculates the first derivative w.r.t the parameters and summing them up over all patterns of the last computed batch. | |
void | weightedInputDerivative (BatchInputType const &patterns, BatchOutputType const &outputs, BatchOutputType const &coefficients, State const &state, MatrixType &derivative) const |
Calculates the first derivative w.r.t the inputs and summs them up over all patterns of the last computed batch. | |
void | weightedDerivatives (BatchInputType const &patterns, BatchOutputType const &outputs, BatchOutputType const &coefficients, State const &state, ParameterVectorType ¶meterDerivative, MatrixType &inputDerivative) const |
void | read (InArchive &archive) |
From ISerializable. | |
void | write (OutArchive &archive) const |
From ISerializable. | |
virtual void | eval (BatchInputType const &patterns, BatchOutputType &outputs) const |
Standard interface for evaluating the response of the model to a batch of patterns. | |
virtual void | eval (BatchInputType const &patterns, BatchOutputType &outputs, State &state) const=0 |
Standard interface for evaluating the response of the model to a batch of patterns. | |
virtual void | eval (InputType const &pattern, OutputType &output) const |
Standard interface for evaluating the response of the model to a single pattern. | |
Public Member Functions inherited from shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType > | |
AbstractModel () | |
virtual | ~AbstractModel () |
const Features & | features () const |
virtual void | updateFeatures () |
bool | hasFirstParameterDerivative () const |
Returns true when the first parameter derivative is implemented. | |
bool | hasFirstInputDerivative () const |
Returns true when the first input derivative is implemented. | |
Data< OutputType > | operator() (Data< InputType > const &patterns) const |
Model evaluation as an operator for a whole dataset. This is a convenience function. | |
OutputType | operator() (InputType const &pattern) const |
Model evaluation as an operator for a single pattern. This is a convenience function. | |
BatchOutputType | operator() (BatchInputType const &patterns) const |
Model evaluation as an operator for a single pattern. This is a convenience function. | |
virtual void | weightedParameterDerivative (BatchInputType const &pattern, BatchOutputType const &outputs, BatchOutputType const &coefficients, State const &state, ParameterVectorType &derivative) const |
calculates the weighted sum of derivatives w.r.t the parameters. | |
virtual void | weightedInputDerivative (BatchInputType const &pattern, BatchOutputType const &outputs, BatchOutputType const &coefficients, State const &state, BatchInputType &derivative) const |
calculates the weighted sum of derivatives w.r.t the inputs | |
virtual void | weightedDerivatives (BatchInputType const &patterns, BatchOutputType const &outputs, BatchOutputType const &coefficients, State const &state, ParameterVectorType ¶meterDerivative, BatchInputType &inputDerivative) const |
calculates weighted input and parameter derivative at the same time | |
Public Member Functions inherited from shark::IParameterizable< VectorType > | |
virtual | ~IParameterizable () |
virtual void | setParameterVector (ParameterVectorType const &newParameters) |
Set the parameter vector. | |
Public Member Functions inherited from shark::INameable | |
virtual | ~INameable () |
Public Member Functions inherited from shark::ISerializable | |
virtual | ~ISerializable () |
Virtual d'tor. | |
void | load (InArchive &archive, unsigned int version) |
Versioned loading of components, calls read(...). | |
void | save (OutArchive &archive, unsigned int version) const |
Versioned storing of components, calls write(...). | |
BOOST_SERIALIZATION_SPLIT_MEMBER () | |
Additional Inherited Members | |
Protected Attributes inherited from shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType > | |
Features | m_features |
Linear Prediction with optional activation function.
The class allows for dense and sparse input vector types. However it assumes that the weight matrix and the ouputs are dense. There are some cases where this is not good behavior. Check for example Normalizer for a class which is designed for sparse inputs and outputs.
Definition at line 59 of file LinearModel.h.
typedef base_type::BatchInputType shark::LinearModel< InputType, ActivationFunction >::BatchInputType |
Definition at line 76 of file LinearModel.h.
typedef base_type::BatchOutputType shark::LinearModel< InputType, ActivationFunction >::BatchOutputType |
Definition at line 77 of file LinearModel.h.
typedef blas::matrix<typename InputType::value_type, blas::row_major, typename InputType::device_type> shark::LinearModel< InputType, ActivationFunction >::MatrixType |
Definition at line 66 of file LinearModel.h.
typedef base_type::ParameterVectorType shark::LinearModel< InputType, ActivationFunction >::ParameterVectorType |
Definition at line 78 of file LinearModel.h.
typedef blas::vector<typename InputType::value_type, typename InputType::device_type> shark::LinearModel< InputType, ActivationFunction >::VectorType |
Definition at line 65 of file LinearModel.h.
|
inline |
CDefault Constructor; use setStructure later.
Definition at line 81 of file LinearModel.h.
References shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::HAS_FIRST_INPUT_DERIVATIVE, shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::HAS_FIRST_PARAMETER_DERIVATIVE, and shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::m_features.
|
inline |
Constructor creating a model with given dimensionalities and optional offset term.
Definition at line 88 of file LinearModel.h.
References shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::HAS_FIRST_INPUT_DERIVATIVE, shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::HAS_FIRST_PARAMETER_DERIVATIVE, and shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::m_features.
|
inline |
Construction from matrix (and vector)
Definition at line 104 of file LinearModel.h.
References shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::HAS_FIRST_INPUT_DERIVATIVE, shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::HAS_FIRST_PARAMETER_DERIVATIVE, and shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::m_features.
|
inline |
Returns the activation function.
Definition at line 182 of file LinearModel.h.
|
inline |
Returns the activation function.
Definition at line 177 of file LinearModel.h.
|
inlinevirtual |
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!
Reimplemented from shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >.
Definition at line 186 of file LinearModel.h.
|
inlinevirtual |
Evaluate the model: output = matrix * input + offset.
Reimplemented from shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >.
Definition at line 193 of file LinearModel.h.
References shark::LinearModel< InputType, ActivationFunction >::hasOffset().
|
inlinevirtual |
Evaluate the model: output = matrix * input + offset.
Implements shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >.
Definition at line 213 of file LinearModel.h.
References shark::LinearModel< InputType, ActivationFunction >::hasOffset(), and shark::State::toState().
|
inlinevirtual |
Standard interface for evaluating the response of the model to a batch of patterns.
patterns | the inputs of the model |
outputs | the predictions or response of the model to every pattern |
Reimplemented from shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >.
Definition at line 163 of file AbstractModel.h.
|
virtual |
Standard interface for evaluating the response of the model to a batch of patterns.
patterns | the inputs of the model |
outputs | the predictions or response of the model to every pattern |
state | intermediate results stored by eval which can be reused for derivative computation. |
Implements shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >.
|
inline |
Definition at line 203 of file LinearModel.h.
References shark::LinearModel< InputType, ActivationFunction >::hasOffset().
|
inlinevirtual |
Standard interface for evaluating the response of the model to a single pattern.
pattern | the input of the model |
output | the prediction or response of the model to the pattern |
Reimplemented from shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >.
Definition at line 179 of file AbstractModel.h.
|
inline |
check for the presence of an offset term
Definition at line 116 of file LinearModel.h.
Referenced by shark::LinearModel< InputType, ActivationFunction >::eval(), shark::LinearModel< InputType, ActivationFunction >::eval(), shark::LinearModel< InputType, ActivationFunction >::eval(), shark::LinearModel< InputType, ActivationFunction >::weightedDerivatives(), and shark::LinearModel< InputType, ActivationFunction >::weightedParameterDerivative().
|
inlinevirtual |
Returns the expected shape of the input.
Implements shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >.
Definition at line 121 of file LinearModel.h.
Referenced by shark::LinearModel< InputType, ActivationFunction >::setParameterVector(), shark::LinearModel< InputType, ActivationFunction >::weightedDerivatives(), and shark::LinearModel< InputType, ActivationFunction >::weightedParameterDerivative().
|
inline |
Definition at line 164 of file LinearModel.h.
|
inline |
return a copy of the matrix in dense format
Definition at line 160 of file LinearModel.h.
Referenced by main(), main(), and shark::LinearModel< InputType, ActivationFunction >::setStructure().
|
inlinevirtual |
From INameable: return the class name.
Reimplemented from shark::INameable.
Definition at line 100 of file LinearModel.h.
|
inlinevirtual |
return the number of parameter
Reimplemented from shark::IParameterizable< VectorType >.
Definition at line 143 of file LinearModel.h.
Referenced by shark::LinearModel< InputType, ActivationFunction >::weightedDerivatives(), and shark::LinearModel< InputType, ActivationFunction >::weightedParameterDerivative().
|
inline |
Definition at line 172 of file LinearModel.h.
|
inline |
return the offset
Definition at line 169 of file LinearModel.h.
Referenced by main(), shark::LinearModel< InputType, ActivationFunction >::setStructure(), and shark::LinearModel< InputType, ActivationFunction >::setStructure().
|
inlinevirtual |
Returns the shape of the output.
Implements shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >.
Definition at line 125 of file LinearModel.h.
Referenced by main(), main(), shark::LinearModel< InputType, ActivationFunction >::setParameterVector(), shark::PCA::train(), shark::LinearModel< InputType, ActivationFunction >::weightedDerivatives(), and shark::LinearModel< InputType, ActivationFunction >::weightedParameterDerivative().
|
inlinevirtual |
obtain the parameter vector
Reimplemented from shark::IParameterizable< VectorType >.
Definition at line 130 of file LinearModel.h.
Referenced by shark::SvmLogisticInterpretation< InputType >::evalDerivative().
|
inlinevirtual |
From ISerializable.
Reimplemented from shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >.
Definition at line 307 of file LinearModel.h.
|
inline |
overwrite the parameter vector
Definition at line 135 of file LinearModel.h.
References shark::LinearModel< InputType, ActivationFunction >::inputShape(), shark::Shape::numElements(), and shark::LinearModel< InputType, ActivationFunction >::outputShape().
Referenced by main().
|
inline |
overwrite structure and parameters
Definition at line 154 of file LinearModel.h.
References shark::LinearModel< InputType, ActivationFunction >::matrix(), and shark::LinearModel< InputType, ActivationFunction >::offset().
|
inline |
overwrite structure and parameters
Definition at line 148 of file LinearModel.h.
References shark::LinearModel< InputType, ActivationFunction >::offset().
Referenced by shark::LassoRegression< InputVectorType >::train(), and shark::NormalizeComponentsZCA::train().
|
inline |
Definition at line 270 of file LinearModel.h.
References shark::LinearModel< InputType, ActivationFunction >::hasOffset(), shark::LinearModel< InputType, ActivationFunction >::inputShape(), shark::LinearModel< InputType, ActivationFunction >::numberOfParameters(), shark::Shape::numElements(), shark::LinearModel< InputType, ActivationFunction >::outputShape(), SIZE_CHECK, and shark::State::toState().
|
inline |
Calculates the first derivative w.r.t the inputs and summs them up over all patterns of the last computed batch.
Definition at line 252 of file LinearModel.h.
References SIZE_CHECK, and shark::State::toState().
|
inline |
Calculates the first derivative w.r.t the parameters and summing them up over all patterns of the last computed batch.
Definition at line 224 of file LinearModel.h.
References shark::LinearModel< InputType, ActivationFunction >::hasOffset(), shark::LinearModel< InputType, ActivationFunction >::inputShape(), shark::LinearModel< InputType, ActivationFunction >::numberOfParameters(), shark::Shape::numElements(), shark::LinearModel< InputType, ActivationFunction >::outputShape(), SIZE_CHECK, and shark::State::toState().
|
inlinevirtual |
From ISerializable.
Reimplemented from shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >.
Definition at line 314 of file LinearModel.h.