All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
lar_content::SupportVectorMachine Class Reference

SupportVectorMachine class. More...

#include <LArSupportVectorMachine.h>

Inheritance diagram for lar_content::SupportVectorMachine:
lar_content::MvaInterface

Classes

class  FeatureInfo
 FeatureInfo class. More...
 
class  SupportVectorInfo
 SupportVectorInfo class. More...
 

Public Types

enum  KernelType {
  USER_DEFINED = 0, LINEAR = 1, QUADRATIC = 2, CUBIC = 3,
  GAUSSIAN_RBF = 4
}
 KernelType enum. More...
 
typedef std::function< double(const
LArMvaHelper::MvaFeatureVector
&, const
LArMvaHelper::MvaFeatureVector
&, const double)> 
KernelFunction
 

Public Member Functions

 SupportVectorMachine ()
 Default constructor. More...
 
pandora::StatusCode Initialize (const std::string &parameterLocation, const std::string &svmName)
 Initialize the svm using a serialized model. More...
 
bool Classify (const LArMvaHelper::MvaFeatureVector &features) const
 Make a classification for a set of input features, based on the trained model. More...
 
double CalculateClassificationScore (const LArMvaHelper::MvaFeatureVector &features) const
 Calculate the classification score for a set of input features, based on the trained model. More...
 
double CalculateProbability (const LArMvaHelper::MvaFeatureVector &features) const
 Calculate the classification probability for a set of input features, based on the trained model. More...
 
bool IsInitialized () const
 Query whether this svm is initialized. More...
 
unsigned int GetNFeatures () const
 Get the number of features. More...
 
void SetKernelFunction (KernelFunction kernelFunction)
 Set the kernel function to use. More...
 
- Public Member Functions inherited from lar_content::MvaInterface
virtual ~MvaInterface ()=default
 Destructor. More...
 

Private Types

typedef std::vector
< SupportVectorInfo
SVInfoList
 
typedef std::vector< FeatureInfoFeatureInfoVector
 
typedef std::map< KernelType,
KernelFunction
KernelMap
 

Private Member Functions

void ReadXmlFile (const std::string &svmFileName, const std::string &svmName)
 Read the svm parameters from an xml file. More...
 
pandora::StatusCode ReadComponent (pandora::TiXmlElement *pCurrentXmlElement)
 Read the component at the current xml element. More...
 
pandora::StatusCode ReadMachine (const pandora::TiXmlHandle &currentHandle)
 Read the machine component at the current xml handle. More...
 
pandora::StatusCode ReadFeatures (const pandora::TiXmlHandle &currentHandle)
 Read the feature component at the current xml handle. More...
 
pandora::StatusCode ReadSupportVector (const pandora::TiXmlHandle &currentHandle)
 Read the support vector component at the current xml handle. More...
 
double CalculateClassificationScoreImpl (const LArMvaHelper::MvaFeatureVector &features) const
 Implementation method for calculating the classification score using the trained model. More...
 

Static Private Member Functions

static double QuadraticKernel (const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
 An inhomogeneous quadratic kernel. More...
 
static double CubicKernel (const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
 An inhomogeneous cubic kernel. More...
 
static double LinearKernel (const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
 A linear kernel. More...
 
static double GaussianRbfKernel (const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
 A gaussian RBF kernel. More...
 

Private Attributes

bool m_isInitialized
 Whether this svm has been initialized. More...
 
bool m_enableProbability
 Whether to enable probability calculations. More...
 
double m_probAParameter
 The first-order score coefficient for mapping to a probability using the logistic function. More...
 
double m_probBParameter
 The score offset parameter for mapping to a probability using the logistic function. More...
 
bool m_standardizeFeatures
 Whether to standardize the features. More...
 
unsigned int m_nFeatures
 The number of features. More...
 
double m_bias
 The bias term. More...
 
double m_scaleFactor
 The kernel scale factor. More...
 
SVInfoList m_svInfoList
 The list of SupportVectorInfo objects. More...
 
FeatureInfoVector m_featureInfoList
 The list of FeatureInfo objects. More...
 
KernelType m_kernelType
 The kernel type. More...
 
KernelFunction m_kernelFunction
 The kernel function. More...
 
KernelMap m_kernelMap
 Map from the kernel types to the kernel functions. More...
 

Detailed Description

SupportVectorMachine class.

Definition at line 30 of file LArSupportVectorMachine.h.

Member Typedef Documentation

Definition at line 162 of file LArSupportVectorMachine.h.

Definition at line 33 of file LArSupportVectorMachine.h.

Definition at line 164 of file LArSupportVectorMachine.h.

Definition at line 161 of file LArSupportVectorMachine.h.

Member Enumeration Documentation

Constructor & Destructor Documentation

lar_content::SupportVectorMachine::SupportVectorMachine ( )

Default constructor.

Definition at line 18 of file LArSupportVectorMachine.cc.

18  :
19  m_isInitialized(false),
20  m_enableProbability(false),
21  m_probAParameter(0.),
22  m_probBParameter(0.),
24  m_nFeatures(0),
25  m_bias(0.),
26  m_scaleFactor(1.),
static double CubicKernel(const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
An inhomogeneous cubic kernel.
bool m_enableProbability
Whether to enable probability calculations.
double m_scaleFactor
The kernel scale factor.
static double QuadraticKernel(const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
An inhomogeneous quadratic kernel.
static double LinearKernel(const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
A linear kernel.
bool m_standardizeFeatures
Whether to standardize the features.
KernelMap m_kernelMap
Map from the kernel types to the kernel functions.
KernelFunction m_kernelFunction
The kernel function.
KernelType m_kernelType
The kernel type.
double m_probAParameter
The first-order score coefficient for mapping to a probability using the logistic function...
unsigned int m_nFeatures
The number of features.
static double GaussianRbfKernel(const LArMvaHelper::MvaFeatureVector &supportVector, const LArMvaHelper::MvaFeatureVector &features, const double scaleFactor=1.)
A gaussian RBF kernel.
bool m_isInitialized
Whether this svm has been initialized.
double m_probBParameter
The score offset parameter for mapping to a probability using the logistic function.

Member Function Documentation

double lar_content::SupportVectorMachine::CalculateClassificationScore ( const LArMvaHelper::MvaFeatureVector features) const
inlinevirtual

Calculate the classification score for a set of input features, based on the trained model.

Parameters
featuresthe input features
Returns
the classification score

Implements lar_content::MvaInterface.

Definition at line 295 of file LArSupportVectorMachine.h.

296 {
297  return this->CalculateClassificationScoreImpl(features);
298 }
double CalculateClassificationScoreImpl(const LArMvaHelper::MvaFeatureVector &features) const
Implementation method for calculating the classification score using the trained model.
double lar_content::SupportVectorMachine::CalculateClassificationScoreImpl ( const LArMvaHelper::MvaFeatureVector features) const
private

Implementation method for calculating the classification score using the trained model.

Parameters
featuresthe vector of features
Returns
the classification score
double lar_content::SupportVectorMachine::CalculateProbability ( const LArMvaHelper::MvaFeatureVector features) const
inlinevirtual

Calculate the classification probability for a set of input features, based on the trained model.

Parameters
featuresthe input features
Returns
the classification probability

Implements lar_content::MvaInterface.

Definition at line 302 of file LArSupportVectorMachine.h.

303 {
304  if (!m_enableProbability)
305  {
306  std::cout << "LArSupportVectorMachine: cannot calculate probabilities for this SVM" << std::endl;
307  throw pandora::STATUS_CODE_NOT_INITIALIZED;
308  }
309 
310  // Use the logistic function to map the linearly-transformed score on the interval (-inf,inf) to a probability on [0,1] - the two free
311  // parameters in the linear transformation are trained such that the logistic map produces an accurate probability
312  const double scaledScore = m_probAParameter * this->CalculateClassificationScoreImpl(features) + m_probBParameter;
313 
314  return 1. / (1. + std::exp(scaledScore));
315 }
bool m_enableProbability
Whether to enable probability calculations.
double m_probAParameter
The first-order score coefficient for mapping to a probability using the logistic function...
double CalculateClassificationScoreImpl(const LArMvaHelper::MvaFeatureVector &features) const
Implementation method for calculating the classification score using the trained model.
double m_probBParameter
The score offset parameter for mapping to a probability using the logistic function.
BEGIN_PROLOG could also be cout
bool lar_content::SupportVectorMachine::Classify ( const LArMvaHelper::MvaFeatureVector features) const
inlinevirtual

Make a classification for a set of input features, based on the trained model.

Parameters
featuresthe input features
Returns
the predicted boolean class

Implements lar_content::MvaInterface.

Definition at line 288 of file LArSupportVectorMachine.h.

289 {
290  return (this->CalculateClassificationScoreImpl(features) > 0.);
291 }
double CalculateClassificationScoreImpl(const LArMvaHelper::MvaFeatureVector &features) const
Implementation method for calculating the classification score using the trained model.
double lar_content::SupportVectorMachine::CubicKernel ( const LArMvaHelper::MvaFeatureVector supportVector,
const LArMvaHelper::MvaFeatureVector features,
const double  scaleFactor = 1. 
)
inlinestaticprivate

An inhomogeneous cubic kernel.

Parameters
supportVectorthe support vector
featuresthe features
scalefactor the scale factor
Returns
result of the kernel operation

Definition at line 373 of file LArSupportVectorMachine.h.

375 {
376  const double denominator(scaleFactor * scaleFactor);
377  if (denominator < std::numeric_limits<double>::epsilon())
378  throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
379 
380  double total(0.);
381  for (unsigned int i = 0; i < features.size(); ++i)
382  total += supportVector.at(i).Get() * features.at(i).Get();
383 
384  total = total / denominator + 1.;
385  return total * total * total;
386 }
double lar_content::SupportVectorMachine::GaussianRbfKernel ( const LArMvaHelper::MvaFeatureVector supportVector,
const LArMvaHelper::MvaFeatureVector features,
const double  scaleFactor = 1. 
)
inlinestaticprivate

A gaussian RBF kernel.

Parameters
supportVectorthe support vector
featuresthe features
scalefactor the scale factor
Returns
result of the kernel operation

Definition at line 390 of file LArSupportVectorMachine.h.

392 {
393  double total(0.);
394  for (unsigned int i = 0; i < features.size(); ++i)
395  total += (supportVector.at(i).Get() - features.at(i).Get()) * (supportVector.at(i).Get() - features.at(i).Get());
396 
397  return std::exp(-scaleFactor * total);
398 }
unsigned int lar_content::SupportVectorMachine::GetNFeatures ( ) const
inline

Get the number of features.

Returns
the number of features

Definition at line 326 of file LArSupportVectorMachine.h.

327 {
328  return m_nFeatures;
329 }
unsigned int m_nFeatures
The number of features.
pandora::StatusCode lar_content::SupportVectorMachine::Initialize ( const std::string &  parameterLocation,
const std::string &  svmName 
)

Initialize the svm using a serialized model.

Parameters
parameterLocationthe location of the model
svmNamethe name of the model
Returns
success
bool lar_content::SupportVectorMachine::IsInitialized ( ) const
inline

Query whether this svm is initialized.

Returns
whether the model is initialized

Definition at line 319 of file LArSupportVectorMachine.h.

320 {
321  return m_isInitialized;
322 }
bool m_isInitialized
Whether this svm has been initialized.
double lar_content::SupportVectorMachine::LinearKernel ( const LArMvaHelper::MvaFeatureVector supportVector,
const LArMvaHelper::MvaFeatureVector features,
const double  scaleFactor = 1. 
)
inlinestaticprivate

A linear kernel.

Parameters
supportVectorthe support vector
featuresthe features
scalefactor the scale factor
Returns
result of the kernel operation

Definition at line 340 of file LArSupportVectorMachine.h.

342 {
343  const double denominator(scaleFactor * scaleFactor);
344  if (denominator < std::numeric_limits<double>::epsilon())
345  throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
346 
347  double total(0.);
348  for (unsigned int i = 0; i < features.size(); ++i)
349  total += supportVector.at(i).Get() * features.at(i).Get();
350 
351  return total / denominator;
352 }
double lar_content::SupportVectorMachine::QuadraticKernel ( const LArMvaHelper::MvaFeatureVector supportVector,
const LArMvaHelper::MvaFeatureVector features,
const double  scaleFactor = 1. 
)
inlinestaticprivate

An inhomogeneous quadratic kernel.

Parameters
supportVectorthe support vector
featuresthe features
scalefactor the scale factor
Returns
result of the kernel operation

Definition at line 356 of file LArSupportVectorMachine.h.

358 {
359  const double denominator(scaleFactor * scaleFactor);
360  if (denominator < std::numeric_limits<double>::epsilon())
361  throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
362 
363  double total(0.);
364  for (unsigned int i = 0; i < features.size(); ++i)
365  total += supportVector.at(i).Get() * features.at(i).Get();
366 
367  total = total / denominator + 1.;
368  return total * total;
369 }
pandora::StatusCode lar_content::SupportVectorMachine::ReadComponent ( pandora::TiXmlElement *  pCurrentXmlElement)
private

Read the component at the current xml element.

Parameters
pCurrentXmlElementaddress of the current xml element
Returns
success
pandora::StatusCode lar_content::SupportVectorMachine::ReadFeatures ( const pandora::TiXmlHandle &  currentHandle)
private

Read the feature component at the current xml handle.

Parameters
currentHandlethe current xml handle
Returns
success
pandora::StatusCode lar_content::SupportVectorMachine::ReadMachine ( const pandora::TiXmlHandle &  currentHandle)
private

Read the machine component at the current xml handle.

Parameters
currentHandlethe current xml handle
Returns
success
pandora::StatusCode lar_content::SupportVectorMachine::ReadSupportVector ( const pandora::TiXmlHandle &  currentHandle)
private

Read the support vector component at the current xml handle.

Parameters
currentHandlethe current xml handle
Returns
success
void lar_content::SupportVectorMachine::ReadXmlFile ( const std::string &  svmFileName,
const std::string &  svmName 
)
private

Read the svm parameters from an xml file.

Parameters
svmFileNamethe sml file name
svmNamethe name of the svm
void lar_content::SupportVectorMachine::SetKernelFunction ( KernelFunction  kernelFunction)
inline

Set the kernel function to use.

Parameters
kernelFunctionthe kernel function

Definition at line 333 of file LArSupportVectorMachine.h.

334 {
335  m_kernelFunction = std::move(kernelFunction);
336 }
KernelFunction m_kernelFunction
The kernel function.

Member Data Documentation

double lar_content::SupportVectorMachine::m_bias
private

The bias term.

Definition at line 174 of file LArSupportVectorMachine.h.

bool lar_content::SupportVectorMachine::m_enableProbability
private

Whether to enable probability calculations.

Definition at line 168 of file LArSupportVectorMachine.h.

FeatureInfoVector lar_content::SupportVectorMachine::m_featureInfoList
private

The list of FeatureInfo objects.

Definition at line 178 of file LArSupportVectorMachine.h.

bool lar_content::SupportVectorMachine::m_isInitialized
private

Whether this svm has been initialized.

Definition at line 166 of file LArSupportVectorMachine.h.

KernelFunction lar_content::SupportVectorMachine::m_kernelFunction
private

The kernel function.

Definition at line 181 of file LArSupportVectorMachine.h.

KernelMap lar_content::SupportVectorMachine::m_kernelMap
private

Map from the kernel types to the kernel functions.

Definition at line 182 of file LArSupportVectorMachine.h.

KernelType lar_content::SupportVectorMachine::m_kernelType
private

The kernel type.

Definition at line 180 of file LArSupportVectorMachine.h.

unsigned int lar_content::SupportVectorMachine::m_nFeatures
private

The number of features.

Definition at line 173 of file LArSupportVectorMachine.h.

double lar_content::SupportVectorMachine::m_probAParameter
private

The first-order score coefficient for mapping to a probability using the logistic function.

Definition at line 169 of file LArSupportVectorMachine.h.

double lar_content::SupportVectorMachine::m_probBParameter
private

The score offset parameter for mapping to a probability using the logistic function.

Definition at line 170 of file LArSupportVectorMachine.h.

double lar_content::SupportVectorMachine::m_scaleFactor
private

The kernel scale factor.

Definition at line 175 of file LArSupportVectorMachine.h.

bool lar_content::SupportVectorMachine::m_standardizeFeatures
private

Whether to standardize the features.

Definition at line 172 of file LArSupportVectorMachine.h.

SVInfoList lar_content::SupportVectorMachine::m_svInfoList
private

The list of SupportVectorInfo objects.

Definition at line 177 of file LArSupportVectorMachine.h.


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