33 #ifndef SHARK_OBJECTIVEFUNCTIONS_LOSS_SQUAREDLOSS_H 34 #define SHARK_OBJECTIVEFUNCTIONS_LOSS_SQUAREDLOSS_H 47 template<
class OutputType = RealVector,
class LabelType = OutputType >
64 {
return "SquaredLoss"; }
69 double eval(BatchLabelType
const& labels, BatchOutputType
const& predictions)
const {
70 SIZE_CHECK(labels.size1()==predictions.size1());
71 SIZE_CHECK(labels.size2()==predictions.size2());
74 for(std::size_t i = 0; i != labels.size1(); ++i){
82 double evalDerivative(BatchLabelType
const& label, BatchOutputType
const& prediction, BatchOutputType& gradient)
const {
83 gradient.resize(prediction.size1(),prediction.size2());
84 noalias(gradient) = 2.0*(prediction - label);
90 template<
class OutputType>
107 {
return "SquaredLoss"; }
112 double eval(BatchLabelType
const& labels, BatchOutputType
const& predictions)
const {
113 SIZE_CHECK(labels.size()==predictions.size1());
116 for(std::size_t i = 0; i != labels.size(); ++i){
117 unsigned int c = labels(i);
119 error+=
norm_sqr(
row(predictions,i))+1.0-2.0*predictions(i,c);
126 double evalDerivative(BatchLabelType
const& labels, BatchOutputType
const& predictions, BatchOutputType& gradient)
const {
127 gradient.resize(predictions.size1(),predictions.size2());
128 noalias(gradient) = 2.0*predictions;
129 for(std::size_t i = 0; i != labels.size(); ++i){
130 unsigned int c = labels(i);
155 {
return "SquaredLoss"; }
165 SIZE_CHECK(labels.size()==predictions.size());
168 for(std::size_t i = 0; i != labels.size(); ++i){
170 if(labels[i].
size() <= m_ignore)
171 throw SHARKEXCEPTION(
"[SquaredLoss::eval] Number of sequence elements to ignore is too large");
173 for(std::size_t j = m_ignore; j != labels[i].size(); ++j){
174 error +=
distanceSqr(predictions[i][j],labels[i][j]);
183 SIZE_CHECK(labels.size()==predictions.size());
184 gradient.resize(labels.size());
187 for(std::size_t i = 0; i != labels.size(); ++i){
189 if(labels[i].
size() <= m_ignore)
190 throw SHARKEXCEPTION(
"[SquaredLoss::eval] Number of sequence elements to ignore is too large");
191 for(std::size_t j = 0; j != m_ignore; ++j){
192 gradient[i].push_back(RealVector(predictions[i][j].
size(),0.0));
194 for(std::size_t j = m_ignore; j != labels[i].size(); ++j){
195 error +=
distanceSqr(predictions[i][j],labels[i][j]);
196 gradient[i].push_back(2.0*(predictions[i][j] - labels[i][j]));
203 std::size_t m_ignore;