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){
75 error+=distanceSqr(row(predictions,i),row(labels,i));
82 double evalDerivative(BatchLabelType
const& label, BatchOutputType
const& prediction, BatchOutputType& gradient)
const {
83 gradient.resize(prediction.size1(),prediction.size2());
84 noalias(gradient) = (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) = 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){
169 SIZE_CHECK(labels[i].size()==predictions[i].size());
170 SHARK_RUNTIME_CHECK(labels[i].size() > m_ignore,
"Number of sequence elements to ignore is too large");
172 for(std::size_t j = m_ignore; j != labels[i].size(); ++j){
173 error += distanceSqr(predictions[i][j],labels[i][j]);
182 SIZE_CHECK(labels.size()==predictions.size());
183 gradient.resize(labels.size());
186 for(std::size_t i = 0; i != labels.size(); ++i){
187 SIZE_CHECK(labels[i].size()==predictions[i].size());
188 SHARK_RUNTIME_CHECK(labels[i].size() > m_ignore,
"Number of sequence elements to ignore is too large");
189 for(std::size_t j = 0; j != m_ignore; ++j){
190 gradient[i].push_back(RealVector(predictions[i][j].size(),0.0));
192 for(std::size_t j = m_ignore; j != labels[i].size(); ++j){
193 error += 0.5 * distanceSqr(predictions[i][j],labels[i][j]);
194 gradient[i].push_back(predictions[i][j] - labels[i][j]);
201 std::size_t m_ignore;