Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // neuralNetwork.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "math.h"
- #include <iostream>
- //количество элементов в столбце
- #define column 5
- //количество элементов в ряду
- #define row 3
- //Количество данных для обучения
- #define numberDataTraining 10000
- using namespace std;
- //Набор данных
- struct Number {
- //Value for learn
- const bool num0[column][row] = {{ 1,1,1 },
- { 1,0,1 },
- { 1,0,1 },
- { 1,0,1 },
- { 1,1,1 } };
- const bool num1[column][row] = { { 0,0,1 },
- { 0,0,1 },
- { 0,0,1 },
- { 0,0,1 },
- { 0,0,1 } };
- const bool num2[column][row] = { { 1,1,1 },
- { 0,0,1 },
- { 1,1,1 },
- { 1,0,0 },
- { 1,1,1 } };
- const bool num3[column][row] = { { 1,1,1 },
- { 0,0,1 },
- { 1,1,1 },
- { 0,0,1 },
- { 1,1,1 } };
- const bool num4[column][row] = { { 1,0,1 },
- { 1,0,1 },
- { 1,1,1 },
- { 0,0,1 },
- { 0,0,1 } };
- const bool num5[column][row] = { { 1,1,1 },
- { 1,0,0 },
- { 1,1,1 },
- { 0,0,1 },
- { 1,1,1 } };
- const bool num6[column][row] = { { 1,1,1 },
- { 1,0,0 },
- { 1,1,1 },
- { 1,0,1 },
- { 1,1,1 } };
- const bool num7[column][row] = { { 1,1,1 },
- { 0,0,1 },
- { 0,0,1 },
- { 0,0,1 },
- { 0,0,1 } };
- const bool num8[column][row] = { { 1,1,1 },
- { 1,0,1 },
- { 1,1,1 },
- { 1,0,1 },
- { 1,1,1 } };
- const bool num9[column][row] = { { 1,1,1 },
- { 1,0,1 },
- { 1,1,1 },
- { 0,0,1 },
- { 1,1,1 } };
- //Тестовый набор 5
- bool num10[column][row] = { { 1,1,1 },
- { 1,0,0 },
- { 1,1,1 },
- { 0,0,0 },
- { 1,1,1 } };
- bool num11[column][row] = { { 1,1,1 },
- { 1,0,0 },
- { 0,1,0 },
- { 0,0,1 },
- { 1,1,1 } };
- bool num12[column][row] = { { 1,1,1 },
- { 1,0,0 },
- { 0,1,1 },
- { 0,0,1 },
- { 1,1,1 } };
- bool num13[column][row] = { { 1,1,0 },
- { 1,0,0 },
- { 1,1,1 },
- { 0,0,1 },
- { 1,1,1 } };
- bool num14[column][row] = { { 1,1,0 },
- { 1,0,0 },
- { 1,1,1 },
- { 0,0,1 },
- { 0,1,1 } };
- bool num15[column][row] = { { 1,1,1 },
- { 1,0,0 },
- { 1,0,1 },
- { 0,0,1 },
- { 1,1,1 } };
- bool num16[column][row] = { { 1,1,1 },
- { 0,0,0 },
- { 1,1,1 },
- { 0,0,0 },
- {1,1,1} };
- //тестовый набор 6
- bool num17[column][row] = { { 1,1,1 },
- { 1,0,0 },
- { 1,1,1 },
- { 1,0,1 },
- { 1,1,1 } };
- bool num18[column][row] = { { 1,1,1 },
- { 1,0,0 },
- { 1,0,1 },
- { 1,0,1 },
- { 1,1,1 } };
- }num;
- //класс нейрона
- class Neural {
- //входной параметр
- bool number[column][row];
- //весы входных параметров
- int weight[column][row];
- //порог функции активации
- int bias;
- public:
- Neural(int bias=7);
- void setNumber(int number);
- void setNumber(const bool number[column][row]);
- void increase();
- void decrease();
- bool proceed();
- void show();
- };
- //Начальная инициализация весов
- Neural::Neural(int bias) {
- this->bias = bias;
- for (int i = 0; i < column; i++) {
- for (int j = 0; j < row; j++) {
- weight[i][j] = 0;
- }
- }
- }
- //Вывод весоов на экран
- void Neural::show() {
- for (int i = 0; i < column; i++) {
- for (int j = 0; j < row; j++) {
- cout << weight[i][j]<<" ";
- }
- cout << endl;
- }
- }
- //Функция активации
- bool Neural::proceed() {
- int result = 0;
- for (int i = 0; i < column; i++) {
- for (int j = 0; j < row; j++) {
- result += (number[i][j] * weight[i][j]);
- }
- }
- return (result >= bias);
- }
- //установка входного параметра
- void Neural::setNumber(const bool number[column][row]) {
- for (int i = 0; i < column; i++) {
- for (int j = 0; j < row; j++) {
- this->number[i][j] = number[i][j];
- }
- }
- }
- //установка входного параметра
- void Neural::setNumber(int number) {
- switch(number){
- case 0:setNumber(num.num0); break;
- case 1:setNumber(num.num1); break;
- case 2:setNumber(num.num2); break;
- case 3:setNumber(num.num3); break;
- case 4:setNumber(num.num4); break;
- case 5:setNumber(num.num5); break;
- case 6:setNumber(num.num6); break;
- case 7:setNumber(num.num7); break;
- case 8:setNumber(num.num8); break;
- case 9:setNumber(num.num9);
- }
- }
- //Увеличение весов
- void Neural::increase() {
- for (int i = 0; i < column; i++) {
- for (int j = 0; j < row; j++) {
- if (number[i][j]) {
- weight[i][j] += 1;
- }
- }
- }
- }
- //Уменьшение весов
- void Neural::decrease() {
- for (int i = 0; i < column; i++) {
- for (int j = 0; j < row; j++) {
- if (number[i][j]) {
- weight[i][j] -= 1;
- }
- }
- }
- }
- //функция обучения
- void training(Neural *neural, int number);
- int main()
- {
- Number number;
- //Установка порога функции активации
- Neural *neural = new Neural(9);
- //
- training(neural,5);
- neural->show();
- cout << "=============CHECKING THE RECOGNITION OF NUMBERS=================" << endl;
- neural->setNumber(0);
- cout << "5 is 0 ? " << neural->proceed() << endl;
- neural->setNumber(1);
- cout << "5 is 1 ? " <<neural->proceed() << endl;
- neural->setNumber(2);
- cout << "5 is 2 ? " << neural->proceed() << endl;
- neural->setNumber(3);
- cout << "5 is 3 ? " << neural->proceed() << endl;
- neural->setNumber(4);
- cout << "5 is 4 ? " << neural->proceed() << endl;
- neural->setNumber(5);
- cout << "5 is 5 ? " << neural->proceed() << endl;
- neural->setNumber(6);
- cout << "5 is 6 ? " << neural->proceed() << endl;
- neural->setNumber(7);
- cout << "5 is 7 ? " << neural->proceed() << endl;
- neural->setNumber(8);
- cout << "5 is 8 ? " << neural->proceed() << endl;
- neural->setNumber(9);
- cout << "5 is 9 ? " << neural->proceed() << endl;
- //*********************************************************
- cout<<"================RECOGNITION 5===================================="<<endl;
- neural->setNumber(num.num10);
- cout << "5 is 51 ? " << (bool)neural->proceed() << endl;
- neural->setNumber(num.num11);
- cout << "5 is 52 ? " << (bool)neural->proceed() << endl;
- neural->setNumber(num.num12);
- cout << "5 is 53 ? " << (bool)neural->proceed() << endl;
- neural->setNumber(num.num13);
- cout << "5 is 54 ? " << (bool)neural->proceed() << endl;
- neural->setNumber(num.num14);
- cout << "5 is 55 ? " << (bool)neural->proceed() << endl;
- neural->setNumber(num.num15);
- cout << "5 is 56 ? " << (bool)neural->proceed() << endl;
- neural->setNumber(num.num16);
- cout << "5 is 57 ? " << (bool)neural->proceed() << endl;
- cout << "=================================================================" << endl;
- }
- void training(Neural *neural,int number) {
- for (int i = 0; i <numberDataTraining; i++) {
- int random_number =rand() % 10;
- neural->setNumber(random_number);
- if (random_number == number) {
- if (!(neural->proceed())) neural->increase();
- }
- else {
- if (neural->proceed()) neural->decrease();
- }
- }
- cout << "=================================" << endl;
- }
RAW Paste Data