-
Notifications
You must be signed in to change notification settings - Fork 62
/
train.cpp
157 lines (133 loc) · 3.62 KB
/
train.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
//**********************
// Deep Learning and Applications
// Piji Li
// http://www.zhizhihu.com
//*********************/
#include "deep.h"
int main(int argc, const char *argv[])
{
string ftx = "./data/test_x.txt";
string fty = "./data/test_y.txt";
int epoch = 100;
int batch_size = 0;
double gamma = 0.1; // learning rate
int k = 1; //Contrastive Divergence k
int hls[] = {500, 500, 900};
int n_layers = sizeof(hls) / sizeof(hls[0]);
int n_lables = 10;
double lbd = 0.0002; // weight cost
Conf conf(ftx, fty, epoch, batch_size, hls, k, gamma, n_layers, n_lables, lbd);
Dataset data(conf);
/*// test rbm
RBM rbm(data.N, data.n_f, 400, NULL, NULL, NULL, lbd, 0);
for(int i=0; i<epoch; i++)
{
cout << "epoch: " << i << endl;
for(int j=0; j<data.N; j++)
{
double *x = new double[data.n_f];
for(int f=0; f<data.n_f; f++)
x[f] = data.X[j][f];
rbm.train(x, gamma, k);
delete[] x;
}
ofstream fout("./model/W1");
for(int j=0; j<rbm.n_visible; j++)
{
for(int l=0; l<rbm.n_hidden; l++)
{
fout << rbm.W[l][j] << " ";
}
fout << endl;
}
fout << flush;
fout.close();
}
*/
//test lr
/*
LR lr(data, conf);
for(int i=0; i<epoch; i++)
{
cout << "epoch: " << i << endl;
for(int j=0; j<data.N; j++)
{
double *x = new double[lr.n_features];
for(int f=0; f<lr.n_features; f++)
x[f] = data.X[j][f];
int *y = new int[lr.n_labels];
y[int(data.Y[j])] = 1;
lr.train(x, y, gamma);
delete[] x;
delete[] y;
}
}
for(int j=0; j<data.N; j++)
{
double *x = new double[lr.n_features];
for(int f=0; f<lr.n_features; f++)
x[f] = data.X[j][f];
double *y = new double[lr.n_labels];
lr.predict(x, y);
cout <<data.Y[j]<<": ";
for(int i=0; i<lr.n_labels; i++)
cout <<y[i]<<" ";
cout<<endl;
delete[] y;
}
*/
DBN dbn(data, conf);
dbn.pretrain(data, conf);
for(int i=0; i<=n_layers; i++)
{
char str[] = "./model/W";
char W_l[128];
sprintf(W_l, "%s%d", str, (i+1));
ofstream fout(W_l);
if(i < n_layers)
{
for(int j=0; j<dbn.rbm_layers[i]->n_visible; j++)
{
for(int l=0; l<dbn.rbm_layers[i]->n_hidden; l++)
{
fout << dbn.rbm_layers[i]->W[l][j] << " ";
}
fout << endl;
}
}
else
{
for(int j=0; j<dbn.lr_layer->n_features; j++)
{
for(int l=0; l<dbn.lr_layer->n_labels; l++)
{
fout << dbn.lr_layer->W[l][j] << " ";
}
fout << endl;
}
}
fout << flush;
fout.close();
}
dbn.finetune(data, conf);
ftx = "./data/train_x.txt";
fty = "./data/train_y.txt";
Conf conf_(ftx, fty, epoch, batch_size, hls, k, gamma, n_layers, n_lables, lbd);
Dataset data_(conf_);
double acc_num = 0;
for(int j=0; j<data_.N; j++)
{
double *x = new double[data_.n_f];
for(int f=0; f<data_.n_f; f++)
x[f] = data_.X[j][f];
double *y = new double[conf.n_labels];
int true_label = int(data_.Y[j]);
if(dbn.predict(x, y, true_label) == 1)
acc_num++;
delete[] x;
delete[] y;
cout << j <<": Accuracy=" << acc_num/(j+1) <<endl;
}
return 0;
}