-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWMainWindow.cpp
executable file
·139 lines (112 loc) · 3.68 KB
/
WMainWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "WMainWindow.h"
#include <QMessageBox>
WMainWindow::WMainWindow() : QWidget() {
vBox_main = new QVBoxLayout;
hBox_input = new QHBoxLayout;
lbl_input = new QLabel("Input :");
tbx_input = new QLineEdit;
hBox_input->addWidget(lbl_input);
hBox_input->addWidget(tbx_input);
hBox_input->addSpacing(100);
btn_calc = new QPushButton("Calculate");
btn_calc->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
QObject::connect(btn_calc, SIGNAL(clicked()), this, SLOT(nnActivate()));
hBox_output = new QHBoxLayout;
lbl_output = new QLabel("Output :");
tbx_output = new QLineEdit;
tbx_output->setEnabled(false);
hBox_output->addSpacing(100);
hBox_output->addWidget(lbl_output);
hBox_output->addWidget(tbx_output);
btn_train = new QPushButton("Train");
btn_train->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
QObject::connect(btn_train, SIGNAL(clicked()), this, SLOT(nnTrain()));
vBox_main->addLayout(hBox_input);
vBox_main->addWidget(btn_calc);
vBox_main->addLayout(hBox_output);
vBox_main->addWidget(btn_train);
vBox_main->setAlignment(btn_calc, Qt::AlignHCenter);
setLayout(vBox_main);
breinz = new WBrains(8, QList<int>{20, 20}, 8);
rnd = new QRandomGenerator;
}
WMainWindow::~WMainWindow() {
breinz->~WBrains();
delete breinz;
delete btn_train;
delete tbx_output;
delete lbl_output;
delete btn_calc;
delete tbx_input;
delete lbl_input;
delete hBox_output;
delete hBox_input;
delete vBox_main;
delete rnd;
}
void WMainWindow::nnActivate() {
QString outTxt = "";
for(int i = 0; i < tbx_input->text().length(); i++) {
QList<double> inputs;
uchar input = tbx_input->text().at(i).toLatin1();
inputs.append((input & 0b10000000) != 0 ? 1 : 0);
inputs.append((input & 0b01000000) != 0 ? 1 : 0);
inputs.append((input & 0b00100000) != 0 ? 1 : 0);
inputs.append((input & 0b00010000) != 0 ? 1 : 0);
inputs.append((input & 0b00001000) != 0 ? 1 : 0);
inputs.append((input & 0b00000100) != 0 ? 1 : 0);
inputs.append((input & 0b00000010) != 0 ? 1 : 0);
inputs.append((input & 0b00000001) != 0 ? 1 : 0);
QList<double> outputs = breinz->activate(inputs);
uchar chr = listToChar(outputs);
if(chr == 255)
outTxt.append(input);
else
outTxt.append(chr);
}
tbx_output->setText(outTxt);
}
void WMainWindow::nnTrain() {
QString trainInputSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
QString trainOutputSet = "DEFGHIJKLMNOPQRSTUVWXYZABCdefghijklmnopqrstuvwxyzabc ";
for(uchar c = 0; c < 255; c++)
if(!trainInputSet.contains(QChar::fromLatin1(c))) {
trainInputSet.append(c);
trainOutputSet.append(255);
}
for(int i = 0; i < LEARN_ITERATIONS; i++) {
int randomItem = rnd->generate() % trainInputSet.size();
QList<double> trainInputs = charToList(trainInputSet.at(randomItem).toLatin1());
QList<double> trainOutputs = charToList(trainOutputSet.at(randomItem).toLatin1());
breinz->train(trainInputs, trainOutputs);
}
QMessageBox::information(this, "Info", "Training finished");
}
QList<double> WMainWindow::charToList(uchar c) {
QList<double> inputs;
inputs.append((c & 0b10000000) != 0 ? 1 : 0);
inputs.append((c & 0b01000000) != 0 ? 1 : 0);
inputs.append((c & 0b00100000) != 0 ? 1 : 0);
inputs.append((c & 0b00010000) != 0 ? 1 : 0);
inputs.append((c & 0b00001000) != 0 ? 1 : 0);
inputs.append((c & 0b00000100) != 0 ? 1 : 0);
inputs.append((c & 0b00000010) != 0 ? 1 : 0);
inputs.append((c & 0b00000001) != 0 ? 1 : 0);
return inputs;
}
uchar WMainWindow::listToChar(QList<double> l) {
uchar out = 0;
int weights[] = {
0b10000000,
0b01000000,
0b00100000,
0b00010000,
0b00001000,
0b00000100,
0b00000010,
0b00000001
};
for(int i = 0; i < l.size(); i++)
out += round(l.at(i)) * weights[i];
return out;
}