36 #ifndef SHARK_OBJECTIVEFUNCTIONS_LOSS_CROSS_ENTROPY_H 37 #define SHARK_OBJECTIVEFUNCTIONS_LOSS_CROSS_ENTROPY_H 74 double evalError(
double label,
double exponential,
double value)
const {
76 if(value*label < -200 ){
79 return - value * label;
81 return std::log(1+exponential);
93 {
return "CrossEntropy"; }
98 double eval(UIntVector
const& target, RealMatrix
const& prediction)
const {
99 if ( prediction.size2() == 1 )
102 for(std::size_t i = 0; i != prediction.size1(); ++i){
104 double label = 2 *
static_cast<double>(target(i)) - 1;
105 double exponential = std::exp ( -label * prediction ( i,0 ) );
106 error+= evalError(label,exponential,prediction (i, 0 ));
113 for(std::size_t i = 0; i != prediction.size1(); ++i){
119 double maximum =
max(
row(prediction,i));
121 for( std::size_t j = 0; j != prediction.size2(); ++j)
123 double term = std::exp(prediction(i,j)-maximum);
126 logNorm = std::log(logNorm)+maximum;
128 error+= logNorm - prediction(i,target(i));
134 double evalDerivative(UIntVector
const& target, RealMatrix
const& prediction, RealMatrix& gradient)
const {
135 gradient.resize(prediction.size1(),prediction.size2());
136 if ( prediction.size2() == 1 )
139 for(std::size_t i = 0; i != prediction.size1(); ++i){
141 double label = 2 *
static_cast<double>(target(i)) - 1;
142 double exponential = std::exp ( -label * prediction (i, 0 ) );
143 double sigmoid = 1.0/(1.0+exponential);
144 gradient ( i,0 ) = -label * (1.0 -
sigmoid);
145 error+=evalError(label,exponential,prediction (i, 0 ));
152 for(std::size_t i = 0; i != prediction.size1(); ++i){
154 RealMatrixRow gradRow=
row(gradient,i);
161 double maximum =
max(
row(prediction,i));
162 noalias(gradRow) = exp(
row(prediction,i) - maximum);
163 double norm =
sum(gradRow);
165 gradient(i,target(i)) -= 1;
166 error+=std::log(norm) - prediction(i,target(i))+maximum;
173 unsigned int const& target,
174 RealVector
const& prediction,
175 RealVector& gradient,
178 gradient.resize(prediction.size());
179 hessian.resize(prediction.size(),prediction.size());
180 if ( prediction.size() == 1 )
183 double label = 2 *
static_cast<double>(target) - 1;
184 double exponential = std::exp ( -label * prediction ( 0 ) );
185 double sigmoid = 1.0/(1.0+exponential);
186 gradient ( 0 ) = -label * (1.0-
sigmoid);
187 hessian ( 0,0 ) = sigmoid * ( 1-
sigmoid );
188 return evalError(label,exponential,prediction ( 0 ));
198 double maximum =
max(prediction);
199 noalias(gradient) = exp(prediction-maximum);
200 double norm =
sum(gradient);
205 gradient(target) -= 1;
207 return std::log(norm) - prediction(target) - maximum;