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 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, ParameterVectorTypeModelBaseType
 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< FeatureFeatures
 
typedef TypedFeatureNotAvailableException< FeatureFeatureNotAvailableException
 
- 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
 
MatrixTypematrix ()
 
VectorType const & offset () const
 return the offset
 
VectorTypeoffset ()
 
ActivationFunction const & activationFunction () const
 Returns the activation function.
 
ActivationFunction & activationFunction ()
 Returns the activation function.
 
boost::shared_ptr< StatecreateState () 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 &parameterDerivative, 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 Featuresfeatures () 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< OutputTypeoperator() (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 &parameterDerivative, 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
 

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, see Neuron activation functions.. 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 59 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 76 of file LinearModel.h.

◆ BatchOutputType

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

Definition at line 77 of file LinearModel.h.

◆ MatrixType

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
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.

◆ ParameterVectorType

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

Definition at line 78 of file LinearModel.h.

◆ VectorType

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
typedef blas::vector<typename InputType::value_type, typename InputType::device_type> shark::LinearModel< InputType, ActivationFunction >::VectorType

Definition at line 65 of file LinearModel.h.

Constructor & Destructor Documentation

◆ LinearModel() [1/3]

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

◆ 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

◆ LinearModel() [3/3]

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

Member Function Documentation

◆ activationFunction() [1/2]

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

Returns the activation function.

Definition at line 182 of file LinearModel.h.

◆ activationFunction() [2/2]

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

Returns the activation function.

Definition at line 177 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< InputTypeT, OutputTypeT, ParameterVectorType >.

Definition at line 186 of file LinearModel.h.

◆ eval() [1/6]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
void shark::LinearModel< InputType, ActivationFunction >::eval ( BatchInputType const &  inputs,
BatchOutputType outputs 
) const
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().

◆ eval() [2/6]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
void shark::LinearModel< InputType, ActivationFunction >::eval ( BatchInputType const &  inputs,
BatchOutputType outputs,
State state 
) const
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().

◆ eval() [3/6]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
virtual void shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::eval ( BatchInputType const &  patterns,
BatchOutputType outputs 
) const
inlinevirtual

Standard interface for evaluating the response of the model to a batch of patterns.

Parameters
patternsthe inputs of the model
outputsthe predictions or response of the model to every pattern

Reimplemented from shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >.

Definition at line 163 of file AbstractModel.h.

◆ eval() [4/6]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
virtual void shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::eval ( BatchInputType const &  patterns,
BatchOutputType outputs,
State state 
) const
virtual

Standard interface for evaluating the response of the model to a batch of patterns.

Parameters
patternsthe inputs of the model
outputsthe predictions or response of the model to every pattern
stateintermediate results stored by eval which can be reused for derivative computation.

Implements shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >.

◆ eval() [5/6]

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

◆ eval() [6/6]

template<class InputType = RealVector, class ActivationFunction = LinearNeuron>
virtual void shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >::eval ( InputType const &  pattern,
OutputType output 
) const
inlinevirtual

Standard interface for evaluating the response of the model to a single pattern.

Parameters
patternthe input of the model
outputthe prediction or response of the model to the pattern

Reimplemented from shark::AbstractModel< InputTypeT, OutputTypeT, ParameterVectorType >.

Definition at line 179 of file AbstractModel.h.

◆ hasOffset()

◆ inputShape()

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

◆ matrix() [1/2]

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

Definition at line 164 of file LinearModel.h.

◆ matrix() [2/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 160 of file LinearModel.h.

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

◆ 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 100 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 & shark::LinearModel< InputType, ActivationFunction >::offset ( )
inline

Definition at line 172 of file LinearModel.h.

◆ offset() [2/2]

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

◆ 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< VectorType >.

Definition at line 130 of file LinearModel.h.

Referenced by shark::SvmLogisticInterpretation< InputType >::evalDerivative().

◆ 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

◆ setStructure() [1/2]

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

◆ setStructure() [2/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

◆ 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

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

References SIZE_CHECK, and shark::State::toState().

◆ 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

◆ 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: