Advertisement
pasholnahuy

score func cpp

Jan 17th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. //
  2. // Created by Denis Ryapolov on 06.01.2024.
  3. //
  4.  
  5. #include "Score_Func.h"
  6. using Eigen::VectorXd;
  7.  
  8. namespace network {
  9. using MatrixXd = Score_Func::MatrixXd;
  10.  
  11. Score_Func::Score_Func(ScoreFuncType score_func, GradientFuncType gradient_func)
  12. : score_func_(std::move(score_func)),
  13. gradient_func_(std::move(gradient_func)) {
  14. assert(score_func_ && "Empty score function!");
  15. assert(gradient_func_ && "Empty gradient function!");
  16. }
  17.  
  18. Score_Func Score_Func::create(Score_Id score) {
  19. switch (score) {
  20. case Score_Id::MSE: {
  21. return create<Score_Id::MSE>();
  22. }
  23. case Score_Id::MAE: {
  24. return create<Score_Id::MAE>();
  25. }
  26. case Score_Id::CrossEntropy: {
  27. return create<Score_Id::CrossEntropy>();
  28. }
  29. default:
  30. return create<Score_Id::MSE>();
  31. }
  32. }
  33.  
  34. double Score_Func::score(const MatrixXd &x, const MatrixXd &reference) const {
  35. assert(score_func_ && "Empty score function!");
  36. return score_func_(x, reference);
  37. }
  38.  
  39. VectorXd Score_Func::gradient(const VectorXd &x,
  40. const VectorXd &reference) const {
  41. assert(gradient_func_ && "Empty gradient function!");
  42. return gradient_func_(x, reference);
  43. }
  44. MatrixXd Score_Database::SoftMax(const MatrixXd &matr) {
  45. MatrixXd exp_matr = matr.unaryExpr([](double x) { return exp(x); });
  46. MatrixXd inv_diag_sum =
  47. (exp_matr.transpose() * VectorXd::Ones(matr.rows()).asDiagonal())
  48. .unaryExpr([](double x) { return 1.0 / x; });
  49.  
  50. return matr * inv_diag_sum;
  51. }
  52.  
  53. } // namespace network
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement