00001 /* 00002 Copyright (c) 2009 Yahoo! Inc. All rights reserved. The copyrights 00003 embodied in the content of this file are licensed under the BSD 00004 (revised) open source license. 00005 00006 Copyright (c) 2011 Berlin Institute of Technology and Max-Planck-Society. 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 Modifications (w) 2011 Shashwat Lal Das 00014 Modifications (w) 2012 Fernando José Iglesias García 00015 */ 00016 00017 #include <shogun/loss/HingeLoss.h> 00018 #include <shogun/mathematics/Math.h> 00019 00020 using namespace shogun; 00021 00022 float64_t CHingeLoss::loss(float64_t prediction, float64_t label) 00023 { 00024 float64_t e = 1 - label * prediction; 00025 00026 return (e > 0) ? e : 0; 00027 } 00028 00029 float64_t CHingeLoss::loss(float64_t z) 00030 { 00031 return CMath::max(0.0, z); 00032 } 00033 00034 float64_t CHingeLoss::first_derivative(float64_t prediction, float64_t label) 00035 { 00036 return (label * prediction >= label * label) ? 0 : -label; 00037 } 00038 00039 float64_t CHingeLoss::first_derivative(float64_t z) 00040 { 00041 return z > 0.0 ? 1.0 : 0.0; 00042 } 00043 00044 float64_t CHingeLoss::second_derivative(float64_t prediction, float64_t label) 00045 { 00046 return 0.; 00047 } 00048 00049 float64_t CHingeLoss::second_derivative(float64_t z) 00050 { 00051 return 0; 00052 } 00053 00054 float64_t CHingeLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) 00055 { 00056 if (label * prediction >= label * label) 00057 return 0; 00058 float64_t err = (label*label - label*prediction)/(label * label); 00059 float64_t normal = eta_t; 00060 return label * (normal < err ? normal : err)/norm; 00061 } 00062 00063 float64_t CHingeLoss::get_square_grad(float64_t prediction, float64_t label) 00064 { 00065 return first_derivative(prediction, label); 00066 }