Skip to content

Model

Model Examples

Examples of using the Model Class are in the Examples section at the bottom of this page. AWS Model setup and deployment are quite complicated to do manually but the SageWorks Model Class makes it a breeze!

Model: Manages AWS Model Package/Group creation and management.

Models are automatically set up and provisioned for deployment into AWS. Models can be viewed in the AWS Sagemaker interfaces or in the SageWorks Dashboard UI, which provides additional model details and performance metrics

Model

Bases: ModelCore

Model: SageWorks Model API Class.

Common Usage
my_model = Model(name)
my_model.details()
my_model.to_endpoint()
Source code in src/sageworks/api/model.py
class Model(ModelCore):
    """Model: SageWorks Model API Class.

    Common Usage:
        ```python
        my_model = Model(name)
        my_model.details()
        my_model.to_endpoint()
        ```
    """

    def details(self, **kwargs) -> dict:
        """Retrieve the Model Details.

        Returns:
            dict: A dictionary of details about the Model
        """
        return super().details(**kwargs)

    def to_endpoint(self, name: str = None, tags: list = None, serverless: bool = True) -> Endpoint:
        """Create an Endpoint from the Model.

        Args:
            name (str): Set the name for the endpoint. If not specified, an automatic name will be generated
            tags (list): Set the tags for the endpoint. If not specified automatic tags will be generated.
            serverless (bool): Set the endpoint to be serverless (default: True)

        Returns:
            Endpoint: The Endpoint created from the Model
        """

        # Ensure the endpoint_name is valid
        if name:
            Artifact.is_name_valid(name, delimiter="-", lower_case=False)

        # If the endpoint_name wasn't given generate it
        else:
            name = self.uuid.replace("_features", "") + "-end"
            name = Artifact.generate_valid_name(name, delimiter="-")

        # Create the Endpoint Tags
        tags = [name] if tags is None else tags

        # Create an Endpoint from the Model
        model_to_endpoint = ModelToEndpoint(self.uuid, name, serverless=serverless)
        model_to_endpoint.set_output_tags(tags)
        model_to_endpoint.transform()

        # Return the Endpoint
        return Endpoint(name)

details(**kwargs)

Retrieve the Model Details.

Returns:

Name Type Description
dict dict

A dictionary of details about the Model

Source code in src/sageworks/api/model.py
def details(self, **kwargs) -> dict:
    """Retrieve the Model Details.

    Returns:
        dict: A dictionary of details about the Model
    """
    return super().details(**kwargs)

to_endpoint(name=None, tags=None, serverless=True)

Create an Endpoint from the Model.

Parameters:

Name Type Description Default
name str

Set the name for the endpoint. If not specified, an automatic name will be generated

None
tags list

Set the tags for the endpoint. If not specified automatic tags will be generated.

None
serverless bool

Set the endpoint to be serverless (default: True)

True

Returns:

Name Type Description
Endpoint Endpoint

The Endpoint created from the Model

Source code in src/sageworks/api/model.py
def to_endpoint(self, name: str = None, tags: list = None, serverless: bool = True) -> Endpoint:
    """Create an Endpoint from the Model.

    Args:
        name (str): Set the name for the endpoint. If not specified, an automatic name will be generated
        tags (list): Set the tags for the endpoint. If not specified automatic tags will be generated.
        serverless (bool): Set the endpoint to be serverless (default: True)

    Returns:
        Endpoint: The Endpoint created from the Model
    """

    # Ensure the endpoint_name is valid
    if name:
        Artifact.is_name_valid(name, delimiter="-", lower_case=False)

    # If the endpoint_name wasn't given generate it
    else:
        name = self.uuid.replace("_features", "") + "-end"
        name = Artifact.generate_valid_name(name, delimiter="-")

    # Create the Endpoint Tags
    tags = [name] if tags is None else tags

    # Create an Endpoint from the Model
    model_to_endpoint = ModelToEndpoint(self.uuid, name, serverless=serverless)
    model_to_endpoint.set_output_tags(tags)
    model_to_endpoint.transform()

    # Return the Endpoint
    return Endpoint(name)

Examples

All of the SageWorks Examples are in the Sageworks Repository under the examples/ directory. For a full code listing of any example please visit our SageWorks Examples

Create a Model from a FeatureSet

featureset_to_model.py
from sageworks.api.feature_set import FeatureSet
from sageworks.api.model import ModelType
from pprint import pprint

# Grab a FeatureSet
my_features = FeatureSet("test_features")

# Create a Model from the FeatureSet
# Note: ModelTypes can be CLASSIFIER, REGRESSOR (XGBoost is default)
my_model = my_features.to_model(model_type=ModelType.REGRESSOR, 
                                target_column="iq_score")
pprint(my_model.details())

Output

{'approval_status': 'Approved',
 'content_types': ['text/csv'],
 ...
 'inference_types': ['ml.t2.medium'],
 'input': 'test_features',
 'model_metrics':   metric_name  value
                0        RMSE  7.924
                1         MAE  6.554,
                2          R2  0.604,
 'regression_predictions':       iq_score  prediction
                            0   136.519012  139.964460
                            1   133.616974  130.819950
                            2   122.495415  124.967834
                            3   133.279510  121.010284
                            4   127.881073  113.825005
    ...
 'response_types': ['text/csv'],
 'sageworks_tags': ['test-model'],
 'shapley_values': None,
 'size': 0.0,
 'status': 'Completed',
 'transform_types': ['ml.m5.large'],
 'uuid': 'test-model',
 'version': 1}

Use a specific Scikit-Learn Model

featureset_to_knn.py
from sageworks.api.feature_set import FeatureSet
from pprint import pprint

# Grab a FeatureSet
my_features = FeatureSet("abalone_features")

# Transform FeatureSet into KNN Regression Model
# Note: model_class can be any sckit-learn model 
#  "KNeighborsRegressor", "BayesianRidge",
#  "GaussianNB", "AdaBoostClassifier", etc
my_model = my_features.to_model(
    model_class="KNeighborsRegressor",
    target_column="class_number_of_rings",
    name="abalone-knn-reg",
    description="Abalone KNN Regression",
    tags=["abalone", "knn"],
    train_all_data=True,
)
pprint(my_model.details())
Another Scikit-Learn Example

featureset_to_rfc.py
from sageworks.api.feature_set import FeatureSet
from pprint import pprint

# Grab a FeatureSet
my_features = FeatureSet("wine_features")

# Using a Scikit-Learn Model
# Note: model_class can be any sckit-learn model ("KNeighborsRegressor", "BayesianRidge",
#       "GaussianNB", "AdaBoostClassifier", "Ridge, "Lasso", "SVC", "SVR", etc...)
my_model = my_features.to_model(
    model_class="RandomForestClassifier",
    target_column="wine_class",
    name="wine-rfc-class",
    description="Wine RandomForest Classification",
    tags=["wine", "rfc"]
)
pprint(my_model.details())

Create an Endpoint from a Model

Endpoint Costs

Serverless endpoints are a great option, they have no AWS charges when not running. A realtime endpoint has less latency (no cold start) but AWS charges an hourly fee which can add up quickly!

model_to_endpoint.py
from sageworks.api.model import Model

# Grab the abalone regression Model
model = Model("abalone-regression")

# By default, an Endpoint is serverless, you can
# make a realtime endpoint with serverless=False
model.to_endpoint(name="abalone-regression-end",
                  tags=["abalone", "regression"],
                  serverless=True)

Model Health Check and Metrics

model_metrics.py
from sageworks.api.model import Model

# Grab the abalone-regression Model
model = Model("abalone-regression")

# Perform a health check on the model
# Note: The health_check() method returns 'issues' if there are any
#       problems, so if there are no issues, the model is healthy
health_issues = model.health_check()
if not health_issues:
    print("Model is Healthy")
else:
    print("Model has issues")
    print(health_issues)

# Get the model metrics and regression predictions
print(model.model_metrics())
print(model.regression_predictions())

Output

Model is Healthy
  metric_name  value
0        RMSE  2.190
1         MAE  1.544
2          R2  0.504

     class_number_of_rings  prediction
0                        9    8.648378
1                       11    9.717787
2                       11   10.933070
3                       10    9.899738
4                        9   10.014504
..                     ...         ...
495                     10   10.261657
496                      9   10.788254
497                     13    7.779886
498                     12   14.718514
499                     13   10.637320

SageWorks UI

Running these few lines of code creates an AWS Model Package Group and an AWS Model Package. These model artifacts can be viewed in the Sagemaker Console/Notebook interfaces or in the SageWorks Dashboard UI.

sageworks_new_light
SageWorks Dashboard: Models

Not Finding a particular method?

The SageWorks API Classes use the 'Core' Classes Internally, so for an extensive listing of all the methods available please take a deep dive into: SageWorks Core Classes