Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by Denis Ryapolov on 04.03.2024.
- //
- #include "sin_test.h"
- void sin_test::test() {
- std::random_device rd;
- std::mt19937 gen(42);
- std::uniform_real_distribution<> dis(
- 0, M_PI + 0.01);
- int size = 500;
- vector<double> in_values(size);
- vector<double> target_values(size);
- for (int i = 0; i < size; ++i) {
- double randomNum = dis(gen);
- in_values[i] = randomNum;
- target_values[i] = sin(randomNum);
- }
- double min_score = 1e9;
- double optimal_step = -1;
- for (int step_size = 1; step_size < 150; ++step_size){
- Network net({1, 50, 50, 1}, {ThresholdId::Sigmoid, ThresholdId::Sigmoid,
- ThresholdId::Sigmoid});
- if (step_size % 100 == 0){
- std::cout << step_size << "\n";
- }
- for (int epoch = 0; epoch < 10; ++epoch) {
- // std::cout << "Epoch num: " << epoch << '\n';
- // std::cout << '\n';
- for (int k = 0; k < size; ++k) {
- MatrixXd input(1, 1);
- MatrixXd target(1, 1);
- input << in_values[k];
- target << target_values[k];
- net.Train(input, target, ScoreFunc::create(ScoreId::MSE), 1, double(step_size)/100);
- }
- }
- double score = 0;
- for (int i = 0; i < size; ++i) {
- double randomNum = dis(gen);
- VectorXd test_input(1, 1);
- test_input << randomNum;
- score += abs(net.Calculate(test_input)(0, 0) - sin(randomNum));
- // test_input << in_values[i];
- // std::cout << net.Calculate(test_input) << " " << target_values[i] <<
- // '\n';
- }
- if (score/size < min_score){
- optimal_step = double(step_size)/100;
- min_score = score/size;
- }
- // std::cout << "step size: " << double(step_size)/100 << " score: "<< score / size << '\n';
- }
- std::cout << min_score << " " << optimal_step;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement