Neural
loss.h
Go to the documentation of this file.
1 #pragma once
2 #ifndef LOSS_H
3 #define LOSS_H
4 
5 #include <iostream>
6 #include <string>
7 #include <eigen3/Eigen/Dense>
8 #include <eigen3/Eigen/Jacobi>
9 
10 namespace Neural
11 {
12  class Loss {
13  public:
14  Loss() {};
15  virtual double Compute(Eigen::MatrixXd y_true, Eigen::MatrixXd y_pred) = 0;
16  virtual Eigen::MatrixXd Compute_prime(Eigen::MatrixXd y_true, Eigen::MatrixXd y_pred) = 0;
17 
18  };
19 
20 
21  class Mse : public Loss {
22  public:
23  Mse() {};
24  virtual double Compute(Eigen::MatrixXd y_true, Eigen::MatrixXd y_pred){
25  Eigen::MatrixXd diff = y_true-y_pred;
26  return diff.array().pow(2).mean();
27  }
28  virtual Eigen::MatrixXd Compute_prime(Eigen::MatrixXd y_true, Eigen::MatrixXd y_pred){
29  Eigen::MatrixXd diff = y_pred-y_true;
30  return (2*diff)/y_true.size();
31  }
32  };
33 
34 
35  class Cross_entropy : public Loss {
36  public:
38  virtual double Compute(Eigen::MatrixXd y_true, Eigen::MatrixXd y_pred){
39  double res;
40  for (int i(0);i<y_true.cols();i++){
41  res+= y_true(0,i)*log(y_pred(0,i));
42  }
43  return -res;
44  }
45  virtual Eigen::MatrixXd Compute_prime(Eigen::MatrixXd y_true, Eigen::MatrixXd y_pred){
46  //Eigen::MatrixXd res = (-(y_true/y_pred)+((1-y_true)/(1-y_pred)))/y_true.size();
47  return y_true;
48  }
49  };
50 }
51 #endif
Neural::Mse::Compute
virtual double Compute(Eigen::MatrixXd y_true, Eigen::MatrixXd y_pred)
Definition: loss.h:24
Neural::Cross_entropy
Definition: loss.h:35
Neural::Mse
Definition: loss.h:21
Neural::Mse::Mse
Mse()
Definition: loss.h:23
Neural::Loss::Compute_prime
virtual Eigen::MatrixXd Compute_prime(Eigen::MatrixXd y_true, Eigen::MatrixXd y_pred)=0
Neural::Cross_entropy::Compute_prime
virtual Eigen::MatrixXd Compute_prime(Eigen::MatrixXd y_true, Eigen::MatrixXd y_pred)
Definition: loss.h:45
Neural::Mse::Compute_prime
virtual Eigen::MatrixXd Compute_prime(Eigen::MatrixXd y_true, Eigen::MatrixXd y_pred)
Definition: loss.h:28
Neural::Cross_entropy::Compute
virtual double Compute(Eigen::MatrixXd y_true, Eigen::MatrixXd y_pred)
Definition: loss.h:38
Neural::Loss::Compute
virtual double Compute(Eigen::MatrixXd y_true, Eigen::MatrixXd y_pred)=0
Neural::Loss
Definition: loss.h:12
Neural::Cross_entropy::Cross_entropy
Cross_entropy()
Definition: loss.h:37
Neural::Loss::Loss
Loss()
Definition: loss.h:14
Neural
Definition: activation.h:10