H2O - Inspecting and scoring with MOJO models inside H2O
Machine-learning models created with H2O may be exported in two basic ways:
- Binary format,
- Model Object, Optimized (MOJO).
An H2O model can be saved in a binary format, which is tied to the very specific version of H2O it has been created with. There are multiple reasons for such a restriction. One of the important reasons is that model-building algorithms may evolve in time. The algorithm’s hyperparameters, as well as the “behavior” of the algorithm itself, may change. To obtain more information about H2O models, please visit official documentation.
The second option is a MOJO. Unlike binary models, MOJOs are meant to productionize H2O models. Those are self-contained models, deployable into production environment. Typically, once a model is well-performing, a MOJO is exported and given to engineers to be deployed into production, bridging the cap between engineering and data science. In-depth description of H2O MOJOs is provided in Productionizing H2O documentation.
Since H2O release 3.26.0.7
, it is possible to re-import MOJO models back into H2O and:
- Inspect hyperparameters used to train the model,
- See the scoring history,
- Predict,
- Display variable importances,
- Use it exactly as native H2O model, except for checkpointing.
With the new MOJO import functionality, all the information about the model are available for the H2O user to inspect. Also, there is no need to use the GenModel for scoring a dataset if only the MOJO model is available - by importing it back into H2O, doing predictions with such imported model are made available. And in case the MOJO gets lost and H2O cluster has it still loaded, it can be re-exported again.
This functionality is available via all H2O intefaces: Flow, Python & R.
Note: Besides MOJO, a similar functionality named POJO used to exist in H2O as well. POJOs are now deprecated and the functionality described in this article does not apply to POJOs.
Flow
There are two ways to import a MOJO using Flow:
- Use MOJO import functionality directly,
- Pre-upload the MOJO to the H2O cluster, then use MOJO import functionality.
To access MOJO import, in the upmost menu of Flow, select the “Model” option and in the bottom part of the menu, then click on “Import MOJO Model”. A dialogue appears, asking for:
- Model ID,
- MOJO file key,
- Path to the MOJO.
Model ID is already pre-generated by H2O and editing it is optional. MOJO file key is an optional parameter, usable when a MOJO was pre-uploaded from H2O user̈́’s local filesystem to the cluster and then imported. If the MOJO zip file file saved out of reach of the H2O cluster, clicking on the “Data” option in Flow’s upmost menu and then using the “Upload file” dialogue to upload the MOJO first makes it possible for a MOJO to be imported.
By default, only the last input box named path
is filled by the user. It represents path on H2O cluster’s filesystem to the import MOJO zip file.
By clicking on the import
button, the MOJO model is actually imported and registered inside H2O. From now on, it can be used like a normal H2O model, with a few restrictions listed above. By cliking on the View
button, import MOJO model’s details can be displayed.
Notice the predict
button is active - users are able to make predictions with imported MOJO models.
Python
As in Flow and R, there are two ways to import a MOJO using Flow:
- Use MOJO import functionality directly,
- Pre-upload the MOJO to the H2O cluster, then use MOJO import functionality.
The pre-upload functionality is useful when the MOJO model can not be imported directly, being out of reach of H2O cluster’s filesystem, e.g. residing on user’s local filesystem. Simply uploading the MOJO using h2o.upload_file('/some/path/to/mojo.zip')
and then using the import functionality solves this problem. However, uploading the file manually and then calling the H2OGenericEstimator
’s constructor introduces a lot of overhead. Therefore, we’ve introduced h2o.upload_mojo('/path/to/some/mojo.zip')
convenience function. However, for the simplest of use cases, there is a function named h2o.import_mojo('/some/path/to/mojo.zip')
. This function takes a path accessible by the H2O cluster, imports the MOJO and creates an H2OGenericEstimator
. Such H2OGenericEstimator
can hold any model, including all kinds of imported MOJO models, hence the name Generic
.
Such a model can then be used to do predictions, just like any H2O model with mojo_model.predict(airlines)
. In fact, all the parameters, the scoring history, it’s all there ! The listing below shows a basic use-case where a GBM model is created, saved into a temporary MOJO zip file and then loaded back into H2O. Once the model is imported, making predictions with the imported MOJO model is demonstrated using h2o.predict
function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
airlines_data = h2o.import_file("https://s3.amazonaws.com/h2o-airlines-unpacked/allyears2k.csv")
## Create a GBM model, only to later export it as a MOJO
from h2o.estimators import H2OGradientBoostingEstimator
original_model = H2OGradientBoostingEstimator(ntrees = 1)
original_model.train(x = ["Origin", "Dest"], y = "IsDepDelayed", training_frame=airlines_data)
#Save the previously created model into a temporary file
import tempfile
original_model_filename = tempfile.mkdtemp()
original_model_filename = original_model.download_mojo(original_model_filename)
# Load the model from the temporary file
mojo_model = h2o.import_mojo(original_model_filename)
predictions = mojo_model.predict(airlines_data)
As an alternative to the h2o.import_mojo('/some/path/to/mojo.zip')
function, creating a generic model directly is possible as well with the H2OGenericEstimator.from_file('/some/path/to/mojo.zip')
function. The result is exactly the same as with the h2o.import_mojo
function. See the runnable example below for comparison.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
airlines_data = h2o.import_file("https://s3.amazonaws.com/h2o-airlines-unpacked/allyears2k.csv")
## Create a GBM model, only to later export it as a MOJO
from h2o.estimators import H2OGradientBoostingEstimator
original_model = H2OGradientBoostingEstimator(ntrees = 1)
original_model.train(x = ["Origin", "Dest"], y = "IsDepDelayed", training_frame=airlines_data)
#Save the previously created model into a temporary file
import tempfile
original_model_filename = tempfile.mkdtemp()
original_model_filename = original_model.download_mojo(original_model_filename)
# Load the model from the temporary file using H2OGenericEstimator
from h2o.estimators import H2OGenericEstimator
mojo_model = H2OGenericEstimator.from_file(original_model_filename)
predictions = mojo_model.predict(airlines_data)
Upload a MOJO in Python
If the MOJO zip file is not reachable by the H2O cluster, it would need to be uploaded first with h2o.upload_file('path/to/some/mojo.zip')
and then, the key to the uploaded file would be required to be supplied to the H2OGenericEstimator
’s constructor. However, for uploading a MOJO not reachable directly by the H2O cluster, there is a convenience function h2o.upload_mojo('/path/to/some/mojo.zip')
. Internally, the MOJO zip file is uploaded into H2O and represented as a Frame
of bytes. Afterwards, the key of such byte Frame is supplied to the H2OGenericEstimator
, creating a Generic model by using the provided frame, instead of trying to import a file from cluster’s filesystem.
A fully reproducible example is to be found in the following example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
airlines_data = h2o.import_file("https://s3.amazonaws.com/h2o-airlines-unpacked/allyears2k.csv")
## Create a GBM model, only to later export it as a MOJO
from h2o.estimators import H2OGradientBoostingEstimator
original_model = H2OGradientBoostingEstimator(ntrees = 1)
original_model.train(x = ["Origin", "Dest"], y = "IsDepDelayed", training_frame=airlines_data)
#Save the previously created model into a temporary file
import tempfile
original_model_filename = tempfile.mkdtemp()
original_model_filename = original_model.download_mojo(original_model_filename)
# Upload a MOJO model and create a Generic model out of it
mojo_model = h2o.upload_mojo(original_model_filename)
predictions = mojo_model.predict(airlines_data)
R
As in Flow and Python, there are two ways to import a MOJO using Flow:
- Use MOJO import functionality directly,
- Pre-upload the MOJO to the H2O cluster, then use MOJO import functionality.
The pre-upload functionality is useful when the MOJO model can not be imported directly, being out of reach of H2O cluster’s filesystem, e.g. residing on user’s local filesystem. Simply uploading the MOJO using h2o.upload_file('/some/path/to/mojo.zip')
and then using the h2o.generic(model_key = 'some_model_key')
functionality solves this problem, but is a lot of work to do. Therefore, there is a convenience function named h2o.upload_mojo('/path/to/some/mojo.zip)
, which does everything in a single call. MOJO upload in R has its dedicated section below, named “Upload a MOJO in R”. However, for the simplest of use cases, there is a function named h2o.import_mojo('/some/path/to/mojo.zip')
. This function takes a path accessible by the H2O cluster, imports the MOJO and creates an H2OGenericEstimator
. Such H2OGenericEstimator
can hold any model, including all kinds of imported MOJO models, hence the name Generic
.
Such a model can then be used to do predictions, just like any H2O model with mojo_model.predict(airlines)
. In fact, all the parameters, the scoring history, it’s all there ! The listing below shows a basic use-case where a GBM model is created, saved into a temporary MOJO zip file and then loaded back into H2O. Once the model is imported, making predictions with the imported MOJO model is demonstrated using h2o.predict
function.
1
2
3
4
5
6
7
8
9
10
11
12
airlines_data <- h2o.importFile("https://s3.amazonaws.com/h2o-airlines-unpacked/allyears2k.csv")
## Create a GBM model, only to later export it as a MOJO
original_model <- h2o.gbm(x = c("Origin", "Dest"), y = "IsDepDelayed", training_frame=airlines_data, ntrees = 1)
#Save the previously created model into a temporary file
original_mojo_path <- h2o.download_mojo(model = original_model, path = tempdir())
original_mojo_path <- paste0(tempdir(),"/",original_mojo_path)
# Load the model from the temporary file
mojo_model <- h2o.import_mojo(original_mojo_path)
predictions <- h2o.predict(mojo_model, airlines_data)
As an alternative to the h2o.import_mojo('/some/path/to/mojo.zip')
function, creating a generic model is also possible by calling h2o.genericModel('/some/path/to/mojo.zip')
function. The result is exactly the same as with the h2o.import_mojo
function. See the runnable example below for comparison.
1
2
3
4
5
6
7
8
9
10
11
12
airlines_data <- h2o.importFile("https://s3.amazonaws.com/h2o-airlines-unpacked/allyears2k.csv")
## Create a GBM model, only to later export it as a MOJO
original_model <- h2o.gbm(x = c("Origin", "Dest"), y = "IsDepDelayed", training_frame=airlines_data, ntrees = 1)
#Save the previously created model into a temporary file
original_mojo_path <- h2o.download_mojo(model = original_model, path = tempdir())
original_mojo_path <- paste0(tempdir(),"/",original_mojo_path)
# Load the model from the temporary file
mojo_model <- h2o.genericModel(original_mojo_path)
predictions <- h2o.predict(mojo_model, airlines_data)
Upload a MOJO in R
If the MOJO zip file is not reachable by the H2O cluster, it would need to be uploaded first with h2o.upload_file('path/to/some/mojo.zip')
and then, the key to the uploaded file would be required to be supplied to the h2o.generic
function. However, for uploading a MOJO not reachable directly by the H2O cluster, there is a convenience function h2o.upload_mojo('/path/to/some/mojo.zip')
. Internally, the MOJO zip file is uploaded into H2O and represented as a Frame
of bytes. Afterwards, the key of such byte Frame is supplied to the h2o.generic(model_key = 'some_h2o_key')
, creating a Generic model by using the provided frame, instead of trying to import a file from cluster’s filesystem.
A fully reproducible example is to be found in the following example.
1
2
3
4
5
6
7
8
9
10
11
12
airlines_data <- h2o.importFile("https://s3.amazonaws.com/h2o-airlines-unpacked/allyears2k.csv")
## Create a GBM model, only to later export it as a MOJO
original_model <- h2o.gbm(x = c("Origin", "Dest"), y = "IsDepDelayed", training_frame=airlines_data, ntrees = 1)
#Save the previously created model into a temporary file
original_mojo_path <- h2o.download_mojo(model = original_model, path = tempdir())
original_mojo_path <- paste0(tempdir(),"/",original_mojo_path)
# Load the model from the temporary file
mojo_model <- h2o.upload_mojo(original_mojo_path)
predictions <- h2o.predict(mojo_model, airlines_data)
Documentation and final thoughts
The H2O MOJO import functionality evolves over time. To explore all of the functionality and possible limitations, please visit H2O MOJO Import official documentation.
Remember, H2O.ai is open-source and can be found on GitHub. Have questions ? H2O offers community Gitter and Slack.