Linear model in a kernel feature space. More...
#include <shark/Models/Kernels/KernelExpansion.h>
Public Types | |
typedef AbstractKernelFunction< InputType > | KernelType |
typedef base_type::BatchInputType | BatchInputType |
typedef base_type::BatchOutputType | BatchOutputType |
Public Types inherited from shark::AbstractModel< InputType, RealVector > | |
enum | Feature |
typedef InputType | InputType |
Defines the input type of the model. | |
typedef RealVector | OutputType |
Defines the output type of the model. | |
typedef AbstractModel< InputType, RealVector, RealVector > | 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 | |
KernelExpansion () | |
KernelExpansion (KernelType *kernel) | |
KernelExpansion (KernelType *kernel, Data< InputType > const &basis, bool offset, std::size_t outputs=1) | |
void | setStructure (KernelType *kernel, Data< InputType > const &basis, bool offset, std::size_t outputs=1) |
std::string | name () const |
From INameable: return the class name. | |
Shape | outputShape () const |
dimensionality of the output RealVector | |
Shape | inputShape () const |
Returns the expected shape of the input. | |
KernelType const * | kernel () const |
KernelType * | kernel () |
void | setKernel (KernelType *kernel) |
bool | hasOffset () const |
RealMatrix & | alpha () |
RealMatrix const & | alpha () const |
double & | alpha (std::size_t example, std::size_t cls) |
double const & | alpha (std::size_t example, std::size_t cls) const |
RealVector & | offset () |
RealVector const & | offset () const |
double & | offset (std::size_t cls) |
double const & | offset (std::size_t cls) const |
Data< InputType > const & | basis () const |
Data< InputType > & | basis () |
void | sparsify () |
RealVector | parameterVector () const |
Return the parameter vector. | |
void | setParameterVector (RealVector const &newParameters) |
Set the parameter vector. | |
std::size_t | numberOfParameters () const |
Return the number of parameters. | |
boost::shared_ptr< State > | createState () const |
Creates an internal state of the model. | |
void | eval (BatchInputType const &patterns, BatchOutputType &output) const |
Standard interface for evaluating the response of the model to a batch of patterns. | |
void | eval (BatchInputType const &patterns, BatchOutputType &outputs, State &state) const |
Standard interface for evaluating the response of the model to a batch of patterns. | |
void | read (InArchive &archive) |
From ISerializable, reads a model from an archive. | |
void | write (OutArchive &archive) const |
From ISerializable, writes a model to an archive. | |
Public Member Functions inherited from shark::AbstractModel< InputType, RealVector > | |
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. | |
virtual void | eval (InputType const &pattern, OutputType &output) const |
Standard interface for evaluating the response of the model to a single pattern. | |
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, RealVector &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, RealVector ¶meterDerivative, BatchInputType &inputDerivative) const |
calculates weighted input and parameter derivative at the same time | |
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. | |
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 () | |
Protected Attributes | |
KernelType * | mep_kernel |
kernel function used in the expansion | |
Data< InputType > | m_basis |
"support" basis vectors | |
RealMatrix | m_alpha |
kernel coefficients | |
RealVector | m_b |
offset or bias term | |
Protected Attributes inherited from shark::AbstractModel< InputType, RealVector > | |
Features | m_features |
Linear model in a kernel feature space.
An affine linear kernel expansion is a model of the type
\[ x : \mathbb{R}^d \to \mathbb{R}^d \enspace , \enspace x \mapsto \sum_{n=1}^{\ell} \alpha_n k(x_n, x) + b \enspace ,\]
with parameters \( \alpha_n \in \mathbb{R}^{d} \) for all \( n \in \{1, \dots, \ell\} \) and \( b \in \mathbb{R}^d \).
One way in which the possibility for vector-valued input and output of dimension \( d \) may be interpreted is as allowing for a KernelExpansion model for \( d \) different classes/outputs in multi-class problems. Then, the i-th column of the matrix m_alpha is the KernelExpansion for class/output i.
For a choice of kernel, see Kernels.
InputType | Type of basis elements supplied to the kernel |
Definition at line 69 of file KernelExpansion.h.
typedef base_type::BatchInputType shark::KernelExpansion< InputType >::BatchInputType |
Definition at line 74 of file KernelExpansion.h.
typedef base_type::BatchOutputType shark::KernelExpansion< InputType >::BatchOutputType |
Definition at line 75 of file KernelExpansion.h.
typedef AbstractKernelFunction<InputType> shark::KernelExpansion< InputType >::KernelType |
Definition at line 72 of file KernelExpansion.h.
|
inline |
Definition at line 81 of file KernelExpansion.h.
|
inline |
Definition at line 83 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::kernel(), and SHARK_ASSERT.
|
inline |
Definition at line 87 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::basis(), shark::KernelExpansion< InputType >::kernel(), shark::KernelExpansion< InputType >::offset(), shark::KernelExpansion< InputType >::setStructure(), and SHARK_ASSERT.
|
inline |
Definition at line 136 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::m_alpha.
Referenced by shark::MergeBudgetMaintenanceStrategy< RealVector >::addToModel(), shark::ProjectBudgetMaintenanceStrategy< RealVector >::addToModel(), shark::RemoveBudgetMaintenanceStrategy< InputType >::addToModel(), shark::MissingFeaturesKernelExpansion< InputType >::computeNorm(), shark::MissingFeaturesKernelExpansion< InputType >::computeNorm(), shark::LooErrorCSvm< InputType, CacheType >::eval(), shark::AbstractBudgetMaintenanceStrategy< InputType >::findSmallestVector(), shark::kMeans(), shark::MergeBudgetMaintenanceStrategy< RealVector >::reduceBudget(), shark::KernelBudgetedSGDTrainer< InputType, CacheType >::train(), shark::KernelSGDTrainer< InputType, CacheType >::train(), shark::Perceptron< InputType >::train(), shark::RegularizationNetworkTrainer< InputType >::train(), and shark::OneClassSvmTrainer< InputType, CacheType >::trainSVM().
|
inline |
Definition at line 139 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::m_alpha.
|
inline |
Definition at line 142 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::m_alpha.
|
inline |
Definition at line 145 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::m_alpha.
|
inline |
Definition at line 174 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::m_basis.
|
inline |
Definition at line 170 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::m_basis.
Referenced by shark::MergeBudgetMaintenanceStrategy< RealVector >::addToModel(), shark::ProjectBudgetMaintenanceStrategy< RealVector >::addToModel(), shark::RemoveBudgetMaintenanceStrategy< InputType >::addToModel(), shark::KernelExpansion< InputType >::KernelExpansion(), shark::MergeBudgetMaintenanceStrategy< RealVector >::reduceBudget(), and shark::KernelExpansion< InputType >::setStructure().
|
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, RealVector >.
Reimplemented in shark::MissingFeaturesKernelExpansion< InputType >.
Definition at line 231 of file KernelExpansion.h.
|
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< InputType, RealVector >.
Reimplemented in shark::MissingFeaturesKernelExpansion< InputType >.
Definition at line 236 of file KernelExpansion.h.
References shark::Data< Type >::batch(), shark::batchEnd(), shark::batchSize(), shark::KernelExpansion< InputType >::hasOffset(), shark::KernelExpansion< InputType >::m_alpha, shark::KernelExpansion< InputType >::m_b, shark::KernelExpansion< InputType >::m_basis, shark::KernelExpansion< InputType >::mep_kernel, shark::Data< Type >::numberOfBatches(), and SHARK_ASSERT.
Referenced by shark::KernelExpansion< InputType >::eval(), and shark::KernelBudgetedSGDTrainer< InputType, CacheType >::train().
|
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 |
state | intermediate results stored by eval which can be reused for derivative computation. |
Implements shark::AbstractModel< InputType, RealVector >.
Reimplemented in shark::MissingFeaturesKernelExpansion< InputType >.
Definition at line 261 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::eval().
|
inline |
Definition at line 133 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::m_b.
Referenced by shark::KernelExpansion< InputType >::eval(), shark::MissingFeaturesKernelExpansion< InputType >::eval(), shark::KernelExpansion< InputType >::numberOfParameters(), shark::KernelExpansion< InputType >::offset(), shark::KernelExpansion< InputType >::offset(), shark::KernelExpansion< InputType >::offset(), shark::KernelExpansion< InputType >::offset(), shark::KernelExpansion< InputType >::parameterVector(), shark::KernelExpansion< InputType >::setParameterVector(), and shark::MissingFeatureSvmTrainer< InputType, CacheType >::train().
|
inlinevirtual |
Returns the expected shape of the input.
Implements shark::AbstractModel< InputType, RealVector >.
Definition at line 111 of file KernelExpansion.h.
|
inline |
Definition at line 122 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::mep_kernel.
|
inline |
Definition at line 119 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::mep_kernel.
Referenced by shark::ProjectBudgetMaintenanceStrategy< RealVector >::addToModel(), shark::KernelExpansion< InputType >::KernelExpansion(), shark::KernelExpansion< InputType >::KernelExpansion(), shark::MergeBudgetMaintenanceStrategy< RealVector >::reduceBudget(), shark::KernelExpansion< InputType >::setKernel(), shark::KernelExpansion< InputType >::setStructure(), and shark::RankingSvmTrainer< InputType, CacheType >::train().
|
inlinevirtual |
From INameable: return the class name.
Reimplemented from shark::INameable.
Reimplemented in shark::MissingFeaturesKernelExpansion< InputType >.
Definition at line 103 of file KernelExpansion.h.
|
inlinevirtual |
Return the number of parameters.
Reimplemented from shark::IParameterizable< VectorType >.
Definition at line 220 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::hasOffset(), shark::KernelExpansion< InputType >::m_alpha, and shark::KernelExpansion< InputType >::m_b.
Referenced by shark::KernelExpansion< InputType >::setParameterVector().
|
inline |
Definition at line 148 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::hasOffset(), shark::KernelExpansion< InputType >::m_b, and SHARK_RUNTIME_CHECK.
Referenced by shark::LooErrorCSvm< InputType, CacheType >::eval(), shark::KernelExpansion< InputType >::KernelExpansion(), shark::kMeans(), shark::KernelExpansion< InputType >::setStructure(), shark::KernelSGDTrainer< InputType, CacheType >::train(), shark::RegularizationNetworkTrainer< InputType >::train(), and shark::OneClassSvmTrainer< InputType, CacheType >::trainSVM().
|
inline |
Definition at line 152 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::hasOffset(), shark::KernelExpansion< InputType >::m_b, and SHARK_RUNTIME_CHECK.
|
inline |
Definition at line 156 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::hasOffset(), shark::KernelExpansion< InputType >::m_b, and SHARK_RUNTIME_CHECK.
|
inline |
Definition at line 160 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::hasOffset(), shark::KernelExpansion< InputType >::m_b, and SHARK_RUNTIME_CHECK.
|
inlinevirtual |
dimensionality of the output RealVector
Implements shark::AbstractModel< InputType, RealVector >.
Definition at line 107 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::m_alpha.
Referenced by shark::MissingFeaturesKernelExpansion< InputType >::eval().
|
inlinevirtual |
Return the parameter vector.
Reimplemented from shark::IParameterizable< VectorType >.
Definition at line 203 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::hasOffset(), shark::KernelExpansion< InputType >::m_alpha, and shark::KernelExpansion< InputType >::m_b.
|
inlinevirtual |
From ISerializable, reads a model from an archive.
Reimplemented from shark::AbstractModel< InputType, RealVector >.
Definition at line 270 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::m_alpha, shark::KernelExpansion< InputType >::m_b, shark::KernelExpansion< InputType >::m_basis, shark::KernelExpansion< InputType >::mep_kernel, and SHARK_ASSERT.
|
inline |
Definition at line 125 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::kernel(), and shark::KernelExpansion< InputType >::mep_kernel.
|
inlinevirtual |
Set the parameter vector.
Reimplemented from shark::IParameterizable< VectorType >.
Definition at line 212 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::hasOffset(), shark::KernelExpansion< InputType >::m_alpha, shark::KernelExpansion< InputType >::m_b, shark::KernelExpansion< InputType >::numberOfParameters(), and SHARK_RUNTIME_CHECK.
|
inline |
Definition at line 92 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::basis(), shark::KernelExpansion< InputType >::kernel(), shark::KernelExpansion< InputType >::m_alpha, shark::KernelExpansion< InputType >::m_b, shark::KernelExpansion< InputType >::m_basis, shark::KernelExpansion< InputType >::mep_kernel, shark::Data< Type >::numberOfElements(), shark::KernelExpansion< InputType >::offset(), and SHARK_ASSERT.
Referenced by shark::KernelExpansion< InputType >::KernelExpansion(), shark::kMeans(), shark::KernelBudgetedSGDTrainer< InputType, CacheType >::train(), shark::KernelSGDTrainer< InputType, CacheType >::train(), shark::Perceptron< InputType >::train(), shark::RankingSvmTrainer< InputType, CacheType >::train(), shark::RegularizationNetworkTrainer< InputType >::train(), shark::EpsilonSvmTrainer< InputType, CacheType >::train(), shark::OneClassSvmTrainer< InputType, CacheType >::train(), and shark::MissingFeatureSvmTrainer< InputType, CacheType >::train().
|
inline |
The sparsify method removes non-support-vectors from its set of basis vectors and the coefficient matrix.
Definition at line 180 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::m_alpha, shark::KernelExpansion< InputType >::m_basis, shark::Data< Type >::numberOfElements(), shark::subset(), shark::swap(), shark::toDataset(), and shark::toView().
Referenced by shark::KernelBudgetedSGDTrainer< InputType, CacheType >::train(), shark::EpsilonSvmTrainer< InputType, CacheType >::train(), and shark::OneClassSvmTrainer< InputType, CacheType >::train().
|
inlinevirtual |
From ISerializable, writes a model to an archive.
Reimplemented from shark::AbstractModel< InputType, RealVector >.
Definition at line 280 of file KernelExpansion.h.
References shark::KernelExpansion< InputType >::m_alpha, shark::KernelExpansion< InputType >::m_b, shark::KernelExpansion< InputType >::m_basis, shark::KernelExpansion< InputType >::mep_kernel, and SHARK_ASSERT.
|
protected |
kernel coefficients
Definition at line 301 of file KernelExpansion.h.
Referenced by shark::KernelExpansion< InputType >::alpha(), shark::KernelExpansion< InputType >::alpha(), shark::KernelExpansion< InputType >::alpha(), shark::KernelExpansion< InputType >::alpha(), shark::KernelExpansion< InputType >::eval(), shark::MissingFeaturesKernelExpansion< InputType >::eval(), shark::KernelExpansion< InputType >::numberOfParameters(), shark::KernelExpansion< InputType >::outputShape(), shark::KernelExpansion< InputType >::parameterVector(), shark::KernelExpansion< InputType >::read(), shark::KernelExpansion< InputType >::setParameterVector(), shark::KernelExpansion< InputType >::setStructure(), shark::KernelExpansion< InputType >::sparsify(), and shark::KernelExpansion< InputType >::write().
|
protected |
offset or bias term
Definition at line 304 of file KernelExpansion.h.
Referenced by shark::KernelExpansion< InputType >::eval(), shark::MissingFeaturesKernelExpansion< InputType >::eval(), shark::KernelExpansion< InputType >::hasOffset(), shark::KernelExpansion< InputType >::numberOfParameters(), shark::KernelExpansion< InputType >::offset(), shark::KernelExpansion< InputType >::offset(), shark::KernelExpansion< InputType >::offset(), shark::KernelExpansion< InputType >::offset(), shark::KernelExpansion< InputType >::parameterVector(), shark::KernelExpansion< InputType >::read(), shark::KernelExpansion< InputType >::setParameterVector(), shark::KernelExpansion< InputType >::setStructure(), and shark::KernelExpansion< InputType >::write().
|
protected |
"support" basis vectors
Definition at line 298 of file KernelExpansion.h.
Referenced by shark::KernelExpansion< InputType >::basis(), shark::KernelExpansion< InputType >::basis(), shark::MissingFeaturesKernelExpansion< InputType >::computeNorm(), shark::MissingFeaturesKernelExpansion< InputType >::computeNorm(), shark::KernelExpansion< InputType >::eval(), shark::MissingFeaturesKernelExpansion< InputType >::eval(), shark::KernelExpansion< InputType >::read(), shark::KernelExpansion< InputType >::setStructure(), shark::KernelExpansion< InputType >::sparsify(), and shark::KernelExpansion< InputType >::write().
|
protected |
kernel function used in the expansion
Definition at line 295 of file KernelExpansion.h.
Referenced by shark::MissingFeaturesKernelExpansion< InputType >::computeNorm(), shark::MissingFeaturesKernelExpansion< InputType >::computeNorm(), shark::KernelExpansion< InputType >::eval(), shark::MissingFeaturesKernelExpansion< InputType >::eval(), shark::KernelExpansion< InputType >::kernel(), shark::KernelExpansion< InputType >::kernel(), shark::KernelExpansion< InputType >::read(), shark::KernelExpansion< InputType >::setKernel(), shark::KernelExpansion< InputType >::setStructure(), and shark::KernelExpansion< InputType >::write().