shark::LinearModel< InputType, ActivationFunction > Class Template Reference

Linear Prediction with optional activation function. More...

#include <shark/Models/LinearModel.h>

+ Inheritance diagram for shark::LinearModel< InputType, ActivationFunction >:

Public Types

typedef base_type::BatchInputType BatchInputType
 
typedef base_type::BatchOutputType BatchOutputType
 
typedef base_type::ParameterVectorType ParameterVectorType
 

Public Member Functions

 LinearModel ()
 CDefault Constructor; use setStructure later. More...
 
 LinearModel (Shape const &inputs, Shape const &outputs=1, bool offset=false)
 Constructor creating a model with given dimensionalities and optional offset term. More...
 
std::string name () const
 From INameable: return the class name. More...
 
 LinearModel (MatrixType const &matrix, VectorType const &offset=VectorType())
 Construction from matrix (and vector) More...
 
bool hasOffset () const
 check for the presence of an offset term More...
 
Shape inputShape () const
 Returns the expected shape of the input. More...
 
Shape outputShape () const
 Returns the shape of the output. More...
 
ParameterVectorType parameterVector () const
 obtain the parameter vector More...
 
void setParameterVector (ParameterVectorType const &newParameters)
 overwrite the parameter vector More...
 
size_t numberOfParameters () const
 return the number of parameter More...
 
void setStructure (Shape const &inputs, Shape const &outputs=1, bool offset=false)
 overwrite structure and parameters More...
 
void setStructure (MatrixType const &matrix, VectorType const &offset=VectorType())
 overwrite structure and parameters More...
 
MatrixType const & matrix () const
 return a copy of the matrix in dense format More...
 
MatrixType & matrix ()
 
VectorType const & offset () const
 return the offset More...
 
VectorType & offset ()
 
ActivationFunction const & activationFunction () const
 Returns the activation function. More...
 
ActivationFunction & activationFunction ()
 Returns the activation function. More...
 
boost::shared_ptr< StatecreateState () const
 Creates an internal state of the model. More...
 
void eval (BatchInputType const &inputs, BatchOutputType &outputs) const
 Evaluate the model: output = matrix * input + offset. More...
 
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. More...
 
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. More...
 
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. More...
 
void weightedDerivatives (BatchInputType const &patterns, BatchOutputType const &outputs, BatchOutputType const &coefficients, State const &state, ParameterVectorType &parameterDerivative, MatrixType &inputDerivative) const
 
void read (InArchive &archive)
 From ISerializable. More...
 
void write (OutArchive &archive) const
 From ISerializable. More...
 
- Public Member Functions inherited from shark::INameable
virtual ~INameable ()
 
- Public Member Functions inherited from shark::ISerializable
virtual ~ISerializable ()
 Virtual d'tor. More...
 
void load (InArchive &archive, unsigned int version)
 Versioned loading of components, calls read(...). More...
 
void save (OutArchive &archive, unsigned int version) const
 Versioned storing of components, calls write(...). More...
 
 BOOST_SERIALIZATION_SPLIT_MEMBER ()
 

Detailed Description

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
class shark::LinearModel< InputType, ActivationFunction >

Linear Prediction with optional activation function.

This model computes the result of \( y = f(x) = g(A x + b) \), where g is an arbitrary activation function. By default g is the identity and the model is a simple linear model. Otherwise, this is known as a generalized linear model. There are two important special cases: The output may be a single number, and the offset term b may be dropped.

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 57 of file LinearModel.h.

Member Typedef Documentation

◆ BatchInputType

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
typedef base_type::BatchInputType shark::LinearModel< InputType, ActivationFunction >::BatchInputType

Definition at line 73 of file LinearModel.h.

◆ BatchOutputType

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
typedef base_type::BatchOutputType shark::LinearModel< InputType, ActivationFunction >::BatchOutputType

Definition at line 74 of file LinearModel.h.

◆ ParameterVectorType

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
typedef base_type::ParameterVectorType shark::LinearModel< InputType, ActivationFunction >::ParameterVectorType

Definition at line 75 of file LinearModel.h.

Constructor & Destructor Documentation

◆ LinearModel() [1/3]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
shark::LinearModel< InputType, ActivationFunction >::LinearModel ( )
inline

CDefault Constructor; use setStructure later.

Definition at line 78 of file LinearModel.h.

◆ LinearModel() [2/3]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
shark::LinearModel< InputType, ActivationFunction >::LinearModel ( Shape const &  inputs,
Shape const &  outputs = 1,
bool  offset = false 
)
inline

Constructor creating a model with given dimensionalities and optional offset term.

Definition at line 85 of file LinearModel.h.

◆ LinearModel() [3/3]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
shark::LinearModel< InputType, ActivationFunction >::LinearModel ( MatrixType const &  matrix,
VectorType const &  offset = VectorType() 
)
inline

Construction from matrix (and vector)

Definition at line 101 of file LinearModel.h.

Member Function Documentation

◆ activationFunction() [1/2]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
ActivationFunction const& shark::LinearModel< InputType, ActivationFunction >::activationFunction ( ) const
inline

Returns the activation function.

Definition at line 174 of file LinearModel.h.

◆ activationFunction() [2/2]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
ActivationFunction& shark::LinearModel< InputType, ActivationFunction >::activationFunction ( )
inline

Returns the activation function.

Definition at line 179 of file LinearModel.h.

◆ createState()

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
boost::shared_ptr<State> shark::LinearModel< InputType, ActivationFunction >::createState ( ) const
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< InputType, blas::vector< InputType::value_type, InputType::device_type >, blas::vector< InputType::value_type, InputType::device_type > >.

Definition at line 183 of file LinearModel.h.

◆ eval() [1/3]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
void shark::LinearModel< InputType, ActivationFunction >::eval ( BatchInputType const &  inputs,
BatchOutputType outputs 
) const
inlinevirtual

◆ eval() [2/3]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
void shark::LinearModel< InputType, ActivationFunction >::eval ( InputType const &  input,
VectorType &  output 
) const
inline

Definition at line 200 of file LinearModel.h.

◆ eval() [3/3]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
void shark::LinearModel< InputType, ActivationFunction >::eval ( BatchInputType const &  inputs,
BatchOutputType outputs,
State state 
) const
inlinevirtual

◆ hasOffset()

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
bool shark::LinearModel< InputType, ActivationFunction >::hasOffset ( ) const
inline

check for the presence of an offset term

Definition at line 113 of file LinearModel.h.

◆ inputShape()

◆ matrix() [1/2]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
MatrixType const& shark::LinearModel< InputType, ActivationFunction >::matrix ( ) const
inline

return a copy of the matrix in dense format

Definition at line 157 of file LinearModel.h.

Referenced by main().

◆ matrix() [2/2]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
MatrixType& shark::LinearModel< InputType, ActivationFunction >::matrix ( )
inline

Definition at line 161 of file LinearModel.h.

◆ name()

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
std::string shark::LinearModel< InputType, ActivationFunction >::name ( ) const
inlinevirtual

From INameable: return the class name.

Reimplemented from shark::INameable.

Definition at line 97 of file LinearModel.h.

◆ numberOfParameters()

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
size_t shark::LinearModel< InputType, ActivationFunction >::numberOfParameters ( ) const
inlinevirtual

◆ offset() [1/2]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
VectorType const& shark::LinearModel< InputType, ActivationFunction >::offset ( ) const
inline

return the offset

Definition at line 166 of file LinearModel.h.

Referenced by main(), and shark::LinearModel< VectorType >::setStructure().

◆ offset() [2/2]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
VectorType& shark::LinearModel< InputType, ActivationFunction >::offset ( )
inline

Definition at line 169 of file LinearModel.h.

◆ outputShape()

◆ parameterVector()

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
ParameterVectorType shark::LinearModel< InputType, ActivationFunction >::parameterVector ( ) const
inlinevirtual

obtain the parameter vector

Reimplemented from shark::IParameterizable< blas::vector< InputType::value_type, InputType::device_type > >.

Definition at line 127 of file LinearModel.h.

◆ read()

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
void shark::LinearModel< InputType, ActivationFunction >::read ( InArchive archive)
inlinevirtual

◆ setParameterVector()

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
void shark::LinearModel< InputType, ActivationFunction >::setParameterVector ( ParameterVectorType const &  newParameters)
inline

overwrite the parameter vector

Definition at line 132 of file LinearModel.h.

Referenced by main().

◆ setStructure() [1/2]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
void shark::LinearModel< InputType, ActivationFunction >::setStructure ( Shape const &  inputs,
Shape const &  outputs = 1,
bool  offset = false 
)
inline

overwrite structure and parameters

Definition at line 145 of file LinearModel.h.

Referenced by shark::LinearSAGTrainer< InputType, LabelType >::numberOfParameters(), and shark::NormalizeComponentsZCA::train().

◆ setStructure() [2/2]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
void shark::LinearModel< InputType, ActivationFunction >::setStructure ( MatrixType const &  matrix,
VectorType const &  offset = VectorType() 
)
inline

overwrite structure and parameters

Definition at line 151 of file LinearModel.h.

◆ weightedDerivatives()

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
void shark::LinearModel< InputType, ActivationFunction >::weightedDerivatives ( BatchInputType const &  patterns,
BatchOutputType const &  outputs,
BatchOutputType const &  coefficients,
State const &  state,
ParameterVectorType parameterDerivative,
MatrixType &  inputDerivative 
) const
inline

Definition at line 267 of file LinearModel.h.

◆ weightedInputDerivative()

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
void shark::LinearModel< InputType, ActivationFunction >::weightedInputDerivative ( BatchInputType const &  patterns,
BatchOutputType const &  outputs,
BatchOutputType const &  coefficients,
State const &  state,
MatrixType &  derivative 
) const
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 249 of file LinearModel.h.

◆ weightedParameterDerivative()

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
void shark::LinearModel< InputType, ActivationFunction >::weightedParameterDerivative ( BatchInputType const &  patterns,
BatchOutputType const &  outputs,
BatchOutputType const &  coefficients,
State const &  state,
ParameterVectorType gradient 
) const
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 221 of file LinearModel.h.

◆ write()

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
void shark::LinearModel< InputType, ActivationFunction >::write ( OutArchive archive) const
inlinevirtual

The documentation for this class was generated from the following file: