diff options
author | Patrick Simianer <p@simianer.de> | 2011-09-08 00:06:52 +0200 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2011-09-23 19:13:58 +0200 |
commit | 0269777fc54bc554c12107bdd5498f743df2a1ce (patch) | |
tree | 05032f88088c2154b4c0ce815bb176ac49dc9b7e /dtrain/test/log_reg/log_reg.cc | |
parent | bcf45fc73bd855a3003dee7a8a0b7551eeb0523b (diff) |
a lot of stuff, fast_sparse_vector, perceptron, removed sofia, sample [...]
Diffstat (limited to 'dtrain/test/log_reg/log_reg.cc')
-rw-r--r-- | dtrain/test/log_reg/log_reg.cc | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/dtrain/test/log_reg/log_reg.cc b/dtrain/test/log_reg/log_reg.cc new file mode 100644 index 00000000..ec2331fe --- /dev/null +++ b/dtrain/test/log_reg/log_reg.cc @@ -0,0 +1,39 @@ +#include "log_reg.h" + +#include <vector> +#include <cmath> + +#include "sparse_vector.h" + +using namespace std; + +double LogisticRegression::ObjectiveAndGradient(const SparseVector<double>& x, + const vector<TrainingInstance>& training_instances, + SparseVector<double>* g) const { + double cll = 0; + for (int i = 0; i < training_instances.size(); ++i) { + const double dotprod = training_instances[i].x_feature_map.dot(x); // TODO no bias, if bias, add x[0] + double lp_false = dotprod; + double lp_true = -dotprod; + if (0 < lp_true) { + lp_true += log1p(exp(-lp_true)); + lp_false = log1p(exp(lp_false)); + } else { + lp_true = log1p(exp(lp_true)); + lp_false += log1p(exp(-lp_false)); + } + lp_true *= -1; + lp_false *= -1; + if (training_instances[i].y) { // true label + cll -= lp_true; + (*g) -= training_instances[i].x_feature_map * exp(lp_false); + // (*g)[0] -= exp(lp_false); // bias + } else { // false label + cll -= lp_false; + (*g) += training_instances[i].x_feature_map * exp(lp_true); + // g += corpus[i].second * exp(lp_true); + } + } + return cll; +} + |