diff options
author | John Langford <jl@hunch.net> | 2009-08-01 00:19:24 +0400 |
---|---|---|
committer | John Langford <jl@hunch.net> | 2009-08-01 00:19:24 +0400 |
commit | f30c74f7d36a3b3691d1e9a598163dcecb87b493 (patch) | |
tree | d531146bcef68de85930e6e65c31ad4c868d3bcd /loss_functions.cc | |
parent | 8b4959c461235e733cef9ccb851c3834c3155c31 (diff) |
Initial release of version 3.10. I've incorporated some of the
Gordon's changes.
Diffstat (limited to 'loss_functions.cc')
-rw-r--r-- | loss_functions.cc | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/loss_functions.cc b/loss_functions.cc new file mode 100644 index 00000000..6712a859 --- /dev/null +++ b/loss_functions.cc @@ -0,0 +1,110 @@ +/* +Copyright (c) 2009 Yahoo! Inc. All rights reserved. The copyrights +embodied in the content of this file are licensed under the BSD +(revised) open source license + */ + +#include "loss_functions.h" +#include<math.h> +#include<iostream> + +class squaredloss : public loss_function { +public: + squaredloss() { + + } + + double getLoss(double prediction, double label) { + double example_loss = (prediction - label) * (prediction - label); + return example_loss; + } + + double getUpdate(double prediction, double label) { + return (label - prediction); + } +}; + +class hingeloss : public loss_function { +public: + hingeloss() { + + } + + double getLoss(double prediction, double label) { + double y = (label == 0.0) ? -1.0 : 1.0; + double e = 1 - y*prediction; + return (e > 0) ? e : 0; + } + + double getUpdate(double prediction, double label) { + double y = (label == 0.0) ? -1.0 : 1.0; + if(prediction == y) return 0; + if((1 - y*prediction) > 0) + return y; + else + return 0; + } +}; + +class logloss : public loss_function { +public: + logloss() { + + } + + double getLoss(double prediction, double label) { + double y = (label == 0.0) ? -1.0 : 1.0; + return log(1 + exp(-y * prediction)); + } + + double getUpdate(double prediction, double label) { + double y = (label == 0.0) ? -1.0 : 1.0; + double d = exp(-y * prediction); + return y * d / (1 + d); + } +}; + +class quantilesloss : public loss_function { +public: + quantilesloss(double &tau_) : tau(tau_) { + } + + double getLoss(double prediction, double label) { + double y = (label == 0.0) ? -1.0 : 1.0; + double e = y - prediction; + if(e > 0) { + return tau * e; + } else { + return -(1 - tau) * e; + } + + } + + double getUpdate(double prediction, double label) { + double y = (label == 0.0) ? -1.0 : 1.0; + double e = y - prediction; + if(e == 0) return 0; + if(e > 0) { + return tau; + } else { + return -(1 - tau); + } + } + + double tau; +}; + +loss_function* getLossFunction(string funcName, double function_parameter) { + if(funcName.compare("squaredloss") == 0) { + return new squaredloss(); + } else if(funcName.compare("hingeloss") == 0) { + return new hingeloss(); + } else if(funcName.compare("logloss") == 0) { + return new logloss(); + } else if(funcName.compare("quantilesloss") == 0 || funcName.compare("pinballloss") == 0) { + return new quantilesloss(function_parameter); + } else { + return NULL; + } +} + |