31 #ifndef SHARK_OBJECTIVEFUNCTIONS_LOSS_SQUAREDHINGELOSS_H 32 #define SHARK_OBJECTIVEFUNCTIONS_LOSS_SQUAREDHINGELOSS_H 67 {
return "SquaredHingeLoss"; }
72 std::size_t numInputs = labels.size();
77 if(predictions.size2() == 1){
78 for(std::size_t i = 0; i != numInputs;++i){
80 double y = 2.0*labels(i)-1.0;
81 error +=
sqr(std::max(0.0,1.0-y*predictions(i,0)));
86 for(std::size_t i = 0; i != numInputs;++i){
88 for(std::size_t o = 0; o != predictions.size2();++o){
89 if( o == labels(i))
continue;
90 error +=
sqr(std::max(0.0,2.0 - predictions(i,labels(i))+predictions(i,o)));
100 std::size_t numInputs = labels.size();
101 std::size_t outputDim = predictions.size2();
104 gradient.resize(numInputs,outputDim);
109 for(std::size_t i = 0; i != numInputs; ++i){
110 double y = 2.0*labels(i)-1.0;
111 double sampleLoss = std::max(0.0,1.0-y*predictions(i,0));
113 gradient(i,0) = -y*sampleLoss;
114 error +=
sqr(sampleLoss);
119 for(std::size_t i = 0; i != numInputs;++i){
121 for(std::size_t o = 0; o != predictions.size2();++o){
122 if( o == labels(i))
continue;
123 double sampleLoss = std::max(0.0,2.0 - predictions(i,labels(i))+predictions(i,o));
125 gradient(i,o) = sampleLoss*0.25;
126 gradient(i,labels(i)) -= sampleLoss*0.25;
128 error+=
sqr(sampleLoss);