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