19 #include "drwnFeatureMaps.h"
20 #include "drwnClassifier.h"
21 #include "drwnOptimizer.h"
42 const vector<vector<double> > *_features;
43 const vector<int> *_targets;
44 const vector<double> *_weights;
56 virtual const char *
type()
const {
return "drwnMultiClassLogistic"; }
59 virtual bool save(drwnXMLNode& xml)
const;
60 virtual bool load(drwnXMLNode& xml);
65 virtual double train(
const vector<vector<double> >& features,
66 const vector<int>& targets);
67 virtual double train(
const vector<vector<double> >& features,
68 const vector<int>& targets,
const vector<double>& weights);
72 virtual void getClassScores(
const vector<double>& features,
73 vector<double>& outputScores)
const = 0;
77 double objective(
const double *x)
const;
78 void gradient(
const double *x,
double *df)
const;
79 virtual double objectiveAndGradient(
const double *x,
double *df)
const = 0;
91 template<
class FeatureMap = drwnBiasJo
intFeatureMap>
111 virtual void initialize(
unsigned n,
unsigned k = 2);
115 virtual void getClassScores(
const vector<double>& features,
116 vector<double>& outputScores)
const;
119 virtual double objectiveAndGradient(
const double *x,
double *df)
const;
131 template<
class FeatureMap>
135 const FeatureMap phi(_nFeatures, _nClasses);
136 const int m = phi.numParameters();
140 _theta = VectorXd::Zero(phi.numParameters());
144 template<
class FeatureMap>
146 vector<double>& outputScores)
const
148 DRWN_ASSERT((
int)features.size() == _nFeatures);
151 vector<double> t(_theta.rows());
152 Eigen::Map<VectorXd>(&t[0], t.size()) = _theta;
154 const FeatureMap phi(_nFeatures, _nClasses);
155 outputScores.resize(_nClasses);
156 for (
int k = 0; k < _nClasses; k++) {
157 outputScores[k] = phi.dot(t, features, k);
161 template<
class FeatureMap>
164 double negLogL = 0.0;
167 const FeatureMap phi(_nFeatures, _nClasses);
168 vector<double> p(_nClasses);
170 const vector<double> vx(x, x + _n);
171 vector<double> vdf(_n, 0.0);
173 for (
unsigned n = 0; n < _features->size(); n++) {
174 if ((*_targets)[n] < 0)
continue;
175 double alpha = (_weights == NULL) ? 1.0 : (*_weights)[n];
178 double maxValue = 0.0;
179 for (
int k = 0; k < _nClasses; k++) {
180 p[k] = phi.dot(vx, (*_features)[n], k);
181 maxValue = std::max(maxValue, p[k]);
186 for (vector<double>::iterator it = p.begin(); it != p.end(); ++it) {
187 Z += (*it = exp(*it - maxValue));
191 negLogL -= alpha * log(p[(*_targets)[n]] / Z);
195 p[(*_targets)[n]] -= Z;
196 for (
int k = 0; k < _nClasses; k++) {
197 phi.mac(vdf, (*_features)[n], alpha * p[k] / Z, k);
201 memcpy((
void *)df, (
void *)&vdf[0], _n *
sizeof(
double));
203 if (numTerms == 0)
return 0.0;
204 negLogL /= (double)numTerms;
205 Eigen::Map<VectorXd>(df, _n) /= (
double)numTerms;
208 switch (_regularizer) {
211 double weightNorm = 0.0;
212 for (
unsigned i = 0; i < _n; i++) {
213 weightNorm += x[i] * x[i];
214 df[i] += _lambda * x[i];
217 negLogL += 0.5 * _lambda * weightNorm;
224 for (
unsigned i = 0; i < _n; i++) {
225 negLogL += _lambda * drwn::huberFunctionAndDerivative(x[i], &dh, 1.0e-3);
226 df[i] += _lambda * dh;
232 DRWN_LOG_ERROR(
"unsupported regularizer " << _regularizer);
virtual double train(const drwnClassifierDataset &dataset)=0
train the parameters of the classifier from a drwnClassifierDataset object
virtual void getClassScores(const vector< double > &features, vector< double > &outputScores) const
compute the unnormalized log-probability for a single feature vector
Definition: drwnMultiClassLogistic.h:145
virtual void getClassScores(const vector< double > &features, vector< double > &outputScores) const =0
compute the unnormalized log-probability for a single feature vector
virtual const char * type() const
returns object type as a string (e.g., Foo::type() { return "Foo"; })
Definition: drwnMultiClassLogistic.h:56
drwnTMultiClassLogistic()
default constructor
Definition: drwnMultiClassLogistic.h:95
virtual void initialize(unsigned n, unsigned k=2)
initialize the classifier object for n features and k classes
Definition: drwnClassifier.cpp:47
static double REG_STRENGTH
default strength of regularizer (used during construction)
Definition: drwnMultiClassLogistic.h:32
drwnTMultiClassLogistic(unsigned n, unsigned k=2)
construct a k-class logistic classifier for data of dimension n
Definition: drwnMultiClassLogistic.h:97
drwnTMultiClassLogistic drwnMultiClassLogistic
Conveinience type declaration for multi-class logistic classifier with default feature mapping...
Definition: drwnMultiClassLogistic.h:127
virtual double objectiveAndGradient(const double *x, double *df) const
returns value of objective function and populates gradient df at point x
Definition: drwnMultiClassLogistic.h:162
Common functionality for drwnMultiClassLogistic.
Definition: drwnMultiClassLogistic.h:30
Interface for solving large-scale unconstrained optimization problems using L-BFGS.
Definition: drwnOptimizer.h:68
Implements the interface for a generic machine learning classifier.
Definition: drwnClassifier.h:31
drwnTMultiClassLogistic(const drwnTMultiClassLogistic< FeatureMap > &c)
copy constructor
Definition: drwnMultiClassLogistic.h:100
VectorXd _theta
joint feature map weights
Definition: drwnMultiClassLogistic.h:36
Implements a multi-class logistic classifier templated on a drwnJointFeatureMap.
Definition: drwnMultiClassLogistic.h:92
virtual void initialize(unsigned n, unsigned k=2)
initialize the classifier object for n features and k classes
Definition: drwnMultiClassLogistic.h:132
static int MAX_ITERATIONS
maximum number of training iterations
Definition: drwnMultiClassLogistic.h:33
Implements a cacheable dataset containing feature vectors, labels and optional weights.
Definition: drwnDataset.h:43
virtual void getClassScores(const vector< double > &features, vector< double > &outputScores) const =0
compute the unnormalized log-probability for a single feature vector
int _regularizer
regularization option
Definition: drwnMultiClassLogistic.h:37
double _lambda
regularization strength
Definition: drwnMultiClassLogistic.h:38
virtual drwnTMultiClassLogistic< FeatureMap > * clone() const
returns a copy of the class usually implemented as virtual Foo* clone() { return new Foo(*this); } ...
Definition: drwnMultiClassLogistic.h:106