Advertisement
sdfDFASDFASDF

Untitled

May 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. #pragma once
  2. #include "Matrix.h"
  3. #include <iostream>
  4. #include <iomanip>
  5.  
  6. template<typename T>
  7. class Weights : public Matrix<T>
  8. {
  9. public:
  10. // Конструкторы ----------------------------------------------------------
  11. Weights(); // По умолчанию
  12. Weights(const int& i_, const int& j_, const int& wbisas_ = 0); // Инициализатор (нулевая матрица)
  13. Weights(T** arr_, const int& i_, const int& j_, const int& wbisas_ = 0); // Инициализатор
  14. Weights(const Weights<T>& copy); // Копирования
  15.  
  16. // Методы класса ---------------------------------------------------------
  17. // Вывод весов на консоль в красивом виде
  18. void Out();
  19.  
  20. // Получение доступа к d
  21. T& GetD() { return d; };
  22. const T& GetD() const { return d; };
  23.  
  24. // Получение доступа к wbisas
  25. T& GetWBias() { return wbias; };
  26. const T& GetWBias() const { return wbias; };
  27.  
  28. // Перегрузки операторов ------------------------
  29. Weights<T>& operator= (const Weights<T>& copy); // Оператор присваивания
  30. template <typename T1> friend std::ostream& operator<< (std::ostream& out, const Weights<T1>& mat); // Оператор вывод матрицы в поток
  31. template <typename T1> friend std::istream& operator>> (std::istream& in, Weights<T1>& mat); // Оператор чтение матрицы из потока
  32.  
  33.  
  34. // Деструктор ------------------------------------------------------------
  35. ~Weights();
  36. private:
  37. // Величина производной функции ошибки
  38. T d;
  39.  
  40. // Вес нейрона сдвига
  41. T wbias;
  42.  
  43. };
  44.  
  45. template<typename T>
  46. Weights<T>::Weights() : Matrix<T>(), d(0), wbias(0)
  47. {
  48. }
  49.  
  50. template<typename T>
  51. Weights<T>::Weights(const int & i_, const int & j_, const int& wbisas_) : Matrix<T>(i_, j_), d(0), wbias(wbisas_)
  52. {
  53. }
  54.  
  55. template<typename T>
  56. Weights<T>::Weights(T ** arr_, const int & i_, const int & j_, const int& wbisas_) : Matrix<T>(arr_, i_, j_), d(0), wbias(0)
  57. {
  58. }
  59.  
  60. template<typename T>
  61. Weights<T>::Weights(const Weights<T> & copy) : Matrix<T>(copy), d(copy.GetD()), wbias(copy.GetWBias())
  62. {
  63. }
  64.  
  65. template<typename T>
  66. inline void Weights<T>::Out()
  67. {
  68. for (int i = 0; i < this->n; i++) {
  69. for(int j = 0; j < this->m; j++){
  70. std::cout << this->arr[i][j] << " ";
  71. }
  72. std::cout << std::endl;
  73. }
  74. if (wbias != 0) {
  75. std::cout << wbias << std::endl;
  76. }
  77. }
  78.  
  79. inline void Weights<int>::Out()
  80. {
  81. using std::cout;
  82. int max = this->Max();
  83. int k = 2;
  84. while (max > 0) {
  85. k++;
  86. max = max / 10;
  87. }
  88. for (int i = 0; i < this->n; i++) {
  89. for (int j = 0; j < this->m; j++) {
  90. cout << std::setw(k) << this->arr[i][j];
  91. }
  92. cout << std::endl;
  93. }
  94. if (wbias != 0) {
  95. std::cout << std::setw(k) << wbias << std::endl;
  96. }
  97. }
  98.  
  99. inline void Weights<double>::Out()
  100. {
  101. using std::cout;
  102. int max = (int)this->Max();
  103. int k = 2;
  104. while (max > 0) {
  105. k++;
  106. max = max / 10;
  107. }
  108. for (int i = 0; i < this->n; i++) {
  109. for (int j = 0; j < this->m; j++) {
  110. cout << std::setw(k+5) << std::setprecision(2)<< this->arr[i][j];
  111. }
  112. cout << std::endl;
  113. }
  114. if (wbias != 0) {
  115. std::cout << std::setw(k + 5) << std::setprecision(4) << wbias << std::endl;
  116. }
  117. }
  118.  
  119. template<typename T>
  120. inline Weights<T>& Weights<T>::operator=(const Weights<T>& copy)
  121. {
  122. if (this == &copy) {
  123. return *this;
  124. }
  125. if ((copy.n > this->n) || (copy.m > this->m)) {
  126. for (int i = 0; i < this->n; i++) {
  127. this->arr[i].reset();
  128. }
  129. this->arr.reset();
  130. this->n = copy.n;
  131. this->m = copy.m;
  132. this->initMat();
  133. }
  134. else {
  135. this->n = copy.n;
  136. this->m = copy.m;
  137. }
  138.  
  139. for (int i = 0; i < this->n; i++) {
  140. for (int j = 0; j < this->m; j++) {
  141. this->arr[i][j] = copy.arr[i][j];
  142. }
  143. }
  144. d = copy.d;
  145. wbias = copy.wbias;
  146. return *this;
  147. }
  148.  
  149. template<typename T>
  150. Weights<T>::~Weights()
  151. {
  152. }
  153.  
  154. template<typename T1>
  155. inline std::ostream & operator<<(std::ostream & out, const Weights<T1>& mat)
  156. {
  157. out << (Matrix<T1>) mat;
  158. out << mat.d << ' ' << mat.wbias << std::endl;
  159. return out;
  160. }
  161.  
  162. template<typename T1>
  163. inline std::istream & operator>>(std::istream & in, Weights<T1>& mat)
  164. {
  165. in >> ((Matrix<T1>&) mat);
  166. in >> mat.d;
  167. in >> mat.wbias;
  168. return in;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement