Skip to content

Commit

Permalink
updating examples and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
brifordwylie committed Dec 30, 2023
1 parent a6ebcd6 commit 5189b08
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 29 deletions.
129 changes: 125 additions & 4 deletions docs/api_classes/endpoint.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,139 @@


## Examples
Example Description 1

**Run Inference on an Endpoint**

```py title="endpoint_inference.py"
from sageworks.api.feature_set import FeatureSet
from sageworks.api.model import Model
from sageworks.api.endpoint import Endpoint

# Grab an existing Endpoint
endpoint = Endpoint("abalone-regression-end")

# SageWorks has full ML Pipeline provenance, so we can backtrack the inputs,
# get a DataFrame of data (not used for training) and run inference
model_name = endpoint.get_input()
fs_name = Model(model_name).get_input()
fs = FeatureSet(fs_name)
athena_table = fs.get_training_view_table()
df = fs.query(f"SELECT * FROM {athena_table} where training = 0")

# Run inference/predictions on the Endpoint
results = endpoint.predict(df)
print(results[["class_number_of_rings", "prediction"]])
```

**Output**

```py
Processing...
class_number_of_rings prediction
0 13 11.477922
1 12 12.316887
2 8 7.612847
3 8 9.663341
4 9 9.075263
.. ... ...
839 8 8.069856
840 15 14.915502
841 11 10.977605
842 10 10.173433
843 7 7.297976
```
**Endpoint Details**

!!!tip inline end "The details() method"
The `detail()` method on the Endpoint class provides a lot of useful information. All of the SageWorks classes have a `details()` method try it out!

```py title="endpoint_details.py"
from sageworks.api.endpoint import Endpoint
from pprint import pprint

# Get Endpoint and print out it's details
endpoint = Endpoint("abalone-regression-end")
pprint(endpoint.details())
```
example code 1

**Output**

```py
{
'input': 'abalone-regression',
'instance': 'Serverless (2GB/5)',
'model_metrics': metric_name value
0 RMSE 2.190
1 MAE 1.544
2 R2 0.504,
'model_name': 'abalone-regression',
'model_type': 'regressor',
'modified': datetime.datetime(2023, 12, 29, 17, 48, 35, 115000, tzinfo=datetime.timezone.utc),
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_tags': ['abalone', 'regression'],
'status': 'InService',
'uuid': 'abalone-regression-end',
'variant': 'AllTraffic'}
```

Example Description 2
**Endpoint Metrics**

```py title="endpoint_metrics.py"
from sageworks.api.endpoint import Endpoint

# Grab an existing Endpoint
endpoint = Endpoint("abalone-regression-end")

# SageWorks tracks both Model performance and Endpoint Metrics
model_metrics = endpoint.details()["model_metrics"]
endpoint_metrics = endpoint.endpoint_metrics()
print(model_metrics)
print(endpoint_metrics)
```
example code 2

**Output**

```py
metric_name value
0 RMSE 2.190
1 MAE 1.544
2 R2 0.504

Invocations ModelLatency OverheadLatency ModelSetupTime Invocation5XXErrors
29 0.0 0.00 0.00 0.00 0.0
30 1.0 1.11 23.73 23.34 0.0
31 0.0 0.00 0.00 0.00 0.0
48 0.0 0.00 0.00 0.00 0.0
49 5.0 0.45 9.64 23.57 0.0
50 2.0 0.57 0.08 0.00 0.0
51 0.0 0.00 0.00 0.00 0.0
60 4.0 0.33 5.80 22.65 0.0
61 1.0 1.11 23.35 23.10 0.0
62 0.0 0.00 0.00 0.00 0.0
...
```


## SageWorks UI
Running these few lines of code creates and deploys an AWS Endpoint. The Endpoint artifacts can be viewed in the Sagemaker Console/Notebook interfaces or in the SageWorks Dashboard UI. SageWorks will monitor the endpoint, plot invocations, latencies, and tracks error metrics.

<figure>
<img alt="sageworks_new_light" src="https://github.com/SuperCowPowers/sageworks/assets/4806709/50687ae1-8a5b-4faa-b45e-18c9e6271af7">
<figcaption>SageWorks Dashboard: Endpoints</figcaption>
</figure>


!!! note "Not Finding a particular method?"
The SageWorks API Classes use 'Core' Classes Internally, so you can lookup all the methods in
[SageWorks Core Classes](../core_classes/overview.md)
6 changes: 6 additions & 0 deletions examples/endpoint_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from sageworks.api.endpoint import Endpoint
from pprint import pprint

# Grab an existing Endpoint and print out it's details
endpoint = Endpoint("abalone-regression-end")
pprint(endpoint.details())
14 changes: 5 additions & 9 deletions examples/endpoint_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@
from sageworks.api.model import Model
from sageworks.api.endpoint import Endpoint

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

# By default, an Endpoint is serverless, but you can make it non-serverless
serverless = True
model.to_endpoint(name="abalone-regression-end", tags=["abalone", "regression"], serverless=serverless)

# Now we'll run inference on the endpoint
# Grab an existing Endpoint
endpoint = Endpoint("abalone-regression-end")

# SageWorks has full ML Pipeline provenance, so we can backtrack the inputs,
# get a DataFrame of data (not used for training) and run inference
fs_name = model.get_input()
model_name = endpoint.get_input()
fs_name = Model(model_name).get_input()
fs = FeatureSet(fs_name)
athena_table = fs.get_training_view_table()
df = fs.query(f"SELECT * FROM {athena_table} where training = 0")

# Run inference/predictions on the Endpoint
results = endpoint.predict(df)
print(results[["class_number_of_rings", "prediction"]])
27 changes: 12 additions & 15 deletions examples/endpoint_metrics.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
from sageworks.api.model import Model
from sageworks.api.endpoint import Endpoint
import pandas as pd

# Grab the abalone-regression Model
model = Model("abalone-regression")
pd.set_option("display.max_columns", None)
pd.set_option("display.max_rows", None)
pd.set_option("display.width", 1000)

# 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)
# Grab an existing Endpoint
endpoint = Endpoint("abalone-regression-end")

# Get the model metrics and regression predictions
print(model.model_metrics())
print(model.regression_predictions())
# SageWorks tracks both Model performance and Endpoint Metrics
model_metrics = endpoint.details()["model_metrics"]
endpoint_metrics = endpoint.endpoint_metrics()
print(model_metrics)
print(endpoint_metrics.iloc[:, 1:6])
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ nav:
theme:
name: material
palette:
- scheme: slate # Dark mode default

# Palette toggle for light mode
- scheme: default
Expand Down
5 changes: 4 additions & 1 deletion src/sageworks/api/endpoint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Endpoint: SageWorks Endpoint API Class"""
"""Endpoint: Manages AWS Endpoint creation and deployment.
Endpoints are automatically set up and provisioned for deployment into AWS.
Endpoints can be viewed in the AWS Sagemaker interfaces or in the SageWorks
Dashboard UI, which provides additional model details and performance metrics"""

# SageWorks Imports
from sageworks.core.artifacts.endpoint_core import EndpointCore
Expand Down

0 comments on commit 5189b08

Please sign in to comment.