Creating Models¶
To create a model, you will need to submit a model class that conforms to the specification
by rafiki.model.BaseModel, written in a single Python file.
The model’s implementation should conform to a specific task (see Supported Tasks).
To submit the model to Rafiki, use the rafiki.client.Client.create_model() method.
Implementing Models¶
Full details on how to implement a model are located in the documentation of rafiki.model.BaseModel,
and sample model implementations are located in ./examples/models/.
In defining the hyperparameters (knobs) of a model, refer to the documentation at Knob Classes for the full list of knob types.
After implementing your model, it is highly recommended to use rafiki.model.test_model_class()
to test your model. This method simulates a full train-inference flow on your model, ensuring that
it is likely to work on Rafiki.
Model Environment¶
Your model will be run in Python 3.6 with the following Python libraries pre-installed:
requests==2.20.0 numpy==1.14.5 Pillow==5.3.0
Additionally, you’ll specify a list of dependencies to be installed for your model,
prior to model training and inference. This is configurable with the dependencies option
during model creation. Alternatively, you can build a custom Docker image that extends rafikiai/rafiki_worker,
installing the required dependencies for your model. This is configurable with docker_image option
during model creation.
See also
Your model should be GPU-sensitive based on the environment variable CUDA_AVAILABLE_DEVICES (see here).
If CUDA_AVAILABLE_DEVICES is set to -1, your model should simply run on CPU. You can assume that your model has exclusive access to the GPUs listed in CUDA_AVAILABLE_DEVICES.
Logging in Models¶
By importing the global logger instance in the rafiki.model module,
you can log messages and metrics while your model is being trained, and you can
define plots to visualize your model’s training on Rafiki’s Admin Web interface.
Refer to rafiki.model.ModelLogger for full usage instructions.
See also
Dataset Loading in Models¶
The global dataset_utils instance in the rafiki.model module provides
a set of built-in dataset loading methods for common dataset types on Rafiki.
Refer to rafiki.model.ModelDatasetUtils for full usage instructions.
Sample Models¶
To illustrate how to write models on Rafiki, we have written the following:
- Sample pre-processing logic to convert common dataset formats to Rafiki’s own dataset formats in ./examples/datasets/
 - Sample models in ./examples/models/
 
Example: Testing Models for IMAGE_CLASSIFICATION¶
Download & pre-process the original Fashion MNIST dataset to the dataset format specified by
IMAGE_CLASSIFICATION:python examples/datasets/image_classification/load_mnist_format.py
Install the Python dependencies for the sample models:
pip install scikit-learn==0.20.0 pip install tensorflow==1.12.0
Test the sample models in
./examples/models/image_classificationwithrafiki.model.test_model_class():python examples/models/image_classification/SkDt.py python examples/models/image_classification/TfFeedForward.py
Example: Testing Models for POS_TAGGING¶
Download & pre-process the subsample of the Penn Treebank dataset to the dataset format specified by
POS_TAGGING:python examples/datasets/pos_tagging/load_ptb_format.py
Install the Python dependencies for the sample models:
pip install torch==0.4.1
Test the sample models in
./examples/models/pos_taggingwithrafiki.model.test_model_class():python examples/models/pos_tagging/BigramHmm.py python examples/models/pos_tagging/PyBiLstm.py