Advertisement
pasholnahuy

Untitled

Jan 17th, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. //
  2. // Created by Denis Ryapolov on 06.01.2024.
  3. //
  4.  
  5. #include "Network.h"
  6. namespace network {
  7. using VectorXd = Network::VectorXd;
  8.  
  9. Network::Network(std::initializer_list<int> dimensions,
  10. std::initializer_list<Threshold_Id> threshold_id)
  11. : threshold_id_(threshold_id) {
  12. layers_.reserve(dimensions.size() - 1);
  13. auto dim_it = dimensions.begin();
  14. for (auto threshold_it = threshold_id.begin();
  15. threshold_it != threshold_id.end(); ++dim_it, ++threshold_it) {
  16. layers_.emplace_back(*threshold_it, *std::next(dim_it), *dim_it);
  17. }
  18. }
  19.  
  20. Values Network::Forward_Prop(const MatrixXd &start_mat) {
  21. Values values;
  22. values.in.resize(layers_.size());
  23. values.out.resize(layers_.size());
  24. MatrixXd cur_mat = start_mat;
  25. for (size_t i = 0; i < layers_.size(); ++i) {
  26. values.in[i] = cur_mat;
  27. cur_mat = layers_[i].apply(cur_mat);
  28. values.out[i] = cur_mat;
  29. cur_mat = Threshold_Func::create(threshold_id_[i]).apply(cur_mat);
  30. }
  31. return values;
  32. }
  33.  
  34. VectorXd Network::Back_Prop(const MatrixXd &start_vec,
  35. const MatrixXd &reference,
  36. const Score_Func &score_func, double step) {
  37. Values values = Forward_Prop(start_vec);
  38. const auto func = Threshold_Func::create(threshold_id_.back());
  39. VectorXd u = score_func.gradient(func.apply(values.out.back()), reference);
  40. for (int i = layers_.size() - 1; i >= 0; --i) {
  41. layers_[i].apply_gradA(layers_[i].gradA(values.in[i], u, values.out[i]),
  42. step);
  43. layers_[i].apply_gradb(layers_[i].gradb(u, values.out[i]), step);
  44. u = layers_[i].gradx(u, values.out[i]);
  45. }
  46. return u;
  47. }
  48.  
  49. VectorXd Network::Back_Prop_SGD(const MatrixXd &start_batch,
  50. const MatrixXd &reference,
  51. const Score_Func &score_func, int iter_num) {
  52. auto rand_ind = index_generator() % start_batch.cols();
  53. return Back_Prop(start_batch.col(rand_ind), reference.col(rand_ind),
  54. score_func, 1.0 / iter_num);
  55. }
  56.  
  57. void Network::TrainSGD(const MatrixXd &start_batch, const MatrixXd &reference,
  58. const Score_Func &score_func, double needed_accuracy,
  59. int max_epochs) {
  60. int epochs = 0;
  61. while (epochs != max_epochs &&
  62. abs(VectorXd::Ones(reference.rows()).transpose() *
  63. Back_Prop_SGD(start_batch, reference, score_func, epochs)) >
  64. needed_accuracy) {
  65. ++epochs;
  66. }
  67. }
  68.  
  69. void Network::TrainBGD(const MatrixXd &start_batch, const MatrixXd &reference,
  70. const Score_Func &score_func, int cols_in_minibatch,
  71. double needed_accuracy, int max_epochs) {
  72. PermutationMatrix perm(start_batch.cols());
  73. perm.setIdentity();
  74. std::shuffle(perm.indices().data(),
  75. perm.indices().data() + perm.indices().size(), index_generator);
  76. MatrixXd mini_batch =
  77. start_batch * MatrixXd(perm).leftCols(cols_in_minibatch);
  78. MatrixXd mini_reference =
  79. reference * MatrixXd(perm).leftCols(cols_in_minibatch);
  80. int epochs = 0;
  81. while (epochs != max_epochs &&
  82. abs(VectorXd::Ones(reference.rows()).transpose() *
  83. Back_Prop(mini_batch, mini_reference, score_func, epochs)) >
  84. needed_accuracy) {
  85. ++epochs;
  86. }
  87. }
  88. VectorXd Network::Cols_Mean(const MatrixXd &x) {
  89. return x * VectorXd::Ones(x.cols()) / x.cols();
  90. }
  91. } // namespace network
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement