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/mathematics/Math.h> 00018 #include <shogun/loss/SquaredLoss.h> 00019 00020 using namespace shogun; 00021 00022 float64_t CSquaredLoss::loss(float64_t prediction, float64_t label) 00023 { 00024 return (prediction - label) * (prediction - label); 00025 } 00026 00027 float64_t CSquaredLoss::loss(float64_t z) 00028 { 00029 return z*z; 00030 } 00031 00032 float64_t CSquaredLoss::first_derivative(float64_t prediction, float64_t label) 00033 { 00034 return 2. * (prediction - label); 00035 } 00036 00037 float64_t CSquaredLoss::first_derivative(float64_t z) 00038 { 00039 return 2. * z; 00040 } 00041 00042 float64_t CSquaredLoss::second_derivative(float64_t prediction, float64_t label) 00043 { 00044 return 2; 00045 } 00046 00047 float64_t CSquaredLoss::second_derivative(float64_t z) 00048 { 00049 return 2; 00050 } 00051 00052 float64_t CSquaredLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) 00053 { 00054 if (eta_t < 1e-6) 00055 { 00056 /* When exp(-eta_t)~= 1 we replace 1-exp(-eta_t) 00057 * with its first order Taylor expansion around 0 00058 * to avoid catastrophic cancellation. 00059 */ 00060 return (label - prediction)*eta_t/norm; 00061 } 00062 return (label - prediction)*(1-exp(-eta_t))/norm; 00063 } 00064 00065 float64_t CSquaredLoss::get_square_grad(float64_t prediction, float64_t label) 00066 { 00067 return (prediction - label) * (prediction - label); 00068 }