Skip to main content

Clara

CLARA is a tool designed to help Machine Learning developers to build their models using High-Level languages (Python), but easily implement them in C. The goal is not to convert the code, but to convert the trained model itself (the object). Therefore, this is not a code converter, but a code transpiler.

The following algorithms are available

  • Classification
    • MLP
    • Decision Tree
    • Support-Vector Machines (SVC & Nu)
    • LinearSVM
    • Gaussian Naive Bayes
    • Complement Naive Bayes
    • Multinomial Naive Bayes
    • Categorical Naive Bayes
    • Bernoulli Naive Bayes
  • Regression
    • MLP
    • Support-Vector Machines
  • Decomposition
    • PCA
  • Preprocessing
    • StandardScaler
    • KernelCenterer
    • MaxAbsScaler
    • MinMaxScaler
    • RobustScaler

Transpiling Tools

Python Class Clara Class
Decomposition
sklearn.decomposition.PCA clara.transpiler.pca.PCATranspiler
Neural Networks
sklearn.neural_network.MLPClassifier clara.transpiler.mlp.MLPCTranspiler
sklearn.neural_network.MLPRegressor clara.transpiler.mlp.MLPRTranspiler
Decision Tree
sklearn.tree.DecisionTreeClassifier clara.transpiler.tree.DecisionTreeClassifierTranspiler
Support-Vector Machines
sklearn.svm.SVC clara.transpiler.svm.SVCTranspiler
sklearn.svm.NuSVC clara.transpiler.svm.SVCTranspiler
sklearn.svm.LinearSVM clara.transpiler.svm.LinearSVMTranspiler
sklearn.svm.SVR clara.transpiler.svm.SVRTranspiler
Naive Bayes
sklearn.naive_bayes.GaussianNB clara.transpiler.naive_bayes.GaussianNBTranspiler
sklearn.naive_bayes.ComplementNB clara.transpiler.naive_bayes.ComplementNBTranspiler
sklearn.naive_bayes.MultinomialNB clara.transpiler.naive_bayes.MultinomialNBTranspiler
sklearn.naive_bayes.CategoricalNB clara.transpiler.naive_bayes.CategoricalNBTranspiler
sklearn.naive_bayes.BernoulliNB clara.transpiler.naive_bayes.BernoulliNBTranspiler
Preprocessing
sklearn.preprocessing.StandardScaler clara.transpiler.preprocessing.StandardScalerTranspiler

Syntax

Besides the multiple available algorithms, the syntax to use in any of them is the same and shown in the snippet bellow.

#The ML Algorithm you want to use
model = ScikitLearnClass()

# TRAIN YOUR MODEL FIRST!!!
model.fit()

# The correspondent CLARA TRanspiler Class
transpiler = ClaraClassTranspiler(model) #The correspondent Clara Class


# The C code to be exported to a .c file and compiled
# The code is of the model trained, therefore no retraining is needed.
c_code = transpiler.generate_code()

PCA Transpiler

Python Exporting

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_wine

data = load_wine()
dataset = np.column_stack((data.data, data.target))
scale = StandardScaler()

pca = PCA(n_components=0.8)

X = scale.fit_transform(dataset[::,:-1])
pca.fit(X)

from clara.transpiler.pca import PCATranspiler

transpiler = PCATranspiler(pca)

code = transpiler.generate_code()

with open("pca.c", "w+") as fp:
  fp.write(code)

Test code in C

The results may vary, but if they should be the same!!

int main(int argc, const char * argv[]) {
    // insert code here...
    double sample[N_FEATURES] = { 1.51861254, -0.5622498 ,  0.23205254, -1.16959318,  1.91390522,
        0.80899739,  1.03481896, -0.65956311,  1.22488398,  0.25171685,
                                  0.36217728,  1.84791957,  1.01300893};
    double scores[N_COMPONENTS] = {0};

    double inverse_sample[N_FEATURES] = {0};

    calculate_scores(sample, scores);

    printf("\nScores\n");


    for(int i = 0; i < N_COMPONENTS; i++){
        printf("%f\t", scores[i]);
    }

    printf("\n\nInverse Transform\n");

    inverse(scores, inverse_sample);

    for(int i = 0; i < N_FEATURES; i++){
        printf("%f\t", inverse_sample[i]);
    }

    printf("\n\n");

    pca_dimensions_t val;

    val = calculate_dimensions(sample);

    printf("T2 = %f, Q-Residuals: %f\n\n", val.hoteling2, val.q_residuals);



}

MLP Transpiler

Multi-Layer Perceptron are the basis of Neural Networks and Deep Learning. Our tools provides a way to transpile MLPs for regression and classification problems.

  • Note: At the current time, binary classifications are not working... Sorry*

MLPClassifier

from sklearn.neural_network import MLPClassifier
from sklearn.datasets import load_wine
import numpy as np

from clara.transpiler.mlp import MLPCTranspiler

data = load_wine()
dataset = np.column_stack((data.data, data.target))

mlp = MLPClassifier(hidden_layer_sizes=(30, 10))

mlp.fit(ddataset.data, dataset.target)


transpiler = MLPCTranspiler(mlp)

code = transpiler.generate_code()

with open("mlp.c", "w+") as fp:
  fp.write(code)

MLPRegressor

from sklearn.neural_network import MLPRegressor
from sklearn.datasets import load_boston
import numpy as np

from clara.transpiler.mlp import MLPRTranspiler

data = load_boston()
dataset = np.column_stack((data.data, data.target))

mlp = MLPClassifier(hidden_layer_sizes=(30, 10))

mlp.fit(dataset.data, dataset.target)



transpiler = MLPRTranspiler(mlp)

code = transpiler.generate_code()

with open("mlp.c", "w+") as fp:
  fp.write(code)

Test code in C

int main(){
    double s[N_FEATURES] = {14.23, 1.71, 2.43, 15.6, 127.0, 2.8, 3.06, 0.28, 2.29, 5.64, 1.04, 3.92, 1065.0};
    int class;
    for(int i = 0; i<N_FEATURES; i++){
      sample[i] = s[i];
    }
    class = predict(sample);
    return 0;
}

Cite Us

Please, if you use our tool in any of your projects, cite us. This will help us improve and look at what people may need! Thanks!

DOI: 10.5281/zenodo.3930335

Sérgio Branco. (2020, July 4). CLARA - Embedded ML Tools (Version v0.0.1). Zenodo. http://doi.org/10.5281/zenodo.3930336

@software{sergio_branco_2020_3930336,
  author       = {Sérgio Branco},
  title        = {CLARA - Embedded ML Tools},
  month        = jul,
  year         = 2020,
  publisher    = {Zenodo},
  version      = {v0.0.1},
  doi          = {10.5281/zenodo.3930336},
  url          = {https://doi.org/10.5281/zenodo.3930336}
}

Release files for clara-transpiler 0.17.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for clara-transpiler 0.17.5
File Size Uploaded
clara_transpiler-0.17.5.tar.gz 13.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for clara-transpiler 0.17.5
File Interpreter ABI Platform
clara_transpiler-0.17.5-py3-none-any.whl Python 3 none any Details

Total release size: 40.1 kB

Release files / clara_transpiler-0.17.5.tar.gz

Download URL clara_transpiler-0.17.5.tar.gz
Size 13.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b1453d35b7ae5bc6c7e21b2dd8b3148bd6989945a54a8ad17c9421bbd9b89abd
BLAKE2b-256 checksum
How to use checksums
3137ff8350fef798206d21bc6a5f327256769d9cbb755785b82502e0c48e6c79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.3

Release files / clara_transpiler-0.17.5-py3-none-any.whl

Download URL clara_transpiler-0.17.5-py3-none-any.whl
Size 26.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c9a41c0dca9cd37dfd421b8dcaaeb41125355c2f359dea7fc3d8aa50dd1855aa
BLAKE2b-256 checksum
How to use checksums
6a83413b4dbf7e978e05e54ec70c079fbf8a5d152386798befbf19dcab998b68
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.17.5 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page