Advertisement
Guest User

Untitled

a guest
May 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #pragma once
  2. #include "Matrix.h"
  3. #include "Filter.h"
  4.  
  5. template<typename T>
  6. class Base_Cnn
  7. {
  8. public:
  9. // Конструкторы ----------------------------------------------------------
  10. Base_Cnn();
  11. Base_Cnn(const Base_Cnn& copy) = delete; // Запрет копирования
  12.  
  13. // Методы класса ---------------------------------------------------------
  14. // Добавление "полей" к матрице
  15. virtual void Padding(Matrix<T>&);
  16.  
  17. // Операция "Макс пулинга"
  18. virtual Matrix<T> Pooling(const Matrix<T>&, const int&, const int&);
  19.  
  20. // Операция свертки над матрицей значений
  21. virtual Matrix<T> Svertka(const Matrix<T>&, const Matrix<T>&) = 0;
  22.  
  23. // Перегрузка операторов -------------------------------------------------
  24. Base_Cnn<T>& operator= (const Base_Cnn<T>& copy) = delete; // Запрет копирования
  25.  
  26. // Деструктор ------------------------------------------------------------
  27. virtual ~Base_Cnn();
  28.  
  29. // Класс исключения ------------------------------------------------------
  30. class Base_CnnExeption : public std::runtime_error {
  31. public:
  32. Base_CnnExeption(std::string str) : std::runtime_error(str) {};
  33. ~Base_CnnExeption() {};
  34. };
  35. };
  36.  
  37. template<typename T>
  38. Base_Cnn<T>::Base_Cnn()
  39. {
  40. }
  41.  
  42. template<typename T>
  43. Base_Cnn<T>::~Base_Cnn()
  44. {
  45. }
  46.  
  47. template<typename T>
  48. Matrix<T> Base_Cnn<T>::Pooling(const Matrix<T>& a, const int& n_, const int& m_)
  49. {
  50. if ((n_ < 0) || (m_ < 0) || (n_ > a.getN()) || (m_ > a.getM())) {
  51. throw Base_Cnn<T>::Base_CnnExeption("Неверный размер ядра!");
  52. }
  53.  
  54. Matrix<T> copy(a.getN() / n_, a.getM() / m_);
  55.  
  56. for (int i = 0; i < copy.getN(); i++) {
  57. for (int j = 0; j < copy.getM(); j++) {
  58. copy[i][j] = a.getPodmatrix(i*n_, j*m_, n_, m_).Max();
  59. }
  60. }
  61. return copy;
  62. }
  63.  
  64.  
  65. template<typename T>
  66. void Base_Cnn<T>::Padding(Matrix<T>& a)
  67. {
  68. Matrix<T> copy(a.getN() + 2, a.getM() + 2);
  69.  
  70. for (int i = 0; i < copy.getN(); i++) {
  71. for (int j = 0; j < copy.getM(); j++) {
  72. if ((i == 0) || (j == 0) || (j == (copy.getM() - 1)) || (i == (copy.getN() - 1))) {
  73. copy[i][j] = 0;
  74. }
  75. else {
  76. copy[i][j] = a[i - 1][j - 1];
  77. }
  78. }
  79. }
  80.  
  81. a = copy;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement