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

Function logit_demo

examples/machine_learning/logistic_regression.cpp:117–170  ·  view source on GitHub ↗

Demo of one vs all logistic regression

Source from the content-addressed store, hash-verified

115
116// Demo of one vs all logistic regression
117int logit_demo(bool console, int perc) {
118 array train_images, train_targets;
119 array test_images, test_targets;
120 int num_train, num_test, num_classes;
121
122 // Load mnist data
123 float frac = (float)(perc) / 100.0;
124 setup_mnist<true>(&num_classes, &num_train, &num_test, train_images,
125 test_images, train_targets, test_targets, frac);
126
127 // Reshape images into feature vectors
128 int feature_length = train_images.elements() / num_train;
129 array train_feats = moddims(train_images, feature_length, num_train).T();
130 array test_feats = moddims(test_images, feature_length, num_test).T();
131
132 train_targets = train_targets.T();
133 test_targets = test_targets.T();
134
135 // Add a bias that is always 1
136 train_feats = join(1, constant(1, num_train, 1), train_feats);
137 test_feats = join(1, constant(1, num_test, 1), test_feats);
138
139 // Train logistic regression parameters
140 array Weights =
141 train(train_feats, train_targets,
142 0.1, // learning rate (aka alpha)
143 1.0, // regularization constant (aka weight decay, aka lamdba)
144 0.01, // maximum error
145 1000, // maximum iterations
146 true); // verbose
147
148 // Predict the results
149 array train_outputs = predict(train_feats, Weights);
150 array test_outputs = predict(test_feats, Weights);
151
152 printf("Accuracy on training data: %2.2f\n",
153 accuracy(train_outputs, train_targets));
154
155 printf("Accuracy on testing data: %2.2f\n",
156 accuracy(test_outputs, test_targets));
157
158 printf("Maximum error on testing data: %2.2f\n",
159 abserr(test_outputs, test_targets));
160
161 benchmark_logistic_regression(train_feats, train_targets, test_feats);
162
163 if (!console) {
164 test_outputs = test_outputs.T();
165 // Get 20 random test images.
166 display_results<true>(test_images, test_outputs, test_targets.T(), 20);
167 }
168
169 return 0;
170}
171
172int main(int argc, char **argv) {
173 int device = argc > 1 ? atoi(argv[1]) : 0;

Callers 1

mainFunction · 0.70

Calls 10

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

Tested by

no test coverage detected