SoftNearestNeighborClassifier returns a probabilistic classification by looking at the k nearest neighbors. More...
#include <shark/Models/SoftNearestNeighborClassifier.h>
Inheritance diagram for shark::SoftNearestNeighborClassifier< InputType >:Public Types | |
| enum | DistanceWeights { UNIFORM, ONE_OVER_DISTANCE } |
| Type of distance-based weights. More... | |
| typedef AbstractNearestNeighbors< InputType, unsigned int > | NearestNeighbors |
| 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. More... | |
| typedef RealVector | OutputType |
| Defines the output type of the model. 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< Feature > | Features |
| typedef TypedFeatureNotAvailableException< Feature > | FeatureNotAvailableException |
Public Member Functions | |
| SoftNearestNeighborClassifier (NearestNeighbors const *algorithm, unsigned int neighbors=3) | |
| Constructor. More... | |
| SoftNearestNeighborClassifier (NearestNeighbors const *algorithm, std::size_t numClasses, unsigned int neighbors) | |
| Constructor. More... | |
| std::string | name () const |
| From INameable: return the class name. More... | |
| unsigned int | neighbors () const |
| return the number of neighbors More... | |
| void | setNeighbors (unsigned int neighbors) |
| set the number of neighbors More... | |
| DistanceWeights | getDistanceWeightType () const |
| query the way distances enter as weights More... | |
| void | setDistanceWeightType (DistanceWeights dw) |
| set the way distances enter as weights More... | |
| virtual RealVector | parameterVector () const |
| get internal parameters of the model More... | |
| virtual void | setParameterVector (RealVector const &newParameters) |
| set internal parameters of the model More... | |
| virtual std::size_t | numberOfParameters () const |
| return the size of the parameter vector More... | |
| boost::shared_ptr< State > | createState () const |
| Creates an internal state of the model. More... | |
| void | eval (BatchInputType const &patterns, BatchOutputType &outputs) const |
| soft k-nearest-neighbor prediction More... | |
| void | eval (BatchInputType const &patterns, BatchOutputType &outputs, State &state) const |
| Standard interface for evaluating the response of the model to a batch of patterns. More... | |
| void | read (InArchive &archive) |
| from ISerializable, reads a model from an archive More... | |
| void | write (OutArchive &archive) const |
| from ISerializable, writes a model to an archive More... | |
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. More... | |
| bool | hasSecondParameterDerivative () const |
| Returns true when the second parameter derivative is implemented. More... | |
| bool | hasFirstInputDerivative () const |
| Returns true when the first input derivative is implemented. More... | |
| bool | hasSecondInputDerivative () const |
| Returns true when the second parameter derivative is implemented. More... | |
| bool | isSequential () const |
| virtual void | eval (InputType const &pattern, OutputType &output) const |
| Standard interface for evaluating the response of the model to a single pattern. More... | |
| Data< OutputType > | operator() (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 | weightedParameterDerivative (BatchInputType const &pattern, BatchOutputType const &coefficients, State const &state, RealVector &derivative) const |
| calculates the weighted sum of derivatives w.r.t the parameters. More... | |
| virtual void | weightedParameterDerivative (BatchInputType const &pattern, BatchOutputType const &coefficients, Batch< RealMatrix >::type const &errorHessian, State const &state, RealVector &derivative, RealMatrix &hessian) const |
| calculates the weighted sum of derivatives w.r.t the parameters More... | |
| virtual void | weightedInputDerivative (BatchInputType const &pattern, BatchOutputType const &coefficients, State const &state, BatchInputType &derivative) const |
| calculates the weighted sum of derivatives w.r.t the inputs More... | |
| virtual void | weightedInputDerivative (BatchInputType const &pattern, BatchOutputType const &coefficients, typename Batch< RealMatrix >::type const &errorHessian, State const &state, RealMatrix &derivative, Batch< RealMatrix >::type &hessian) const |
| calculates the weighted sum of derivatives w.r.t the inputs More... | |
| virtual void | weightedDerivatives (BatchInputType const &patterns, BatchOutputType const &coefficients, State const &state, RealVector ¶meterDerivative, BatchInputType &inputDerivative) const |
| calculates weighted input and parameter derivative at the same time More... | |
Public Member Functions inherited from shark::IParameterizable | |
| 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 () | |
Protected Attributes | |
| NearestNeighbors const * | m_algorithm |
| std::size_t | m_classes |
| number of classes More... | |
| unsigned int | m_neighbors |
| number of neighbors to be taken into account More... | |
| DistanceWeights | m_distanceWeights |
| type of distance-based weights computation More... | |
Protected Attributes inherited from shark::AbstractModel< InputType, RealVector > | |
| Features | m_features |
SoftNearestNeighborClassifier returns a probabilistic classification by looking at the k nearest neighbors.
For a given number C of classes, which has to be specified a priori, a C-dimensional real-valued vector is returned for each query point. Each component corresponds to a class and contains the fraction of neighbors among the K nearest neighbors that belong to the particular class.
Definition at line 54 of file SoftNearestNeighborClassifier.h.
| typedef base_type::BatchInputType shark::SoftNearestNeighborClassifier< InputType >::BatchInputType |
Definition at line 59 of file SoftNearestNeighborClassifier.h.
| typedef base_type::BatchOutputType shark::SoftNearestNeighborClassifier< InputType >::BatchOutputType |
Definition at line 60 of file SoftNearestNeighborClassifier.h.
| typedef AbstractNearestNeighbors<InputType,unsigned int> shark::SoftNearestNeighborClassifier< InputType >::NearestNeighbors |
Definition at line 57 of file SoftNearestNeighborClassifier.h.
| enum shark::SoftNearestNeighborClassifier::DistanceWeights |
Type of distance-based weights.
| Enumerator | |
|---|---|
| UNIFORM | uniform (= no) distance-based weights |
| ONE_OVER_DISTANCE | weight each neighbor's label with 1/distance |
Definition at line 63 of file SoftNearestNeighborClassifier.h.
|
inline |
Constructor.
| algorithm | the used algorithm for nearest neighbor search |
| neighbors | number of neighbors |
Definition at line 73 of file SoftNearestNeighborClassifier.h.
|
inline |
Constructor.
| algorithm | the used algorithm for nearest neighbor search |
| numClasses | number of classes (given explicitly, not derived from the training data) |
| neighbors | number of neighbors |
Definition at line 85 of file SoftNearestNeighborClassifier.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< InputType, RealVector >.
Definition at line 136 of file SoftNearestNeighborClassifier.h.
|
inlinevirtual |
soft k-nearest-neighbor prediction
Reimplemented from shark::AbstractModel< InputType, RealVector >.
Definition at line 141 of file SoftNearestNeighborClassifier.h.
References shark::AbstractNearestNeighbors< InputType, LabelType >::getNeighbors(), shark::SoftNearestNeighborClassifier< InputType >::m_algorithm, shark::SoftNearestNeighborClassifier< InputType >::m_classes, shark::SoftNearestNeighborClassifier< InputType >::m_distanceWeights, shark::SoftNearestNeighborClassifier< InputType >::m_neighbors, shark::SoftNearestNeighborClassifier< InputType >::neighbors(), shark::blas::row(), shark::size(), shark::SoftNearestNeighborClassifier< InputType >::UNIFORM, and w.
Referenced by shark::SoftNearestNeighborClassifier< InputType >::eval().
|
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 >.
Definition at line 168 of file SoftNearestNeighborClassifier.h.
References shark::SoftNearestNeighborClassifier< InputType >::eval(), and shark::AbstractModel< InputType, RealVector >::eval().
|
inline |
query the way distances enter as weights
Definition at line 108 of file SoftNearestNeighborClassifier.h.
References shark::SoftNearestNeighborClassifier< InputType >::m_distanceWeights.
|
inlinevirtual |
From INameable: return the class name.
Reimplemented from shark::INameable.
Definition at line 93 of file SoftNearestNeighborClassifier.h.
|
inline |
return the number of neighbors
Definition at line 98 of file SoftNearestNeighborClassifier.h.
References shark::SoftNearestNeighborClassifier< InputType >::m_neighbors.
Referenced by shark::SoftNearestNeighborClassifier< InputType >::eval(), and shark::SoftNearestNeighborClassifier< InputType >::setNeighbors().
|
inlinevirtual |
return the size of the parameter vector
Reimplemented from shark::IParameterizable.
Definition at line 132 of file SoftNearestNeighborClassifier.h.
|
inlinevirtual |
get internal parameters of the model
Reimplemented from shark::IParameterizable.
Definition at line 116 of file SoftNearestNeighborClassifier.h.
References shark::SoftNearestNeighborClassifier< InputType >::m_neighbors, and shark::blas::parameters().
|
inlinevirtual |
from ISerializable, reads a model from an archive
Reimplemented from shark::AbstractModel< InputType, RealVector >.
Definition at line 175 of file SoftNearestNeighborClassifier.h.
References shark::SoftNearestNeighborClassifier< InputType >::m_classes, and shark::SoftNearestNeighborClassifier< InputType >::m_neighbors.
|
inline |
set the way distances enter as weights
Definition at line 112 of file SoftNearestNeighborClassifier.h.
References shark::SoftNearestNeighborClassifier< InputType >::m_distanceWeights.
|
inline |
set the number of neighbors
Definition at line 103 of file SoftNearestNeighborClassifier.h.
References shark::SoftNearestNeighborClassifier< InputType >::m_neighbors, and shark::SoftNearestNeighborClassifier< InputType >::neighbors().
|
inlinevirtual |
set internal parameters of the model
Reimplemented from shark::IParameterizable.
Definition at line 123 of file SoftNearestNeighborClassifier.h.
References shark::SoftNearestNeighborClassifier< InputType >::m_neighbors, and SHARK_CHECK.
|
inlinevirtual |
from ISerializable, writes a model to an archive
Reimplemented from shark::AbstractModel< InputType, RealVector >.
Definition at line 181 of file SoftNearestNeighborClassifier.h.
References shark::SoftNearestNeighborClassifier< InputType >::m_classes, and shark::SoftNearestNeighborClassifier< InputType >::m_neighbors.
|
protected |
Definition at line 187 of file SoftNearestNeighborClassifier.h.
Referenced by shark::SoftNearestNeighborClassifier< InputType >::eval().
|
protected |
number of classes
Definition at line 190 of file SoftNearestNeighborClassifier.h.
Referenced by shark::SoftNearestNeighborClassifier< InputType >::eval(), shark::SoftNearestNeighborClassifier< InputType >::read(), and shark::SoftNearestNeighborClassifier< InputType >::write().
|
protected |
type of distance-based weights computation
Definition at line 196 of file SoftNearestNeighborClassifier.h.
Referenced by shark::SoftNearestNeighborClassifier< InputType >::eval(), shark::SoftNearestNeighborClassifier< InputType >::getDistanceWeightType(), and shark::SoftNearestNeighborClassifier< InputType >::setDistanceWeightType().
|
protected |
number of neighbors to be taken into account
Definition at line 193 of file SoftNearestNeighborClassifier.h.
Referenced by shark::SoftNearestNeighborClassifier< InputType >::eval(), shark::SoftNearestNeighborClassifier< InputType >::neighbors(), shark::SoftNearestNeighborClassifier< InputType >::parameterVector(), shark::SoftNearestNeighborClassifier< InputType >::read(), shark::SoftNearestNeighborClassifier< InputType >::setNeighbors(), shark::SoftNearestNeighborClassifier< InputType >::setParameterVector(), and shark::SoftNearestNeighborClassifier< InputType >::write().