Saving external models

In some cases, there is a need to store an external model in WML project or WML deployment space. Depending on model type, it has to be saved or archived with correct extension.

Following types and file extensions are supported:

Tensorflow / Keras

This section shows how to save and archive a tensorflow (keras) model.

import tarfile
import os


# Prepare your tf / keras model
model = ... # (Sequential, Functional Model, or Model subclass)

# Set names
model_name = 'tf_model'
folder_name = 'models'

# Prepare directories
file_name = model_name + '.h5'
full_path = os.path.join(os.getcwd(), folder_name)
os.makedirs(full_path, exist_ok=True)
model_path = os.path.join(full_path, file_name)

# Save model as '.h5' file
model.save(model_path)

# Archive model
archive_name = model_name + '.tar.gz'
archive_path = os.path.join(full_path, archive_name)
with tarfile.open(archive_path, 'w:gz') as tar:
    tar.add(model_path, arcname=os.path.basename(model_path))

# Path that can be used to store model in project or space
path_to_store_model = archive_path

PMML

For PMML models, it is required to prepare a model as file in .xml format, that can be directly stored in project or space.

sample_model_path = "./models/pmml_model.xml"
path_to_store_model = sample_model_path

Scikit-learn

This section shows how to save and archive a scikit-learn model.

import joblib
import os
import tarfile


# Prepare your scikit-learn model
model = ...

# Set names
model_name = 'sklearn_model'
folder_name = 'models'

# Prepare directories
file_name = model_name + '.pkl'
full_path = os.path.join(os.getcwd(), folder_name)
os.makedirs(full_path, exist_ok=True)
model_path = os.path.join(full_path, file_name)

# Save model as '.pkl' file
with open(model_path, 'wb') as file:
    joblib.dump(model, file)

# Archive model
archive_name = model_name + '.tar.gz'
archive_path = os.path.join(full_path, archive_name)
with tarfile.open(archive_path, 'w:gz') as tar:
    tar.add(model_path, arcname=os.path.basename(model_path))

# Path that can be used to store model in project or space
path_to_store_model = archive_path

SPSS

For SPSS models, it is required to prepare a model as file in .str format, that can be directly stored in project or space.

sample_model_path = "./models/spss_model.str"
path_to_store_model = sample_model_path

Spark

This section shows how to save and archive a spark model.

import os
import tarfile


# Prepare your spark model
model = ...

# Set names
model_name = "spark_model"
folder_name = "models"

# Prepare directories
full_path = os.path.join(os.getcwd(), folder_name)
os.makedirs(full_path, exist_ok=True)
model_path = os.path.join(full_path, model_name)

# Save model
model.save(model_path)

# Archive model
archive_name = model_name + ".tar.gz"
archive_path = os.path.join(full_path, archive_name)
with tarfile.open(archive_path, 'w:gz') as tar:
    tar.add(model_path, arcname=os.path.basename(model_path))

# Path that can be used to store model in project or space
path_to_store_model = archive_path

XGBoost

This section shows how to save and archive an XGBoost model.

import os
import joblib
import tarfile


# Prepare your xgboost model
model = ...

# Set names
model_name = "xgboost_model"
folder_name = "models"

# Prepare directories
file_name = model_name + '.pkl'
full_path = os.path.join(os.getcwd(), folder_name)
os.makedirs(full_path, exist_ok=True)
model_path = os.path.join(full_path, file_name)

# Save model as '.pkl' file
with open(model_path, 'wb') as file:
    joblib.dump(model, file)

# Archive model
archive_name = model_name + '.tar.gz'
archive_path = os.path.join(full_path, archive_name)
with tarfile.open(archive_path, 'w:gz') as tar:
    tar.add(model_path, arcname=os.path.basename(model_path))

# Path that can be used to store model in project or space
path_to_store_model = archive_path