Idea of the mlapi package is to provide guideline on how to implement interfaces of the machine learning models in order to have unified consistent flow. API design is mainly borrowed from very successful python scikit-learn package. At the moment scope is limited to the following base classes:
mlapiEstimation/mlapiEstimationOnline - models which implements supervised learning - regression or classification
mlapiTransformation/mlapiTransformationOnline - models which learn transformations of the data. For example model can learn TF-IDF on some matrix and apply it to the other holdout matrix
mlapiDecomposition/mlapiDecompositionOnline - models which decompose input matrix into two matrices (usually low rank). A good example could be matrix factorization where input matrix X decomposed into 2 matrices P and Q so X≈PQ.
All the base classes above suggest developer to implement set of methods and expose set of members. Developer should provide realization of the class which inherits from a corresponding base class above.
There are several agreements which helps to maintain consistent workflow.
In opposite to the most of the R packages mlapi defines models to be mutable and internally implemented as R6 classes.
Model creation is a declarative process where all the model parameters should be passed to the constructor. Model creation is separate to model fitting: model = SomeModel$new(param_1 = 1, param_2 = 10).
Depending on the base class models should implement following methods for model training:
After mlapiDecomposition/mlapiDecompositionOnline model fitting field private$components_ should be initialized (mind undescore at the end!). It should contain matrixQ (as per X≈PQ).
All the methods above should work only with matrices - dense or sparse. Dense matrices usually are from base package and sparse matrices from Matrix package.
This allows us to create concise pipelines which easy to train and apply to new data (details in next section):
Example in pseudocode
Declare models
# transformer:
# scaler just divide each column by std_dev
scaler = Scaler$new()
# decomposition:
# fits truncated SVD: X = U * S * V
# or rephrasing X = P * Q where P = U * sqrt(S); Q = sqrt(S) * V
# as a result trunc_svd$fit_transform(train) returns matrix P and learns matrix Q (stores inside model)
# when trunc_svd$transform(test) is called, model use matrix Q in order to find matrix P for `test` data
trunc_svd = SVD$new(rank = 16)
# estimator:
# fit L1/L2 regularized logistic regression
logreg = LogisticRegression(L1 = 0.1, L2 = 10)
title: “Developing machine learning models with ‘mlapi’” author: “Dmitry Selivanov” date: “2022-04-24” output: rmarkdown::html_vignette: keep_md: true vignette: > %\VignetteIndexEntry{Developing machine learning models with ‘mlapi’} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8}
mlapi
Idea of the
mlapipackage is to provide guideline on how to implement interfaces of the machine learning models in order to have unified consistent flow. API design is mainly borrowed from very successful pythonscikit-learnpackage. At the moment scope is limited to the following base classes:mlapiEstimation/mlapiEstimationOnline- models which implements supervised learning - regression or classificationmlapiTransformation/mlapiTransformationOnline- models which learn transformations of the data. For example model can learn TF-IDF on some matrix and apply it to the other holdout matrixmlapiDecomposition/mlapiDecompositionOnline- models which decompose input matrix into two matrices (usually low rank). A good example could be matrix factorization where input matrix X decomposed into 2 matrices P and Q so X≈PQ.All the base classes above suggest developer to implement set of methods and expose set of members. Developer should provide realization of the class which inherits from a corresponding base class above.
There are several agreements which helps to maintain consistent workflow.
mlapidefines models to be mutable and internally implemented asR6classes.model = SomeModel$new(param_1 = 1, param_2 = 10).fit-mlapiEstimationfit_transform-mlapiTransformation,mlapiDecompositionpartial_fit-mlapiEstimationOnline,mlapiTransformationOnline,mlapiDecompositionOnlinepredict-mlapiEstimation,mlapiEstimationOnlinetransform-mlapiTransformation,mlapiTransformationOnline,mlapiDecomposition,mlapiDecompositionOnlinemlapiDecomposition/mlapiDecompositionOnlinemodel fitting fieldprivate$components_should be initialized (mind undescore at the end!). It should contain matrix Q (as per X≈PQ).basepackage and sparse matrices fromMatrixpackage.This allows us to create concise pipelines which easy to train and apply to new data (details in next section):
Example in pseudocode
Declare models
Apply pipeline to the train data
Now all models are fitted.
Apply models to the new data
Estimator
Usage
Decomposition
Usage