The C3 AI Suite has Types specifically designed to facilitate certain machine learning pipelines. Taking their inspiration from the Scikit learn machine learning pipeline. At the most general, we have the `MLPipe` Type defining the most basic behavior of the pipeline with the `
process`, and `
score` methods, and various specializations which define a whole pipeline of operations. With this structure, you can call '
train' on the top level pipeline Type and the whole pipeline is trained. Same with '
process' and '
score' to both process inputs and score results. We'll start by discussing the basic Types which are available, beginning with the abstract Types forming the basis of this Machine Learning system, then some concrete implementations of those Types and finally some examples. We'll then discuss how a new machine learning model may be ported onto the C3 AI platform.
MLPipe: An abstract Type which defines general behaviors like train, process, and score. This Type is mixed in by nearly all Types in the C3 AI Machine Learning space.
MLPipeline: An abstract Type which mixes MLPipe and connects multiple steps into a full pipeline. The provided concrete implementation of this Type is `MLSerialPipeline`. The MLPipeline contains a list of MLSteps to perform.
MLStep: A helper Type to allow MLSerialPipeline and other implementations of MLPipeline store their step pipelines and metadata.
MLLeafPipe: This is an abstract Type mixing MLPipe which is meant for steps in a machine learning pipeline which have no child steps, i.e. this step performs a specific action of the pipeline. In the terminology of the C3 AI Suite, this is a 'leaf' of the pipeline. There are several concrete versions of this Type and they are usually different implementations within various machine learning systems.
CustomPythonMLPipe: A helper Type to act as a 'base' for defining new python based machine learning Pipes.
MLSerialPipeline: This is the concrete implementation of the MLPipeline Type. Since MLSerialPipeline is so general, you won't have to subclass it.
SklearnPipe: This concrete implementation of MLLeafPipe provides a straightforward way to use sklearn machine learning pre-processing and modelling functions. See the
TensorflowClassifier: This Type allows the user to store a tensorflow estimator-based model, specifically a classifier. This Type currently works with tensorflow 1.9.0. See the
TensorflowRegressor: This Type allows the user to store a tensorflow estimator-based model, specifically a regressor. This Type currently works with tensorflow 1.9.0.
KerasPipe: This Type allows the user to store a keras tensorflow model. This Type currently uses tensorflow 2.1.0. See the
XgBoostPipe: This Type implements the sklearn-compatible part of the Xgboost library. See the
Let's take a look at the C3 AI developed TutorailIntroMLPipeline.ipynb notebook.
The first step in any machine learning task is to prepare the data. In this case, we're going to use the popular iris dataset. We first use some sklearn functions to load the data, and split it into a training set and testing set.
Now, datasets_np consists of four numpy arrays: training data, training targets, testing data, and testing targets. If we inspect the array dimensions we expect the training data and testing data to have shape '(120,4)' and the training and testing targets to have shape '(120,)'.
We must now convert this numpy array into something usable by C3 AI Suite, a Dataset. C3 AI provides the helper function
c3.Dataset.fromPython() to convert numpy arrays to datasets:
Defining the C3 MLPipeline
C3 AI Machine Learning Pipelines can be thought of as a series of steps. These steps can be nested, so we can define a 'preprocessing' step (perhaps containing multiple steps itself) which can normalize and transform data into a better form for ML algorithms, and a regression step which runs the ML model on the transformed data. So, we'll build the MLPipeline step by step.
Let's build a preprocessing pipeline which will first scale the data within the interval [0,1], then we'll do a principal component analysis and extract the first two principal components. These components will be easy for a machine learning algorithm to use.
First, let's build sklearn StandardScaler, and PCA decomposition MLLeafPipes to contain those transformation steps:
Now we can combine these two steps into a preprocessing MLSerialPipeline:
Note that we store the individual steps as `
MLStep` Types in the '
Now we have `
preprocessPipeline` containing our preprocessing pipeline! We can use this pipeline as part of a larger pipeline now.
Regression Leaf Pipe
We now need to define our logistic regression model. We'll create an SklearnPipe (a concrete MLLeafPipe) which contains this example:
This SklearnPipe now contains the sklearn model.
Compose full Pipeline
Now, we build the final pipeline:
This definition looks identical to the
preprocessPipeline from before but for one addition, the '
scoringMetrics' parameter. This parameter is for Pipelines that are meant to be trained. Here, we define the metric on which the ML algorithm should be trained to minimize. In this case, we see the
MLAccuracyMetric has been chosen. Other metrics are available as well.
Training the Pipeline
Once our MLPipeline has been defined we can use the training data from before to train it:
This produces a new Type `
trainedLr` which contains all of the parameters and model definitions of the trained MLPipeline. We can now inspect this model's parameters and evaluate it on new data:
Might return something like this:
And to evaluate on new data:
We can also score this MLPipeline based on the metric we chose with the '
Storing and Retrieving the Trained Pipeline
Once a model is trained, you can store it as a persisted MLSerialPipeline Type. You can then retrieve this model later in a different script or different component of the C3 AI Suite. Let's look at storing:
upsertedPipeline` contains an 'id' value of the upserted MLSerialPipeline object. We can retrieve this object one of two ways:
Now you can use `
process` on new data with the fetched Pipeline!
Quick Example Using KerasPipe
Using snippets from the DTI created mnistExample, we'll show quickly how you can use the KerasPipe MLLeafPipe.
Suppose we have already prepared data XTrain and YTrain. We can build a tensorflow keras model and use a KerasPipe. First, define your model as you usually would:
Then, we create a KerasPipe Type using this model definition using the '
upsertNativeModel' function. This function creates a new KerasPipe Type using the model definition object passed with the '
Then we can train the model as we did with the sklearn model:
And finally, we can use the trained pipe with the '
Several C3 AI developed Jupyter notebooks exist which demonstrate the usage of these Pipeline Types:
DTI developed notebooks:
- MNIST example: https://github.com/c3aidti/mnistExample
- Developer Documentation