| 236 | }; |
| 237 | |
| 238 | int dbn_demo(bool console, int perc) { |
| 239 | printf("** ArrayFire DBN Demo **\n\n"); |
| 240 | |
| 241 | array train_images, test_images; |
| 242 | array train_target, test_target; |
| 243 | int num_classes, num_train, num_test; |
| 244 | |
| 245 | // Load mnist data |
| 246 | float frac = (float)(perc) / 100.0; |
| 247 | setup_mnist<true>(&num_classes, &num_train, &num_test, train_images, |
| 248 | test_images, train_target, test_target, frac); |
| 249 | |
| 250 | int feature_size = train_images.elements() / num_train; |
| 251 | |
| 252 | // Reshape images into feature vectors |
| 253 | array train_feats = moddims(train_images, feature_size, num_train).T(); |
| 254 | array test_feats = moddims(test_images, feature_size, num_test).T(); |
| 255 | |
| 256 | train_target = train_target.T(); |
| 257 | test_target = test_target.T(); |
| 258 | |
| 259 | // Network parameters |
| 260 | vector<int> layers; |
| 261 | layers.push_back(100); |
| 262 | layers.push_back(50); |
| 263 | |
| 264 | // Create network |
| 265 | dbn network(train_feats.dims(1), num_classes, layers); |
| 266 | |
| 267 | // Train network |
| 268 | timer::start(); |
| 269 | network.train(train_feats, train_target, |
| 270 | 0.2, // rbm learning rate |
| 271 | 4.0, // nn learning rate |
| 272 | 15, // rbm epochs |
| 273 | 250, // nn epochs |
| 274 | 100, // batch_size |
| 275 | 0.5, // max error |
| 276 | true); // verbose |
| 277 | af::sync(); |
| 278 | double train_time = timer::stop(); |
| 279 | |
| 280 | // Run the trained network and test accuracy. |
| 281 | array train_output = network.predict(train_feats); |
| 282 | array test_output = network.predict(test_feats); |
| 283 | |
| 284 | // Benchmark prediction |
| 285 | af::sync(); |
| 286 | timer::start(); |
| 287 | for (int i = 0; i < 100; i++) { network.predict(test_feats); } |
| 288 | af::sync(); |
| 289 | double test_time = timer::stop() / 100; |
| 290 | |
| 291 | printf("\nTraining set:\n"); |
| 292 | printf("Accuracy on training data: %2.2f\n", |
| 293 | accuracy(train_output, train_target)); |
| 294 | |
| 295 | printf("\nTest set:\n"); |