Advertisement
pasholnahuy

Untitled

Mar 6th, 2024
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. //
  2. // Created by Denis Ryapolov on 04.03.2024.
  3. //
  4. #include "sin_test.h"
  5.  
  6. void sin_test::test() {
  7. std::random_device rd;
  8. std::mt19937 gen(42);
  9. std::uniform_real_distribution<> dis(
  10. 0, M_PI + 0.01);
  11. int size = 500;
  12. vector<double> in_values(size);
  13. vector<double> target_values(size);
  14. for (int i = 0; i < size; ++i) {
  15. double randomNum = dis(gen);
  16. in_values[i] = randomNum;
  17. target_values[i] = sin(randomNum);
  18. }
  19. double min_score = 1e9;
  20. double optimal_step = -1;
  21. for (int step_size = 1; step_size < 150; ++step_size){
  22. Network net({1, 50, 50, 1}, {ThresholdId::Sigmoid, ThresholdId::Sigmoid,
  23. ThresholdId::Sigmoid});
  24. if (step_size % 100 == 0){
  25. std::cout << step_size << "\n";
  26. }
  27. for (int epoch = 0; epoch < 10; ++epoch) {
  28. // std::cout << "Epoch num: " << epoch << '\n';
  29. // std::cout << '\n';
  30. for (int k = 0; k < size; ++k) {
  31. MatrixXd input(1, 1);
  32. MatrixXd target(1, 1);
  33. input << in_values[k];
  34. target << target_values[k];
  35. net.Train(input, target, ScoreFunc::create(ScoreId::MSE), 1, double(step_size)/100);
  36. }
  37. }
  38. double score = 0;
  39. for (int i = 0; i < size; ++i) {
  40. double randomNum = dis(gen);
  41. VectorXd test_input(1, 1);
  42. test_input << randomNum;
  43. score += abs(net.Calculate(test_input)(0, 0) - sin(randomNum));
  44. // test_input << in_values[i];
  45. // std::cout << net.Calculate(test_input) << " " << target_values[i] <<
  46. // '\n';
  47. }
  48. if (score/size < min_score){
  49. optimal_step = double(step_size)/100;
  50. min_score = score/size;
  51. }
  52. // std::cout << "step size: " << double(step_size)/100 << " score: "<< score / size << '\n';
  53. }
  54. std::cout << min_score << " " << optimal_step;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement