Advertisement
pasholnahuy

Untitled

Jan 17th, 2024
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <Eigen/Eigen>
  4. #include <EigenRand/EigenRand>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <utility>
  8.  
  9. namespace network {
  10.  
  11. enum class Threshold_Id { Sigmoid, ReLu };
  12.  
  13. struct Threshold_Database {
  14. template <Threshold_Id> static double evaluate_0(double);
  15.  
  16. template <Threshold_Id> static double evaluate_1(double);
  17.  
  18. template <> inline double evaluate_0<Threshold_Id::Sigmoid>(double x) {
  19. return 1. / (1. + std::exp(-x));
  20. }
  21.  
  22. template <> inline double evaluate_1<Threshold_Id::Sigmoid>(double x) {
  23. return std::exp(-x) * evaluate_0<Threshold_Id::Sigmoid>(x) *
  24. evaluate_0<Threshold_Id::Sigmoid>(x);
  25. }
  26.  
  27. template <> inline double evaluate_0<Threshold_Id::ReLu>(double x) {
  28. return x > 0 ? x : 0;
  29. }
  30.  
  31. template <> inline double evaluate_1<Threshold_Id::ReLu>(double x) {
  32. return x > 0 ? 1 : 0;
  33. }
  34. };
  35.  
  36. class Threshold_Func {
  37. using FunctionType = std::function<double(double)>;
  38.  
  39. public:
  40. using MatrixXd = Eigen::MatrixXd;
  41. using VectorXd = Eigen::VectorXd;
  42. Threshold_Func(FunctionType evaluate_0, FunctionType evaluate_1);
  43.  
  44. template <Threshold_Id Id> static Threshold_Func create() {
  45. return Threshold_Func(Threshold_Database::evaluate_0<Id>,
  46. Threshold_Database::evaluate_1<Id>);
  47. }
  48.  
  49. static Threshold_Func create(Threshold_Id threshold);
  50.  
  51. double evaluate_0(double x) const;
  52.  
  53. double evaluate_1(double x) const;
  54.  
  55. MatrixXd apply(const MatrixXd &vec) const;
  56.  
  57. MatrixXd derive(const MatrixXd &vec) const;
  58.  
  59. private:
  60. FunctionType evaluate_0_;
  61. FunctionType evaluate_1_;
  62. };
  63. } // namespace network
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement