diff options
author | Patrick Simianer <simianer@cl.uni-heidelberg.de> | 2012-06-26 14:47:46 +0200 |
---|---|---|
committer | Patrick Simianer <simianer@cl.uni-heidelberg.de> | 2012-06-26 14:47:46 +0200 |
commit | 3c03e716a8638163a820dbecf652c772e6d651f2 (patch) | |
tree | 255e2aa075b9a4c2a2fb1c077e899afdc5654c19 /training/risk.cc | |
parent | d04c516536db996e5fe5b94b8e5dea4ce2e04b4a (diff) | |
parent | e1b85b4f02045c62400b9c755883e7fae14557f6 (diff) |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'training/risk.cc')
-rw-r--r-- | training/risk.cc | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/training/risk.cc b/training/risk.cc new file mode 100644 index 00000000..d5a12cfd --- /dev/null +++ b/training/risk.cc @@ -0,0 +1,45 @@ +#include "risk.h" + +#include "prob.h" +#include "candidate_set.h" +#include "ns.h" + +using namespace std; + +namespace training { + +// g = \sum_e p(e|f) * loss(e) * (phi(e,f) - E[phi(e,f)]) +double CandidateSetRisk::operator()(const vector<double>& params, + SparseVector<double>* g) const { + prob_t z; + for (unsigned i = 0; i < cands_.size(); ++i) { + const prob_t u(cands_[i].fmap.dot(params), init_lnx()); + z += u; + } + const double log_z = log(z); + + SparseVector<double> exp_feats; + if (g) { + for (unsigned i = 0; i < cands_.size(); ++i) { + const double log_prob = cands_[i].fmap.dot(params) - log_z; + const double prob = exp(log_prob); + exp_feats += cands_[i].fmap * prob; + } + } + + double risk = 0; + for (unsigned i = 0; i < cands_.size(); ++i) { + const double log_prob = cands_[i].fmap.dot(params) - log_z; + const double prob = exp(log_prob); + const double cost = metric_.IsErrorMetric() ? metric_.ComputeScore(cands_[i].eval_feats) + : 1.0 - metric_.ComputeScore(cands_[i].eval_feats); + const double r = prob * cost; + risk += r; + if (g) (*g) += (cands_[i].fmap - exp_feats) * r; + } + return risk; +} + +} + + |