Skip to content

Commit 13d557b

Browse files
committed
Clean project config
1 parent a7848e6 commit 13d557b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+5580
-0
lines changed

.gitignore

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Compiled Object files
5+
*.slo
6+
*.lo
7+
*.o
8+
*.obj
9+
10+
# Precompiled Headers
11+
*.gch
12+
*.pch
13+
14+
# Compiled Dynamic libraries
15+
*.so
16+
*.dylib
17+
*.dll
18+
19+
# Fortran module files
20+
*.mod
21+
*.smod
22+
23+
# Compiled Static libraries
24+
*.lai
25+
*.la
26+
*.a
27+
*.lib
28+
29+
# Executables
30+
*.exe
31+
*.out
32+
*.
33+
34+
# vs code
35+
*.vscode
36+
.vs
37+
38+
# cmake
39+
build
40+
cmake-build-debug
41+
cmake-build*
42+
43+
# Mac OS
44+
.DS_Store
45+
46+
#lib_of_images
47+
mnist_png
48+

ArtificialNN/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cmake_minimum_required(VERSION 3.0.0)# Проверка версии CMake.
2+
# Если версия установленой программы
3+
# старее указаной, произайдёт аварийный выход.
4+
project(ArtificialNN) # Название проекта
5+
6+
add_custom_target(copy_target ALL
7+
${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/src ./include
8+
COMMENT "Copy neded folder to compiling executable")

ArtificialNN/src/CNN/Base_Cnn.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}

ArtificialNN/src/CNN/CNNs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
#include "NeyronCnn.h"
3+
#include "CNNLearns.h"
4+
#include "Filter.h"
5+
#define D_NeyronCnn NeyronCnn<double>
6+
#define I_NeyronCnn NeyronCnn<int>
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#pragma once
2+
#include "Weights.h"
3+
#include "Filter.h"
4+
#include "NeyronCnn.h"
5+
#include <cstdlib>
6+
7+
template<typename T>
8+
class CNNLearning
9+
{
10+
public:
11+
// Конструкторы ----------------------------------------------------------
12+
CNNLearning(const int& s_ = 1, const double& E_ = 1);
13+
14+
// Методы класса ---------------------------------------------------------
15+
// Метод обратного распространения ошибки
16+
Matrix<T> ReversConvolution(const Matrix<T>& D, const Filter<T>& f);
17+
18+
// Метод градиентного спуска
19+
void GradDes(const Matrix<T>& X, const Matrix<T>& D, Filter<T>& F);
20+
21+
// Получение доступа к шагу свертки
22+
int& getStep() { return s; }
23+
24+
// Метод получения доступа к кофиценту обучения
25+
double& getE() { return E; };
26+
27+
// Операция обратного распространение ошибки c перцептрона на подвыборочный слой
28+
void Revers_Perceptron_to_CNN(Matrix<T>& a, const Weights<T>& w);
29+
30+
// Операция обратного распространение ошибки на слое "Макс пулинга"
31+
Matrix<T> ReversPooling(const Matrix<T>& a, const int& n_, const int& m_);
32+
33+
// Класс исключения ------------------------------------------------------
34+
class CNNLearningExeption : public std::runtime_error {
35+
public:
36+
CNNLearningExeption(std::string str) : std::runtime_error(str) {};
37+
~CNNLearningExeption() {};
38+
};
39+
40+
// Деструктор ------------------------------------------------------------
41+
~CNNLearning();
42+
private:
43+
NeyronCnn<T> neyron;
44+
int s;
45+
double E;
46+
};
47+
48+
template<typename T>
49+
inline CNNLearning<T>::CNNLearning(const int& s_, const double& E_):neyron(), s(s_), E(E_)
50+
{
51+
}
52+
53+
template<typename T>
54+
inline Matrix<T> CNNLearning<T>::ReversConvolution(const Matrix<T>& D, const Filter<T>& f)
55+
{
56+
if (s < 1) {
57+
throw CNNLearning<T>::CNNLearningExeption("Задан невозможный шаг свертки!");
58+
}
59+
auto F = f.roate_180();
60+
Matrix<T> O((D.getN() - 1) / s + f.getN(), (D.getM() - 1) / s + f.getM());
61+
if (s != 1) {
62+
int stepJ = 0, stepI = 0;
63+
int ii = 0, jj = 0;
64+
for (int i = 0; i < O.getN(); i++) {
65+
stepJ = 0;
66+
jj = 0;
67+
if (stepI) {
68+
for (int j = 0; j < O.getM(); j++) {
69+
O[i][j] = 0;
70+
}
71+
stepI--;
72+
}
73+
else {
74+
for (int j = 0; j < O.getM(); j++) {
75+
if (stepJ) {
76+
stepJ--;
77+
O[i][j] = 0;
78+
}
79+
else {
80+
O[i][j] = D[ii][jj++];
81+
stepJ = s;
82+
}
83+
}
84+
stepI = s;
85+
}
86+
ii++;
87+
}
88+
}
89+
else {
90+
O = D;
91+
}
92+
for (int i = 0; i < f.getN()-1; i++) {
93+
neyron.Padding(O);
94+
}
95+
return neyron.Svertka(F, O);
96+
}
97+
98+
template<typename T>
99+
void CNNLearning<T>::GradDes(const Matrix<T>& X, const Matrix<T>& D, Filter<T>& F) {
100+
Matrix<T> Delta = neyron.Svertka(D, X);
101+
if ((Delta.getN() != F.getN()) || (Delta.getM() != F.getM())) {
102+
throw typename CNNLearning<T>::CNNLearningExeption("Задана неверная размерность! После свертки размеры матрицы фильтра и матрицы ошибки не совпадают!");
103+
}
104+
T delt;
105+
for (int i = 0; i < Delta.getN(); i++) {
106+
for (int j = 0; j < Delta.getM(); j++) {
107+
delt = E * Delta[i][j];
108+
if (delt > 1000) {
109+
throw typename CNNLearning<T>::CNNLearningExeption("Слишком большая производная!");
110+
}
111+
F[i][j]-= delt;
112+
}
113+
}
114+
}
115+
116+
template<typename T>
117+
inline void CNNLearning<T>::Revers_Perceptron_to_CNN(Matrix<T>& a, const Weights<T>& w)
118+
{
119+
if ((a.getN() < 0) || (a.getM() < 0)||(a.getN() != w.getN()) || (a.getM() != w.getM())) {
120+
throw typename Base_Cnn<T>::Base_CnnExeption("Неверный размер матрицы ошибки!");
121+
}
122+
for (int i = 0; i < w.getN(); i++) {
123+
for (int j = 0; j < w.getM(); j++) {
124+
a[i][j] += w.GetD() * w[i][j];
125+
}
126+
}
127+
}
128+
129+
template<typename T>
130+
inline Matrix<T> CNNLearning<T>::ReversPooling(const Matrix<T>& D, const int & n_, const int & m_)
131+
{
132+
if ((n_ < 0) || (m_ < 0) || (n_ > D.getN()) || (m_ > D.getM())) {
133+
throw typename Base_Cnn<T>::Base_CnnExeption("Неверный размер ядра!");
134+
}
135+
136+
Matrix<T> copy(D.getN() * n_, D.getM() * m_);
137+
138+
for (int i = 0; i < D.getN(); i++) {
139+
for (int j = 0; j < D.getM(); j++) {
140+
for (int ii = i * n_; ii < i*n_ + n_; ii++) {
141+
for (int jj = j * m_; jj < j*m_ + m_; jj++) {
142+
copy[ii][jj] = D[i][j];
143+
}
144+
}
145+
}
146+
}
147+
return copy;
148+
}
149+
150+
151+
template<typename T>
152+
inline CNNLearning<T>::~CNNLearning()
153+
{
154+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
#include "CNNLearning.h"
3+
#define D_CNNLeaning CNNLearning<double>
4+
#define I_CNNLeaning CNNLearning<int>

0 commit comments

Comments
 (0)