-
Notifications
You must be signed in to change notification settings - Fork 1
/
qmlp.cpp
149 lines (131 loc) · 3.84 KB
/
qmlp.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
140
141
142
143
144
145
146
147
148
149
#include "qmlp.h"
bool QMLP::displayMQE(clock_t const &start, qreal &nextDisplayTime, const qreal & mqe, const qreal &learningRate, const qreal & refreshTime)
{
Q_UNUSED(refreshTime)
if (MLP::displayMQE(start, nextDisplayTime, mqe, learningRate, 0.1))
{
emit newSecond((clock() - start) / (qreal)CLOCKS_PER_SEC);
return 1;
}
else
return 0;
}
void QMLP::init ()
{
qRegisterMetaTypeStreamOperators<QMLP>("QMLP");
qMetaTypeId<QMLP>();
}
bool QMLP::learn(QString str)
{
QMap<QString, qint8> argname;
argname["maxError"] = 0;
argname["maxTime"] = 1;
argname["learningRate"] = 2;
argname["adaptativeLearningRate"] = 3;
argname["lambda"] = 4;
argname["lambda1"] = 5;
argname["lambda2"] = 6;
str = str.simplified();
while (str.contains(QRegExp("\\;\\S")))
str.insert(str.indexOf(QRegExp("\\;\\S"))+1, "\n");
while (str.contains(QRegExp("\\s\\:")))
str.remove(str.indexOf(QRegExp("\\s\\:")), 1);
while (str.contains(QRegExp("\\:\\s")))
str.remove(str.indexOf(QRegExp("\\:\\s"))+1, 1);
QVector<qreal> args(argname.size(), 0);
foreach(QString key,argname.keys())
if (str.contains(key+":"))
args[argname.value(key)] = str.split(key+":")[1].split(";")[0].toDouble();
bool result = MLP::learn(args[0], args[1], args[2], bool(args[3]), args[4], args[5], args[6]);
emit learningFinished();
return result;
}
QMLP QMLP::load(QIODevice *f)
{
if (f->open(QIODevice::ReadOnly))
{
QDataStream in(f);
QMLP mlp;
in >> mlp;
f->close();
return mlp;
}
else
{
QMessageBox msgBox;
msgBox.setText("File cannot be opened.");
msgBox.exec();
return QMLP();
}
}
void QMLP::save(QIODevice *f, const QMLP &mlp)
{
if(f->open(QIODevice::WriteOnly | QIODevice::Truncate))
{
QDataStream out(f);
out << mlp;
f->close();
}
else
{
QMessageBox msgBox;
msgBox.setText("File cannot be opened.");
msgBox.exec();
}
}
QDataStream & operator << (QDataStream & out, const QMLP & mlp)
{
out << (qint64) mlp.m_perLayer
<< (qint64) mlp.m_last
<< mlp.m_input
<< mlp.m_output
<< mlp.m_mean
<< (realnumber) mlp.m_sigma;
for (qint8 i = 0; i <= mlp.m_last; ++i)
out << mlp.m_layers[i];
return out;
}
QDataStream & operator >> (QDataStream & in, QMLP & mlp)
{
in >> mlp.m_perLayer
>> mlp.m_last
>> mlp.m_input
>> mlp.m_output
>> mlp.m_mean
>> mlp.m_sigma;
mlp.reset(MLP::NOTINIT);
for (qint8 i = 0; i <= mlp.m_last; ++i)
in >> mlp.m_layers[i];
return in;
}
QDataStream & operator << (QDataStream & out, const EigenMatrix & mat)
{
QString str;
for(qint64 i = 0; i < mat.rows(); ++i)
{
for(qint64 j = 0; j < mat.cols(); ++j)
str.append(QString::number(mat.coeff(i,j))+",");
str.truncate(str.size()-1);
str.append(";");
}
out << str;
return out;
}
QDataStream & operator >> (QDataStream & in, EigenMatrix & mat)
{
QString str;
QStringList strCut, rowCut;
in >> str;
qint64 jm = 0, im = str.split(";", QString::SkipEmptyParts).size();
foreach (QString row, str.split(";", QString::SkipEmptyParts))
jm = max(qint64(row.count(",")+1), jm);
mat.resize(im,jm);
strCut = str.split(";", QString::SkipEmptyParts);
for (qint64 i = 0 ; i < im ; ++i)
{
rowCut = strCut.value(i).split(",", QString::SkipEmptyParts);
for (qint64 j = 0 ; j < jm ; ++j)
mat(i,j) = rowCut.value(j).toDouble();
}
return in;
}