-
Notifications
You must be signed in to change notification settings - Fork 0
/
nn_step.cpp
276 lines (204 loc) · 6.43 KB
/
nn_step.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <stdio.h>
#include <iostream>
#include <dinrhiw/dinrhiw.h>
#include <fenv.h>
int main(int argc, char** argv)
{
printf("NN PREDICTION MODEL\n");
#if 1
{
// FE_UNDERFLOW | FE_OVERFLOW | FE_INEXACT
feenableexcept(FE_INVALID | FE_DIVBYZERO);
}
#endif
// loads examples database
whiteice::dataset<> db;
if(db.load("models/midinotes.ds") == false){
printf("ERROR: Couldn't load database.");
return -1;
}
if(db.getNumberOfClusters() != 2){
printf("ERROR: Bad database.\n");
return -1;
}
if(db.size(0) != db.size(1) || db.size(0) == 0){
printf("ERROR: Bad database.\n");
return -1;
}
printf("%d training samples loaded\n", db.size(0));
whiteice::DBN<> inputRBM;
whiteice::DBN<> outputRBM;
std::vector< whiteice::math::vertex<> > input_samples;
std::vector< whiteice::math::vertex<> > output_samples;
for(unsigned int i=0;i<db.size(0);i++){
input_samples.push_back(db.access(0,i));
output_samples.push_back(db.access(1,i));
}
if(inputRBM.load("models/midi-input-nnetwork.conf") == false){
printf("ERROR: loading RBM model (input) failed\n");
return -1;
}
if(outputRBM.load("models/midi-output-nnetwork.conf") == false){
printf("ERROR: loading RBM model (output) failed\n");
return -1;
}
// calculate RBM feature vectors from inputs
std::vector< whiteice::math::vertex<> > input_features;
std::vector< whiteice::math::vertex<> > output_features;
whiteice::GBRBM<>& input = inputRBM.getInputGBRBM();
whiteice::GBRBM<>& output = outputRBM.getInputGBRBM();
for(unsigned int i=0;i<db.size(0);i++){
const unsigned int index = i;
{
if(input.setVisible(db.access(0, index)) == false){
printf("ERROR: input dimension mismatch when setting GBRBM input\n");
return -1;
}
if(input.reconstructDataHidden(1) == false){
printf("ERROR: GBRBM reconstruct failed\n");
return -1;
}
whiteice::math::vertex<> h;
input.getHidden(h);
input_features.push_back(h);
}
{
if(output.setVisible(db.access(1, index)) == false){
printf("ERROR: input dimension mismatch when setting GBRBM input\n");
return -1;
}
if(output.reconstructDataHidden(1) == false){
printf("ERROR: GBRBM reconstruct failed\n");
return -1;
}
whiteice::math::vertex<> h;
output.getHidden(h);
output_features.push_back(h);
}
}
//////////////////////////////////////////////////////////////////////
// now we learn neural network model for predicting then next step
std::vector<unsigned int> arch;
// 2 layer network
arch.push_back(input.getHiddenNodes());
arch.push_back(1000);
arch.push_back(output.getHiddenNodes());
whiteice::nnetwork<> net;
// net.setArchitecture(arch, whiteice::nnetwork<>::halfLinear);
// net.setNonlinearity(arch.size()-2, whiteice::nnetwork<>::pureLinear);
// net.randomize();
whiteice::dataset<> data;
// add data to dataset
{
data.createCluster("input-features", input.getHiddenNodes());
data.createCluster("output-features", output.getHiddenNodes());
data.add(0, input_features);
data.add(1, output_features);
// data.preprocess(0, whiteice::dataset<>::dnMeanVarianceNormalization);
data.preprocess(1, whiteice::dataset<>::dnMeanVarianceNormalization);
}
printf("INPUT DATA: %d samples\n", data.size(0));
printf("OUTPUT DATA: %d samples\n", data.size(1));
// pretrain using BB-RBM
{
std::vector<unsigned int> a;
a.push_back(arch[0]);
a.push_back(arch[1]);
whiteice::DBN<> dbn(a, true); // BB-RBM network
whiteice::math::blas_real<float> delta = 0.01; // 0.001
std::vector< whiteice::math::vertex<> > samples;
data.getData(0, samples);
for(const auto& s : samples){
int size = s.size();
if(size > 10) size = 10;
if(size > 0){
printf("IN %d", (int)s[0].c[0]);
for(unsigned int i=1;i<size;i++)
printf(" %d", (int)s[i].c[0]);
printf("\n");
}
}
dbn.learnWeights(samples, delta, true);
// check reconstruction..
dbn.reconstructData(samples);
for(const auto& s : samples){
int size = s.size();
if(size > 10) size = 10;
if(size > 0){
printf("OUT %d", (int)s[0].c[0]);
for(unsigned int i=1;i<size;i++)
printf(" %d", (int)s[i].c[0]);
printf("\n");
}
}
whiteice::nnetwork<>* nn = nullptr;
dbn.convertToNNetwork(data, nn);
net = *nn;
delete nn;
// disable frozen flags
{
std::vector<bool> frozen;
net.getFrozen(frozen);
for(unsigned int i=0;i<frozen.size();i++)
frozen[i] = false;
net.setFrozen(frozen);
}
net.setNonlinearity(whiteice::nnetwork<>::sigmoid);
net.setNonlinearity(net.getLayers()-1, whiteice::nnetwork<>::pureLinear);
}
printf("%d", arch[0]);
for(unsigned int i=1;i<arch.size();i++)
printf("x%d", arch[i]);
printf(" NNETWORK\n");
whiteice::math::NNGradDescent<> grad;
// whiteice::LBFGS_nnetwork<> lbfgs(net, data, true);
// whiteice::math::vertex<> w0;
// net.exportdata(w0);
{
std::vector<whiteice::nnetwork<>::nonLinearity> nls;
net.getNonlinearity(nls);
printf("NNETWORK NON-LINEARITIES\n");
for(unsigned int i=0;i<nls.size();i++)
printf("%d ", (int)nls[i]);
printf("\n");
}
grad.startOptimize(data, net, 2, 5000);
// lbfgs.minimize(w0);
unsigned int iters = 0;
while(grad.isRunning()){
// while(lbfgs.isRunning()){
sleep(1);
whiteice::math::blas_real<float> error;
const unsigned int olditers = iters;
if(grad.getSolution(net, error, iters)){
// if(lbfgs.getSolution(w0, error, iters)){
if(olditers != iters){
printf("NNETWORK OPTIMIZER %d ITER %f ERROR\n", iters, error.c[0]);
}
}
}
printf("NNETWORK OPTIMIZATION STOPPED\n");
{
whiteice::math::blas_real<float> error;
if(grad.getSolution(net, error, iters) == false){
//if(lbfgs.getSolution(w0, error, iters) == false){
printf("ERROR: getting optimization results failed\n");
return -1;
}
// net.importdata(w0);
}
// saves resulting nnetwork
{
data.clearData(0);
data.clearData(1);
if(data.save("models/data.dat") == false){
printf("ERROR: saving features dataset & preprocessing information failed\n");
return -1;
}
if(net.save("models/midisynth.conf") == false){
printf("ERROR: saving midi synth prediction failed\n");
return -1;
}
}
return 0;
}