Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnFeatureTransform.h
1 /******************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnFeatureTransform.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <cstdlib>
16 #include <vector>
17 
18 #include "drwnBase.h"
19 #include "drwnFeatureMaps.h"
20 
21 using namespace std;
22 using namespace Eigen;
23 
24 // drwnFeatureTransform -----------------------------------------------------
30 
32  protected:
33  int _nFeatures;
34  bool _bValid;
35 
36  public:
41  virtual ~drwnFeatureTransform() {
42  // do nothing
43  }
44 
45  // access functions
48  int numFeatures() const { return _nFeatures; }
50  virtual bool valid() const { return _bValid; }
51 
52  // clone operation from (drwnStdObjIface)
53  virtual drwnFeatureTransform *clone() const = 0;
54 
55  // i/o
57  virtual void clear() { _nFeatures = 0; _bValid = false; }
58  virtual bool save(drwnXMLNode& xml) const;
59  virtual bool load(drwnXMLNode& xml);
60 
61  // training methods (implemented in derived classes)
62 
63  // evaluation (in-place and copy)
65  virtual void transform(vector<double>& x) const;
67  virtual void transform(const vector<double>& x, vector<double>& y) const = 0;
69  virtual void transform(vector<vector<double> >& x) const;
71  virtual void transform(const vector<vector<double> >& x,
72  vector<vector<double> >& y) const;
73 
74  // evaluation with pre-processing (in-place and copy)
76  virtual void transform(vector<double>& x, const drwnFeatureTransform& xform) const;
78  virtual void transform(const vector<double>& x, vector<double>& y,
79  const drwnFeatureTransform& xform) const;
81  virtual void transform(vector<vector<double> >& x, const drwnFeatureTransform& xform) const;
84  virtual void transform(const vector<vector<double> >& x,
85  vector<vector<double> >& y, const drwnFeatureTransform& xform) const;
86 };
87 
88 // drwnTFeatureMapTranform --------------------------------------------------
90 
91 template<class FeatureMap>
93  public:
94  drwnTFeatureMapTransform() : drwnFeatureTransform() { /* do nothing */ }
96  drwnFeatureTransform(t) { /* do nothing */ }
97  ~drwnTFeatureMapTransform() { /* do nothing */ }
98 
99  // access
100  const char *type() const { return "drwnTFeatureMapTransform"; }
102  return new drwnTFeatureMapTransform<FeatureMap>(*this);
103  }
104 
105  bool valid() const { return true; }
106 
107  // evaluation
108  void transform(const vector<double>& x, vector<double>& y) const {
109  const FeatureMap phi(x.size());
110  y = phi(x);
111  }
112 };
113 
114 // drwnUnsupervisedTransform ------------------------------------------------
117 
119  public:
124  virtual ~drwnUnsupervisedTransform() {
125  // do nothing
126  }
127 
128  // clone operation from (drwnStdObjIface)
129  virtual drwnUnsupervisedTransform *clone() const = 0;
130 
131  // training
134  virtual double train(const vector<vector<double> >& features) = 0;
137  virtual double train(const vector<vector<double> >& features,
138  const vector<double>& weights);
139 
144  virtual double train(const vector<vector<double> >& features,
145  const drwnFeatureTransform& xform);
150  virtual double train(const vector<vector<double> >& features,
151  const vector<double>& weights, const drwnFeatureTransform& xform);
152 };
153 
154 // drwnSupervisedTransform --------------------------------------------------
157 
159  public:
164  virtual ~drwnSupervisedTransform() {
165  // do nothing
166  }
167 
168  // clone operation from (drwnStdObjIface)
169  virtual drwnSupervisedTransform *clone() const = 0;
170 
171  // training
174  virtual double train(const vector<vector<double> >& features,
175  const vector<int>& labels) = 0;
178  virtual double train(const vector<vector<double> >& features,
179  const vector<int>& labels, const vector<double>& weights);
180 
185  virtual double train(const vector<vector<double> >& features,
186  const vector<int>& labels, const drwnFeatureTransform& xform);
191  virtual double train(const vector<vector<double> >& features,
192  const vector<int>& labels, const vector<double>& weights,
193  const drwnFeatureTransform& xform);
194 };
195 
196 // drwnFeatureTransformFactory ----------------------------------------------
199 
200 template <>
202  static void staticRegistration();
203 };
204 
drwnTFeatureMapTransform< FeatureMap > * clone() const
returns a copy of the class usually implemented as virtual Foo* clone() { return new Foo(*this); } ...
Definition: drwnFeatureTransform.h:101
Some classes may provide default factory registration (e.g., built-in classes such as drwnClassifier ...
Definition: drwnFactory.h:32
Provides an abstract interface for dynamic properties.
Definition: drwnProperties.h:338
Templated factory for creating or cloning objects for a particular base class.
Definition: drwnFactory.h:59
bool valid() const
returns true if the feature transform object is initialized (and trained)
Definition: drwnFeatureTransform.h:105
int numFeatures() const
returns the length of the feature vector expected by the feature transform object (or zero for arbitr...
Definition: drwnFeatureTransform.h:48
Implements the interface for a generic feature transforms possibly with learned parameters, e.g., PCA (unsupervised) or LDA (supervised).
Definition: drwnFeatureTransform.h:31
void transform(const vector< double > &x, vector< double > &y) const
transforms feature vector x into feature vector y
Definition: drwnFeatureTransform.h:108
Implements interface for unsupervised feature transforms (i.e, without class labels).
Definition: drwnFeatureTransform.h:118
virtual bool valid() const
returns true if the feature transform object is initialized (and trained)
Definition: drwnFeatureTransform.h:50
bool _bValid
true if transform parameters are trained or loaded
Definition: drwnFeatureTransform.h:34
virtual void clear()
clears the parameters of the feature transform object
Definition: drwnFeatureTransform.h:57
Helper feature transformation based on a drwnFeatureMap.
Definition: drwnFeatureTransform.h:92
Implements interface for supervised feature transforms (i.e., with class labels). ...
Definition: drwnFeatureTransform.h:158
const char * type() const
returns object type as a string (e.g., Foo::type() { return "Foo"; })
Definition: drwnFeatureTransform.h:100
int _nFeatures
number of (input) features
Definition: drwnFeatureTransform.h:33
standard Darwin object interface (cloneable and writeable)
Definition: drwnInterfaces.h:72