A HEADER-ONLY LIBRARY PROVIDES SKLEARN-LIKE API WITH GPU SUPPORT.
- ArrayFire: a general purpose GPU library.
- Googletest: Google Testing and Mocking Framework.
#include "mlkit.hpp"
using namespace std;
using namespace mk;
int main(int argc, char **argv)
{
int device = argc > 1 ? atoi(argv[1]) : -1; // default -1
try {
if(device >= 0)
af::setBackend(AF_BACKEND_CUDA); // use gpu
else
af::setBackend(AF_BACKEND_CPU); // use cpu
af::info();
af::array X = af::randn(100, 3);
af::array y = 1 * X.col(0) + 2 * X.col(1) + 3 * X.col(2) + 4 + af::randu(100, 1) * 0.5;
linear_model::LinearRegression lr = linear_model::LinearRegression(true);
lr.fit(X, y);
cout << endl \
<< "[linear regression]" << endl \
<< "-----------------------------------------------" << endl \
<< "expect coef: [1, 2, 3], expect intercept: 4" << endl \
<< "-----------------------------------------------" << endl \
<< "fit result: " << endl;
af_print(lr.coef_);
af_print(lr.intercept_)
cout << "-----------------------------------------------" << endl;
lr.score(X, y);
} catch (af::exception &ae) {
cerr << ae.what() << endl;
}
return 0;
}
The output:
# compiler command
g++ -std=c++11 -g example.cpp -o test -I/opt/arrayfire/include -Imlkit/include -laf -L/opt/arrayfire/lib
# output
ArrayFire v3.7.0 (CPU, 64-bit Linux, build c30d5455)
[0] Intel: Intel(R) Xeon(R) Silver 4114 CPU @ 2.20GHz, 95293 MB, Max threads(20) GNU Compiler Collection(GCC/G++) 7.4.0
[linear regression]
-----------------------------------------------
expect coef: [1, 2, 3], expect intercept: 4
-----------------------------------------------
fit result:
lr.coef_
[3 1 1 1]
Offset: 1
Strides: [1 4 4 4]
0.9999
1.9851
2.9896
lr.intercept_
[1 1 1 1]
Offset: 0
Strides: [1 4 4 4]
4.2475
-----------------------------------------------
Mean Sqaure Error: 0.01791
- Statistical Learning:
- linear_model.LinearRegression
- linear_model.LogisticRegression
- neighbors.KNeighborsClassifier
- cluster.KMeans
- decomposition.PCA
- tree.DecisionTreeClassifier
- mixture.GaussianMixture
- svm.LinearSVC
- 李航. 统计学习方法[M]. 2012.
- Harrington P. Machine Learning in Action[M]. 2012.