测试代码如下:
void test_sda() { srand(0); double pretrain_lr = 0.1; double corruption_level = 0.3; int pretraining_epochs = 1000; double finetune_lr = 0.1; int finetune_epochs = 500; int train_N = 10; int test_N = 4; int n_ins = 28; int n_outs = 2; int hidden_layer_sizes[] = {15, 15}; int n_layers = sizeof(hidden_layer_sizes) / sizeof(hidden_layer_sizes[0]); // training data int train_X[10][28] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1} }; int train_Y[10][2] = { {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {0, 1}, {0, 1}, {0, 1}, {0, 1}, {0, 1} }; // construct SdA SdA sda(train_N, n_ins, hidden_layer_sizes, n_outs, n_layers); // pretrain sda.pretrain(*train_X, pretrain_lr, corruption_level, pretraining_epochs); // finetune sda.finetune(*train_X, *train_Y, finetune_lr, finetune_epochs); // test data int test_X[4][28] = { {1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1} }; // double test_Y[4][28]; // bug!! double test_Y[4][2]; // there are only two possible labels for each test sample // test for(int i=0; i<test_N; i++) { sda.predict(test_X[i], test_Y[i]); for(int j=0; j<n_outs; j++) { printf("%.5f ", test_Y[i][j]); } cout << endl; } } int main() { test_sda(); getchar(); return 0; }可以看到,模型先用训练集训练,然后给出测试集合,预测测试集合的label概率。期间又改了一个bug,不过这个bug无伤大雅。观察训练集合和对应的标签,发现输入sample的向量中,前半部分如果‘1’很多,则第一个标签很有可能是‘1’;否则,后一个标签很有可能是‘1’。
运行结果:
如果predict函数中的那个bug没有改过来(参见上一篇博文),则结果为:
0.34653 0.65347
0.34632 0.65368
0.18838 0.81162
0.28590 0.71410
如果predict函数中的那个bug按照我现在写的修正,则运行结果为:
0.99194 0.00806
0.99194 0.00806
0.00853 0.99147
0.00853 0.99147
后面这一组数更“好看”些,毕竟两个类别的差距分得更开。
不过在运行源程序的时候,还是有bug,windows发生段错误,可以预计是在写内存的时候,把test_sda函数的调用栈给破坏了。暂时没找到是哪个地方造成的。