MiniDNN
RegressionMSE.h
1 #ifndef OUTPUT_REGRESSIONMSE_H_
2 #define OUTPUT_REGRESSIONMSE_H_
3 
4 #include <Eigen/Core>
5 #include <stdexcept>
6 #include "../Config.h"
7 
8 namespace MiniDNN {
9 
10 
16 class RegressionMSE: public Output
17 {
18 private:
19  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> Matrix;
20  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1> Vector;
21 
22  Matrix m_din; // Derivative of the input of this layer.
23  // Note that input of this layer is also the output of previous layer
24 
25 public:
26  void evaluate(const Matrix& prev_layer_data, const Matrix& target)
27  {
28  // Check dimension
29  const int nobs = prev_layer_data.cols();
30  const int nvar = prev_layer_data.rows();
31  if((target.cols() != nobs) || (target.rows() != nvar))
32  throw std::invalid_argument("[class RegressionMSE]: Target data have incorrect dimension");
33 
34  // Compute the derivative of the input of this layer
35  // L = 0.5 * ||yhat - y||^2
36  // in = yhat
37  // d(L) / d(in) = yhat - y
38  m_din.resize(nvar, nobs);
39  m_din.noalias() = prev_layer_data - target;
40  }
41 
42  const Matrix& backprop_data() const
43  {
44  return m_din;
45  }
46 
47  Scalar loss() const
48  {
49  // L = 0.5 * ||yhat - y||^2
50  return m_din.squaredNorm() / m_din.cols() * Scalar(0.5);
51  }
52 };
53 
54 
55 } // namespace MiniDNN
56 
57 
58 #endif /* OUTPUT_REGRESSIONMSE_H_ */