Advertisement
sdfDFASDFASDF

Untitled

May 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. #pragma once
  2. #include "Weights.h"
  3. #include "NeyronPerceptron.h"
  4. #include <cstdlib>
  5.  
  6. template<typename T, typename Y>
  7. class PerceptronLearning
  8. {
  9. public:
  10. // Конструкторы ----------------------------------------------------------
  11. PerceptronLearning(); // По умолчанию
  12. PerceptronLearning(const double& E_); // Инициализатор
  13. PerceptronLearning(const NeyronPerceptron<T,Y>& copy) = delete; // Запрет копирования
  14.  
  15. // Методы класса ---------------------------------------------------------
  16. // Обучение однослойного перцептрона методом обратного распространения ошибки
  17. void WTSimplePerceptron(const Y& a, const Y& y, Weights<T>& w, const Matrix<T>& in);
  18.  
  19. // Метод обратного распространения ошибки
  20. static void BackPropagation(Matrix<Weights<T>>& w, const Weights<T>& y);
  21. static void BackPropagation(Matrix<Weights<T>>& w, const Matrix<Weights<T>>& y);
  22.  
  23. // Метод градиентного спуска
  24. void GradDes(Weights<T>& w, Matrix<T>& in, Func<T, Y>& F, const T& x);
  25.  
  26. // Метод вычисления средней квадратичной ошибки
  27. static Y RMS_error(const Y* a, const Y* y, const int& lenth);
  28.  
  29. // Метод вычисления ошибки выходного слоя
  30. static Y PartDOutLay(const Y& a, const Y& y);
  31.  
  32. // Метод стягивания весов
  33. void retract(Matrix<Weights<T>>& weights,const int& decs);
  34. void retract(Weights<T>& weights, const int& decs);
  35.  
  36. // Тасование последовательности
  37. void shuffle(int* arr, const int& lenth);
  38.  
  39. // Метод получения доступа к кофиценту обучения
  40. double& getE() { return E; };
  41.  
  42. // Перегрузка операторов -------------------------------------------------
  43. PerceptronLearning& operator= (const PerceptronLearning& copy) = delete; // Запрет копирования
  44. friend std::ostream& operator<<(std::ostream& out, const PerceptronLearning& mat) = delete; // Запрет вывода в поток
  45. friend std::istream& operator>>(std::istream& out, PerceptronLearning& mat) = delete; // Запрет считывания из потока
  46.  
  47. // Деструктор ------------------------------------------------------------
  48. ~PerceptronLearning();
  49.  
  50. // Класс исключения ------------------------------------------------------
  51. class LearningExeption : public std::runtime_error {
  52. public:
  53. LearningExeption(std::string str) : std::runtime_error(str) {};
  54. ~LearningExeption() {};
  55. };
  56. protected:
  57. // Поля класса ----------------------------------
  58. double E; // Кофицент обучения
  59. };
  60.  
  61. template<typename T, typename Y>
  62. inline PerceptronLearning<T,Y>::PerceptronLearning() : E(1)
  63. {
  64. }
  65.  
  66. template<typename T, typename Y>
  67. inline PerceptronLearning<T, Y>::PerceptronLearning(const double & E_) : E(E_)
  68. {
  69. }
  70.  
  71. template<typename T, typename Y>
  72. inline void PerceptronLearning<T, Y>::WTSimplePerceptron(const Y & a, const Y & y, Weights<T> & w, const Matrix<T>& in)
  73. {
  74. if ((w.getN() != in.getN()) || (w.getM() != in.getM())) {
  75. throw LearningExeption("Несовпадение размеров входной матрицы и матрицы весов!");
  76. }
  77. T delta = a - y;
  78. T ii = 0;
  79. if (delta == 0) {
  80. return;
  81. }
  82. for (int i = 0; i < w.getN(); i++) {
  83. for (int j = 0; j < w.getM(); j++) {
  84. ii = w[i][j] + E * delta*in[i][j];
  85. w[i][j] = ii;
  86. }
  87. }
  88. w.GetWBias() += E * delta;
  89. }
  90.  
  91. template<typename T, typename Y>
  92. inline void PerceptronLearning<T, Y>::BackPropagation(Matrix<Weights<T>>& w, const Weights<T>& y)
  93. {
  94. for (int i = 0; i < y.getN(); i++) {
  95. for (int j = 0; j < y.getM(); j++) {
  96. w[i][j].GetD() += (y[i][j] * y.GetD());
  97. }
  98. }
  99. }
  100.  
  101. template<typename T, typename Y>
  102. inline void PerceptronLearning<T, Y>::BackPropagation(Matrix<Weights<T>>& w, const Matrix<Weights<T>>& y)
  103. {
  104. for (int o = 0; o < y.getN(); o++) {
  105. for (int u = 0; u < y.getM(); u++) {
  106. for (int i = 0; i < y[o][u].getN(); i++) {
  107. for (int j = 0; j < y[o][u].getM(); j++) {
  108. w[i][j].GetD() += (y[o][u][i][j] * y.GetD());
  109. }
  110. }
  111. }
  112. }
  113. }
  114.  
  115. template<typename T, typename Y>
  116. inline void PerceptronLearning<T, Y>::GradDes(Weights<T>& w, Matrix<T>& in, Func<T, Y>& F, const T& x)
  117. {
  118. if ((w.getN() != in.getN()) || (w.getM() != in.getM())) {
  119. throw LearningExeption("Несовпадение размеров входной матрицы и матрицы весов!");
  120. }
  121.  
  122. for (int i = 0; i < w.getN(); i++) {
  123. for (int j = 0; j < w.getM(); j++) {
  124. w[i][j] -= (w.GetD() * E * F(x) * in[i][j]);
  125. }
  126. }
  127. w.GetWBias() -= E * F(x) * w.GetD();
  128. }
  129.  
  130.  
  131. template<typename T, typename Y>
  132. inline Y PerceptronLearning<T, Y>::RMS_error(const Y * a, const Y * y, const int & lenth)
  133. {
  134. Y err = 0;
  135. for (int i = 0; i < lenth; i++) {
  136. err += (a[i] - y[i])*(a[i] - y[i]);
  137. }
  138. err /= 2;
  139. return err;
  140. }
  141.  
  142. template<typename T, typename Y>
  143. inline Y PerceptronLearning<T, Y>::PartDOutLay(const Y & a, const Y & y)
  144. {
  145. return -2*(a - y);
  146. }
  147.  
  148. template<typename T, typename Y>
  149. inline void PerceptronLearning<T, Y>::retract(Matrix<Weights<T>>& weights, const int & decs)
  150. {
  151. int d = 1;
  152. for (int i = 0; i < decs; i++) {
  153. d *= 0.1;
  154. }
  155. for (int i = 0; i < weights.getN(); i++) {
  156. for (int j = 0; j < weights.getM(); j++) {
  157. for (int k = 0; k < weights[i][j].getN(); k++) {
  158. for (int y = 0; y < weights[i][j].getM(); y++) {
  159. if (weights[i][j][k][y] > 0) {
  160. weights[i][j][k][y] -= d;
  161. }
  162. else {
  163. weights[i][j][k][y] += d;
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170.  
  171. template<typename T, typename Y>
  172. inline void PerceptronLearning<T, Y>::retract(Weights<T>& weights, const int & decs)
  173. {
  174. int d = 1;
  175. for (int i = 0; i < decs; i++) {
  176. d *= 0.1;
  177. }
  178. for (int k = 0; k < weights.getN(); k++) {
  179. for (int y = 0; y < weights.getM(); y++) {
  180. if (weights[k][y] > 0) {
  181. weights[k][y] -= d;
  182. }
  183. else {
  184. weights[k][y] += d;
  185. }
  186. }
  187. }
  188. }
  189.  
  190.  
  191.  
  192. template<typename T, typename Y>
  193. inline void PerceptronLearning<T, Y>::shuffle(int * arr, const int & lenth)
  194. {
  195. srand(time(0));
  196. int j = 0;
  197. int tmp = 0;
  198. for (int i = 0; i < lenth; i++) {
  199. j = ((double)rand()/INT_MAX*lenth);
  200. tmp = arr[i];
  201. arr[i] = arr[j];
  202. arr[j] = tmp;
  203. }
  204. }
  205.  
  206. template<typename T, typename Y>
  207. inline PerceptronLearning<T, Y>::~PerceptronLearning()
  208. {
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement