DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Accelerating AI Inference With TensorRT
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Getting Started With GenAI on BigQuery: A Step-by-Step Guide

Trending

  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  • A Complete Guide to Modern AI Developer Tools
  • Dropwizard vs. Micronaut: Unpacking the Best Framework for Microservices
  • How To Develop a Truly Performant Mobile Application in 2025: A Case for Android
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Importing, Inspecting, and Scoring With MOJO Models Inside H2O

Importing, Inspecting, and Scoring With MOJO Models Inside H2O

See how to import, inspect, and score with MOJO models inside H2O.

By 
Pavel Pscheidl user avatar
Pavel Pscheidl
·
Oct. 22, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
6.4K Views

Join the DZone community and get the full member experience.

Join For Free

Image title

H2O is so refreshing.

Machine-learning models created with H2O may be exported in two basic ways:

  1. Binary format,
  2. 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 the official documentation.

You may also like:  Machine Learning With H2O — Hands-On Guide for Data Scientists

The second option is a MOJO. Unlike binary models, MOJOs are meant to productionize H2O models. Those are self-contained models, deployable into a production environment. Typically, once a model is well-performing, a MOJO is exported and given to engineers to be deployed into production, bridging the gap between engineering and data science. An in-depth description of H2O MOJOs is provided in Productionizing H2O documentation.

Since H2O release 3.26.0.8, it is possible to re-import MOJO models back into H2O and:

  1. Inspect hyperparameters used to train the model
  2. See the scoring history
  3. Predict
  4. Display variable importances
  5. Use it exactly as native H2O model, except for checkpointing

With the new MOJO import functionality, all the information about the model is 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 the H2O cluster has it still loaded, it can be re-exported again.

This functionality is available via all H2O interfaces: 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:

  1. Use MOJO import functionality directly
  2. Pre-upload the MOJO to the H2O cluster and 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:

  1. Model ID
  2. MOJO file key
  3. 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 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 the 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 clicking 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:

  1. Use MOJO import functionality directly
  2. 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.

    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.

    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.

    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:

  1. Use MOJO import functionality directly,
  2. 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.

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.

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.

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. Found a bug? Head to H2O JIRA and file an issue. Have questions? H2O offers community Gitter and Slack.

Further Reading

Anomaly Detection With Isolation Forests Using H2O

H2O (web server) Machine learning Flow (web browser)

Opinions expressed by DZone contributors are their own.

Related

  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Accelerating AI Inference With TensorRT
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Getting Started With GenAI on BigQuery: A Step-by-Step Guide

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!