MCPcopy Create free account
hub / github.com/arrayfire/arrayfire / perceptron_demo

Function perceptron_demo

examples/machine_learning/perceptron.cpp:73–118  ·  view source on GitHub ↗

Demo of one vs all logistic regression

Source from the content-addressed store, hash-verified

71
72// Demo of one vs all logistic regression
73int perceptron_demo(bool console, int perc) {
74 array train_images, train_targets;
75 array test_images, test_targets;
76 int num_train, num_test, num_classes;
77
78 // Load mnist data
79 float frac = (float)(perc) / 100.0;
80 setup_mnist<true>(&num_classes, &num_train, &num_test, train_images,
81 test_images, train_targets, test_targets, frac);
82
83 // Reshape images into feature vectors
84 int feature_length = train_images.elements() / num_train;
85 array train_feats = moddims(train_images, feature_length, num_train).T();
86 array test_feats = moddims(test_images, feature_length, num_test).T();
87
88 train_targets = train_targets.T();
89 test_targets = test_targets.T();
90
91 // Add a bias that is always 1
92 train_feats = join(1, constant(1, num_train, 1), train_feats);
93 test_feats = join(1, constant(1, num_test, 1), test_feats);
94
95 // Train logistic regression parameters
96 array Weights = train(train_feats, train_targets, 0.1, 0.01, 1000, true);
97
98 // Predict the results
99 array train_outputs = predict(train_feats, Weights);
100 array test_outputs = predict(test_feats, Weights);
101
102 printf("Accuracy on training data: %2.2f\n",
103 accuracy(train_outputs, train_targets));
104
105 printf("Accuracy on testing data: %2.2f\n",
106 accuracy(test_outputs, test_targets));
107
108 benchmark_perceptron(train_feats, train_targets, test_feats);
109
110 if (!console) {
111 test_outputs = test_outputs.T();
112 test_targets = test_targets.T();
113 // Get 20 random test images.
114 display_results<true>(test_images, test_outputs, test_targets, 20);
115 }
116
117 return 0;
118}
119
120int main(int argc, char **argv) {
121 int device = argc > 1 ? atoi(argv[1]) : 0;

Callers 1

mainFunction · 0.85

Calls 9

moddimsFunction · 0.85
constantFunction · 0.85
benchmark_perceptronFunction · 0.85
TMethod · 0.80
trainFunction · 0.70
predictFunction · 0.70
accuracyFunction · 0.70
joinFunction · 0.50
elementsMethod · 0.45

Tested by

no test coverage detected