shark::Conv2DModel< VectorType, ActivationFunction > Class Template Reference

Convolutional Model for 2D image data. More...

#include <shark/Models/ConvolutionalModel.h>

+ Inheritance diagram for shark::Conv2DModel< VectorType, ActivationFunction >:

Public Types

typedef base_type::BatchOutputType BatchOutputType
 
typedef base_type::BatchInputType BatchInputType
 
typedef base_type::ParameterVectorType ParameterVectorType
 
- Public Types inherited from shark::AbstractModel< VectorType, VectorType, VectorType >
enum  Feature
 
typedef VectorType InputType
 Defines the input type of the model. More...
 
typedef VectorType OutputType
 Defines the output type of the model. More...
 
typedef AbstractModel< VectorType, VectorType, VectorTypeModelBaseType
 Defines the BaseType used by the model (this type). Useful for creating derived models. More...
 
typedef Batch< InputType >::type BatchInputType
 defines the batch type of the input type. More...
 
typedef Batch< OutputType >::type BatchOutputType
 defines the batch type of the output type More...
 
typedef TypedFlags< FeatureFeatures
 
typedef TypedFeatureNotAvailableException< FeatureFeatureNotAvailableException
 
- Public Types inherited from shark::IParameterizable< VectorType >
typedef VectorType ParameterVectorType
 

Public Member Functions

 Conv2DModel ()
 Default Constructor; use setStructure later. More...
 
 Conv2DModel (Shape const &imageShape, Shape const &filterShape, Convolution type=Convolution::ZeroPad)
 Sets the structure by setting the dimensionalities of image and filters. More...
 
std::string name () const
 returns the name of the object More...
 
Shape inputShape () const
 Returns the expected shape of the input. More...
 
Shape outputShape () const
 Returns the shape of the output. More...
 
ActivationFunction const & activationFunction () const
 Returns the activation function. More...
 
ActivationFunction & activationFunction ()
 Returns the activation function. More...
 
ParameterVectorType parameterVector () const
 Obtain the parameter vector. More...
 
void setParameterVector (ParameterVectorType const &newParameters)
 Set the new parameters of the model. More...
 
size_t numberOfParameters () const
 Return the number of parameters. More...
 
void setStructure (Shape const &imageShape, Shape const &filterShape, Convolution type=Convolution::ZeroPad)
 Sets the structure by setting the shape of image and filters. More...
 
boost::shared_ptr< StatecreateState () const
 Creates an internal state of the model. More...
 
void eval (BatchInputType const &inputs, BatchOutputType &outputs, State &state) const
 Evaluate the model. More...
 
void weightedParameterDerivative (BatchInputType const &inputs, 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 inputs of the last computed batch. More...
 
void weightedInputDerivative (BatchInputType const &inputs, BatchOutputType const &outputs, BatchOutputType const &coefficients, State const &state, BatchInputType &derivatives) const
 Calculates the first derivative w.r.t the inputs and summs them up over all inputs of the last computed batch. More...
 
void read (InArchive &archive)
 From ISerializable. More...
 
void write (OutArchive &archive) const
 From ISerializable. More...
 
- Public Member Functions inherited from shark::AbstractModel< VectorType, VectorType, VectorType >
 AbstractModel ()
 
virtual ~AbstractModel ()
 
const Featuresfeatures () const
 
virtual void updateFeatures ()
 
bool hasFirstParameterDerivative () const
 Returns true when the first parameter derivative is implemented. More...
 
bool hasFirstInputDerivative () const
 Returns true when the first input derivative is implemented. More...
 
virtual void eval (BatchInputType const &patterns, BatchOutputType &outputs) const
 Standard interface for evaluating the response of the model to a batch of patterns. More...
 
virtual void eval (InputType const &pattern, OutputType &output) const
 Standard interface for evaluating the response of the model to a single pattern. More...
 
Data< OutputTypeoperator() (Data< InputType > const &patterns) const
 Model evaluation as an operator for a whole dataset. This is a convenience function. More...
 
OutputType operator() (InputType const &pattern) const
 Model evaluation as an operator for a single pattern. This is a convenience function. More...
 
BatchOutputType operator() (BatchInputType const &patterns) const
 Model evaluation as an operator for a single pattern. This is a convenience function. More...
 
virtual void weightedDerivatives (BatchInputType const &patterns, BatchOutputType const &outputs, BatchOutputType const &coefficients, State const &state, RealVector &parameterDerivative, BatchInputType &inputDerivative) const
 calculates weighted input and parameter derivative at the same time More...
 
- Public Member Functions inherited from shark::IParameterizable< VectorType >
virtual ~IParameterizable ()
 
- 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 ()
 

Additional Inherited Members

- Protected Attributes inherited from shark::AbstractModel< VectorType, VectorType, VectorType >
Features m_features
 

Detailed Description

template<class VectorType = RealVector, class ActivationFunction = LinearNeuron>
class shark::Conv2DModel< VectorType, ActivationFunction >

Convolutional Model for 2D image data.

This model computes the result of \( y = f(x) = g(\text{convolution}(w, x) + b) \), where g is an arbitrary activation function and convolution is the convolution of the input image x with the filters w. b is a vector with one entry for each filter which is then applied to each response above

The image is allowed to have several channels andare linearized to a single vector of size width * height * numChannels. the linearization is performed as linearizing each channel as if it were a row-major matrix and then concatenating the different channels.

For handling edge condition, the Conv2D model handles two different convolution modes:

Convolution::Valid: The output is only computed on patches which are fully inside the unpadded image as a linearized vector in the same format of size (width - filter_width+1) * (height - filter_height+1) * numFilters.

Convolution::ZeroPad The output input is padded with zeros and the output has the same size as the input of size width * height * numFilters.

Definition at line 67 of file ConvolutionalModel.h.

Member Typedef Documentation

◆ BatchInputType

template<class VectorType = RealVector, class ActivationFunction = LinearNeuron>
typedef base_type::BatchInputType shark::Conv2DModel< VectorType, ActivationFunction >::BatchInputType

Definition at line 75 of file ConvolutionalModel.h.

◆ BatchOutputType

template<class VectorType = RealVector, class ActivationFunction = LinearNeuron>
typedef base_type::BatchOutputType shark::Conv2DModel< VectorType, ActivationFunction >::BatchOutputType

Definition at line 72 of file ConvolutionalModel.h.

◆ ParameterVectorType

template<class VectorType = RealVector, class ActivationFunction = LinearNeuron>
typedef base_type::ParameterVectorType shark::Conv2DModel< VectorType, ActivationFunction >::ParameterVectorType

Definition at line 76 of file ConvolutionalModel.h.

Constructor & Destructor Documentation

◆ Conv2DModel() [1/2]

template<class VectorType = RealVector, class ActivationFunction = LinearNeuron>
shark::Conv2DModel< VectorType, ActivationFunction >::Conv2DModel ( )
inline

Default Constructor; use setStructure later.

Definition at line 79 of file ConvolutionalModel.h.

◆ Conv2DModel() [2/2]

template<class VectorType = RealVector, class ActivationFunction = LinearNeuron>
shark::Conv2DModel< VectorType, ActivationFunction >::Conv2DModel ( Shape const &  imageShape,
Shape const &  filterShape,
Convolution  type = Convolution::ZeroPad 
)
inline

Sets the structure by setting the dimensionalities of image and filters.

  • imageShape Shape of the image imHeight x imWidth x channel
  • filterShape Shape of the filter matrix numFilters x fiHeight x fiWidth x channel
  • type Type of convolution padding to perform

Definition at line 89 of file ConvolutionalModel.h.

Member Function Documentation

◆ activationFunction() [1/2]

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

Returns the activation function.

Definition at line 114 of file ConvolutionalModel.h.

◆ activationFunction() [2/2]

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

Returns the activation function.

Definition at line 119 of file ConvolutionalModel.h.

◆ createState()

template<class VectorType = RealVector, class ActivationFunction = LinearNeuron>
boost::shared_ptr<State> shark::Conv2DModel< VectorType, 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< VectorType, VectorType, VectorType >.

Definition at line 161 of file ConvolutionalModel.h.

◆ eval()

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

Evaluate the model.

Implements shark::AbstractModel< VectorType, VectorType, VectorType >.

Definition at line 168 of file ConvolutionalModel.h.

References SIZE_CHECK, and shark::Valid.

◆ inputShape()

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

Returns the expected shape of the input.

Implements shark::AbstractModel< VectorType, VectorType, VectorType >.

Definition at line 101 of file ConvolutionalModel.h.

◆ name()

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

returns the name of the object

Reimplemented from shark::INameable.

Definition at line 97 of file ConvolutionalModel.h.

◆ numberOfParameters()

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

Return the number of parameters.

Reimplemented from shark::IParameterizable< VectorType >.

Definition at line 137 of file ConvolutionalModel.h.

◆ outputShape()

template<class VectorType = RealVector, class ActivationFunction = LinearNeuron>
Shape shark::Conv2DModel< VectorType, ActivationFunction >::outputShape ( ) const
inlinevirtual

Returns the shape of the output.

Implements shark::AbstractModel< VectorType, VectorType, VectorType >.

Definition at line 105 of file ConvolutionalModel.h.

References shark::Valid.

◆ parameterVector()

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

Obtain the parameter vector.

Reimplemented from shark::IParameterizable< VectorType >.

Definition at line 124 of file ConvolutionalModel.h.

◆ read()

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

◆ setParameterVector()

template<class VectorType = RealVector, class ActivationFunction = LinearNeuron>
void shark::Conv2DModel< VectorType, ActivationFunction >::setParameterVector ( ParameterVectorType const &  newParameters)
inlinevirtual

Set the new parameters of the model.

Reimplemented from shark::IParameterizable< VectorType >.

Definition at line 129 of file ConvolutionalModel.h.

References SIZE_CHECK.

◆ setStructure()

template<class VectorType = RealVector, class ActivationFunction = LinearNeuron>
void shark::Conv2DModel< VectorType, ActivationFunction >::setStructure ( Shape const &  imageShape,
Shape const &  filterShape,
Convolution  type = Convolution::ZeroPad 
)
inline

Sets the structure by setting the shape of image and filters.

  • imageShape Shape of the image imHeight x imWidth x channel
  • filterShape Shape of the filter matrix numFilters x fiHeight x fiWidth
  • type Type of convolution padding to perform

Definition at line 146 of file ConvolutionalModel.h.

◆ weightedInputDerivative()

template<class VectorType = RealVector, class ActivationFunction = LinearNeuron>
void shark::Conv2DModel< VectorType, ActivationFunction >::weightedInputDerivative ( BatchInputType const &  inputs,
BatchOutputType const &  outputs,
BatchOutputType const &  coefficients,
State const &  state,
BatchInputType derivatives 
) const
inlinevirtual

Calculates the first derivative w.r.t the inputs and summs them up over all inputs of the last computed batch.

Reimplemented from shark::AbstractModel< VectorType, VectorType, VectorType >.

Definition at line 234 of file ConvolutionalModel.h.

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

◆ weightedParameterDerivative()

template<class VectorType = RealVector, class ActivationFunction = LinearNeuron>
void shark::Conv2DModel< VectorType, ActivationFunction >::weightedParameterDerivative ( BatchInputType const &  inputs,
BatchOutputType const &  outputs,
BatchOutputType const &  coefficients,
State const &  state,
ParameterVectorType gradient 
) const
inlinevirtual

Calculates the first derivative w.r.t the parameters and summing them up over all inputs of the last computed batch.

Reimplemented from shark::AbstractModel< VectorType, VectorType, VectorType >.

Definition at line 190 of file ConvolutionalModel.h.

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

◆ write()

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

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