1 #ifndef OUTPUT_BINARYCLASSENTROPY_H_ 2 #define OUTPUT_BINARYCLASSENTROPY_H_ 23 void check_target_data(
const Matrix& target)
26 const int nelem = target.size();
27 const Scalar* target_data = target.data();
28 for(
int i = 0; i < nelem; i++)
30 if((target_data[i] != Scalar(0)) && (target_data[i] != Scalar(1)))
31 throw std::invalid_argument(
"[class BinaryClassEntropy]: Target data should only contain zero or one");
35 void check_target_data(
const IntegerVector& target)
38 const int nobs = target.size();
39 for(
int i = 0; i < nobs; i++)
41 if((target[i] != 0) && (target[i] != 1))
42 throw std::invalid_argument(
"[class BinaryClassEntropy]: Target data should only contain zero or one");
46 void evaluate(
const Matrix& prev_layer_data,
const Matrix& target)
49 const int nobs = prev_layer_data.cols();
50 const int nvar = prev_layer_data.rows();
51 if((target.cols() != nobs) || (target.rows() != nvar))
52 throw std::invalid_argument(
"[class BinaryClassEntropy]: Target data have incorrect dimension");
58 m_din.resize(nvar, nobs);
59 m_din.array() = (target.array() < Scalar(0.5)).select((Scalar(1) - prev_layer_data.array()).cwiseInverse(),
60 -prev_layer_data.cwiseInverse());
63 void evaluate(
const Matrix& prev_layer_data,
const IntegerVector& target)
66 const int nvar = prev_layer_data.rows();
68 throw std::invalid_argument(
"[class BinaryClassEntropy]: Only one response variable is allowed when class labels are used as target data");
71 const int nobs = prev_layer_data.cols();
72 if(target.size() != nobs)
73 throw std::invalid_argument(
"[class BinaryClassEntropy]: Target data have incorrect dimension");
76 m_din.resize(1, nobs);
77 m_din.array() = (target.array() == 0).select((Scalar(1) - prev_layer_data.array()).cwiseInverse(),
78 -prev_layer_data.cwiseInverse());
81 const Matrix& backprop_data()
const 93 return m_din.array().abs().log().sum() / m_din.cols();