1 /** 2 * Copyright (c) 2011, The University of Southampton and the individual contributors. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * * Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * * Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * * Neither the name of the University of Southampton nor the names of its 16 * contributors may be used to endorse or promote products derived from this 17 * software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 package org.openimaj.workinprogress.sgdsvm; 31 32 import static java.lang.Math.exp; 33 import static java.lang.Math.log; 34 35 public enum LossFunctions implements Loss { 36 LogLoss 37 { 38 // logloss(a,y) = log(1+exp(-a*y)) 39 @Override 40 public double loss(double a, double y) { 41 final double z = a * y; 42 if (z > 18) 43 return exp(-z); 44 if (z < -18) 45 return -z; 46 return log(1 + exp(-z)); 47 } 48 49 // -dloss(a,y)/da 50 @Override 51 public double dloss(double a, double y) { 52 final double z = a * y; 53 if (z > 18) 54 return y * exp(-z); 55 if (z < -18) 56 return y; 57 return y / (1 + exp(z)); 58 } 59 }, 60 HingeLoss 61 { 62 // hingeloss(a,y) = max(0, 1-a*y) 63 @Override 64 public double loss(double a, double y) { 65 final double z = a * y; 66 if (z > 1) 67 return 0; 68 return 1 - z; 69 } 70 71 // -dloss(a,y)/da 72 @Override 73 public double dloss(double a, double y) { 74 final double z = a * y; 75 if (z > 1) 76 return 0; 77 return y; 78 } 79 }, 80 SquaredHingeLoss 81 { 82 // squaredhingeloss(a,y) = 1/2 * max(0, 1-a*y)^2 83 @Override 84 public double loss(double a, double y) { 85 final double z = a * y; 86 if (z > 1) 87 return 0; 88 final double d = 1 - z; 89 return 0.5 * d * d; 90 91 } 92 93 // -dloss(a,y)/da 94 @Override 95 public double dloss(double a, double y) { 96 final double z = a * y; 97 if (z > 1) 98 return 0; 99 return y * (1 - z); 100 } 101 }, 102 SmoothHingeLoss 103 { 104 // smoothhingeloss(a,y) = ... 105 @Override 106 public double loss(double a, double y) { 107 final double z = a * y; 108 if (z > 1) 109 return 0; 110 if (z < 0) 111 return 0.5 - z; 112 final double d = 1 - z; 113 return 0.5 * d * d; 114 } 115 116 // -dloss(a,y)/da 117 @Override 118 public double dloss(double a, double y) { 119 final double z = a * y; 120 if (z > 1) 121 return 0; 122 if (z < 0) 123 return y; 124 return y * (1 - z); 125 } 126 }; 127 }