| 178 | } |
| 179 | |
| 180 | int ann_demo(bool console, int perc, const dtype dt) { |
| 181 | printf("** ArrayFire ANN Demo **\n\n"); |
| 182 | |
| 183 | array train_images, test_images; |
| 184 | array train_target, test_target; |
| 185 | int num_classes, num_train, num_test; |
| 186 | |
| 187 | // Load mnist data |
| 188 | float frac = (float)(perc) / 100.0; |
| 189 | setup_mnist<true>(&num_classes, &num_train, &num_test, train_images, |
| 190 | test_images, train_target, test_target, frac); |
| 191 | if (dt != f32) { |
| 192 | train_images = train_images.as(dt); |
| 193 | test_images = test_images.as(dt); |
| 194 | train_target = train_target.as(dt); |
| 195 | } |
| 196 | |
| 197 | int feature_size = train_images.elements() / num_train; |
| 198 | |
| 199 | // Reshape images into feature vectors |
| 200 | array train_feats = moddims(train_images, feature_size, num_train).T(); |
| 201 | array test_feats = moddims(test_images, feature_size, num_test).T(); |
| 202 | |
| 203 | train_target = train_target.T(); |
| 204 | test_target = test_target.T(); |
| 205 | |
| 206 | // Network parameters |
| 207 | vector<int> layers; |
| 208 | layers.push_back(train_feats.dims(1)); |
| 209 | layers.push_back(100); |
| 210 | layers.push_back(50); |
| 211 | layers.push_back(num_classes); |
| 212 | |
| 213 | // Create network: architecture, range, datatype |
| 214 | ann network(layers, 0.05, dt); |
| 215 | |
| 216 | // Train network |
| 217 | timer::start(); |
| 218 | network.train(train_feats, train_target, |
| 219 | 2.0, // learning rate / alpha |
| 220 | 250, // max epochs |
| 221 | 100, // batch size |
| 222 | 0.5, // max error |
| 223 | true); // verbose |
| 224 | af::sync(); |
| 225 | double train_time = timer::stop(); |
| 226 | |
| 227 | // Run the trained network and test accuracy. |
| 228 | array train_output = network.predict(train_feats); |
| 229 | array test_output = network.predict(test_feats); |
| 230 | |
| 231 | // Benchmark prediction |
| 232 | af::sync(); |
| 233 | timer::start(); |
| 234 | for (int i = 0; i < 100; i++) { network.predict(test_feats); } |
| 235 | af::sync(); |
| 236 | double test_time = timer::stop() / 100; |
| 237 | |