Skip to content

Commit de1719e

Browse files
Merge branch 'master' into feature/CompilationStep
2 parents 2deab8a + aae4c57 commit de1719e

31 files changed

+368
-1654
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
# Changelog
22

3+
## v2.66.2.post0 (2021-10-28)
4+
5+
### Documentation Changes
6+
7+
* Update estimator docstrings to add Fast File Mode
8+
9+
## v2.66.2 (2021-10-27)
10+
11+
### Bug Fixes and Other Changes
12+
13+
* expose num_clusters parameter for clarify shap in shapconfig
14+
* Update cron job to run hourly
15+
316
## v2.66.1 (2021-10-26)
417

518
### Bug Fixes and Other Changes

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.66.2.dev0
1+
2.66.3.dev0

buildspec-deploy.yml

Lines changed: 0 additions & 29 deletions
This file was deleted.

buildspec-localmodetests.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

buildspec-notebooktests.yml

Lines changed: 0 additions & 10 deletions
This file was deleted.

buildspec-release.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

buildspec-slowtests.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

buildspec-unittests.yml

Lines changed: 0 additions & 22 deletions
This file was deleted.

buildspec.yml

Lines changed: 0 additions & 30 deletions
This file was deleted.

doc/api/inference/model.rst

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,3 @@ Model
1616
:undoc-members:
1717
:show-inheritance:
1818

19-
.. autoclass:: sagemaker.serverless.model.LambdaModel
20-
:members:
21-
:undoc-members:
22-
:show-inheritance:

doc/api/inference/predictors.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,3 @@ Make real-time predictions against SageMaker endpoints with Python objects
77
:members:
88
:undoc-members:
99
:show-inheritance:
10-
11-
.. autoclass:: sagemaker.serverless.predictor.LambdaPredictor
12-
:members:
13-
:undoc-members:
14-
:show-inheritance:

doc/overview.rst

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,50 +1063,6 @@ You can also find these notebooks in the **Advanced Functionality** section of t
10631063
For information about using sample notebooks in a SageMaker notebook instance, see `Use Example Notebooks <https://docs.aws.amazon.com/sagemaker/latest/dg/howitworks-nbexamples.html>`__
10641064
in the AWS documentation.
10651065
1066-
********************
1067-
Serverless Inference
1068-
********************
1069-
1070-
You can use the SageMaker Python SDK to perform serverless inference on Lambda.
1071-
1072-
To deploy models to Lambda, you must complete the following prerequisites:
1073-
1074-
- `Package your model and inference code as a container image. <https://docs.aws.amazon.com/lambda/latest/dg/images-create.html>`_
1075-
- `Create a role that lists Lambda as a trusted entity. <https://docs.aws.amazon.com/lambda/latest/dg/lambda-intro-execution-role.html#permissions-executionrole-console>`_
1076-
1077-
After completing the prerequisites, you can deploy your model to Lambda using
1078-
the `LambdaModel`_ class.
1079-
1080-
.. code:: python
1081-
1082-
from sagemaker.serverless import LambdaModel
1083-
1084-
image_uri = "123456789012.dkr.ecr.us-west-2.amazonaws.com/my-lambda-repository:latest"
1085-
role = "arn:aws:iam::123456789012:role/MyLambdaExecutionRole"
1086-
1087-
model = LambdaModel(image_uri=image_uri, role=role)
1088-
predictor = model.deploy("my-lambda-function", timeout=20, memory_size=4092)
1089-
1090-
The ``deploy`` method returns a `LambdaPredictor`_ instance. Use the
1091-
`LambdaPredictor`_ ``predict`` method to perform inference on Lambda.
1092-
1093-
.. code:: python
1094-
1095-
url = "https://example.com/cat.jpeg"
1096-
predictor.predict({"url": url}) # {'class': 'tabby'}
1097-
1098-
Once you are done performing inference on Lambda, free the `LambdaModel`_ and
1099-
`LambdaPredictor`_ resources using the ``delete_model`` and ``delete_predictor``
1100-
methods.
1101-
1102-
.. code:: python
1103-
1104-
model.delete_model()
1105-
predictor.delete_predictor()
1106-
1107-
.. _LambdaModel : https://sagemaker.readthedocs.io/en/stable/api/inference/model.html#sagemaker.serverless.model.LambdaModel
1108-
.. _LambdaPredictor : https://sagemaker.readthedocs.io/en/stable/api/inference/predictors.html#sagemaker.serverless.predictor.LambdaPredictor
1109-
11101066
******************
11111067
SageMaker Workflow
11121068
******************

src/sagemaker/clarify.py

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -300,17 +300,49 @@ def get_explainability_config(self):
300300
return None
301301

302302

303+
class PDPConfig(ExplainabilityConfig):
304+
"""Config class for Partial Dependence Plots (PDP).
305+
306+
If PDP is requested, the Partial Dependence Plots will be included in the report, and the
307+
corresponding values will be included in the analysis output.
308+
"""
309+
310+
def __init__(self, features=None, grid_resolution=15, top_k_features=10):
311+
"""Initializes config for PDP.
312+
313+
Args:
314+
features (None or list): List of features names or indices for which partial dependence
315+
plots must be computed and plotted. When ShapConfig is provided, this parameter is
316+
optional as Clarify will try to compute the partial dependence plots for top
317+
feature based on SHAP attributions. When ShapConfig is not provided, 'features'
318+
must be provided.
319+
grid_resolution (int): In case of numerical features, this number represents that
320+
number of buckets that range of values must be divided into. This decides the
321+
granularity of the grid in which the PDP are plotted.
322+
top_k_features (int): Set the number of top SHAP attributes to be selected to compute
323+
partial dependence plots.
324+
"""
325+
self.pdp_config = {"grid_resolution": grid_resolution, "top_k_features": top_k_features}
326+
if features is not None:
327+
self.pdp_config["features"] = features
328+
329+
def get_explainability_config(self):
330+
"""Returns config."""
331+
return copy.deepcopy({"pdp": self.pdp_config})
332+
333+
303334
class SHAPConfig(ExplainabilityConfig):
304335
"""Config class of SHAP."""
305336

306337
def __init__(
307338
self,
308-
baseline,
309-
num_samples,
310-
agg_method,
339+
baseline=None,
340+
num_samples=None,
341+
agg_method=None,
311342
use_logit=False,
312343
save_local_shap_values=True,
313344
seed=None,
345+
num_clusters=None,
314346
):
315347
"""Initializes config for SHAP.
316348
@@ -320,34 +352,49 @@ def __init__(
320352
be the same as the dataset format. Each row should contain only the feature
321353
columns/values and omit the label column/values. If None a baseline will be
322354
calculated automatically by using K-means or K-prototypes in the input dataset.
323-
num_samples (int): Number of samples to be used in the Kernel SHAP algorithm.
355+
num_samples (None or int): Number of samples to be used in the Kernel SHAP algorithm.
324356
This number determines the size of the generated synthetic dataset to compute the
325-
SHAP values.
326-
agg_method (str): Aggregation method for global SHAP values. Valid values are
357+
SHAP values. If not provided then Clarify job will choose a proper value according
358+
to the count of features.
359+
agg_method (None or str): Aggregation method for global SHAP values. Valid values are
327360
"mean_abs" (mean of absolute SHAP values for all instances),
328361
"median" (median of SHAP values for all instances) and
329362
"mean_sq" (mean of squared SHAP values for all instances).
363+
If not provided then Clarify job uses method "mean_abs"
330364
use_logit (bool): Indicator of whether the logit function is to be applied to the model
331365
predictions. Default is False. If "use_logit" is true then the SHAP values will
332366
have log-odds units.
333367
save_local_shap_values (bool): Indicator of whether to save the local SHAP values
334368
in the output location. Default is True.
335369
seed (int): seed value to get deterministic SHAP values. Default is None.
370+
num_clusters (None or int): If a baseline is not provided, Clarify automatically
371+
computes a baseline dataset via a clustering algorithm (K-means/K-prototypes).
372+
num_clusters is a parameter for this algorithm. num_clusters will be the resulting
373+
size of the baseline dataset. If not provided, Clarify job will use a default value.
336374
"""
337-
if agg_method not in ["mean_abs", "median", "mean_sq"]:
375+
if agg_method is not None and agg_method not in ["mean_abs", "median", "mean_sq"]:
338376
raise ValueError(
339377
f"Invalid agg_method {agg_method}." f" Please choose mean_abs, median, or mean_sq."
340378
)
341-
379+
if num_clusters is not None and baseline is not None:
380+
raise ValueError(
381+
"Baseline and num_clusters cannot be provided together. "
382+
"Please specify one of the two."
383+
)
342384
self.shap_config = {
343-
"baseline": baseline,
344-
"num_samples": num_samples,
345-
"agg_method": agg_method,
346385
"use_logit": use_logit,
347386
"save_local_shap_values": save_local_shap_values,
348387
}
388+
if baseline is not None:
389+
self.shap_config["baseline"] = baseline
390+
if num_samples is not None:
391+
self.shap_config["num_samples"] = num_samples
392+
if agg_method is not None:
393+
self.shap_config["agg_method"] = agg_method
349394
if seed is not None:
350395
self.shap_config["seed"] = seed
396+
if num_clusters is not None:
397+
self.shap_config["num_clusters"] = num_clusters
351398

352399
def get_explainability_config(self):
353400
"""Returns config."""
@@ -776,8 +823,9 @@ def run_explainability(
776823
data_config (:class:`~sagemaker.clarify.DataConfig`): Config of the input/output data.
777824
model_config (:class:`~sagemaker.clarify.ModelConfig`): Config of the model and its
778825
endpoint to be created.
779-
explainability_config (:class:`~sagemaker.clarify.ExplainabilityConfig`): Config of the
780-
specific explainability method. Currently, only SHAP is supported.
826+
explainability_config (:class:`~sagemaker.clarify.ExplainabilityConfig` or list):
827+
Config of the specific explainability method or a list of ExplainabilityConfig
828+
objects. Currently, SHAP and PDP are the two methods supported.
781829
model_scores(str|int|ModelPredictedLabelConfig): Index or JSONPath location in the
782830
model output for the predicted scores to be explained. This is not required if the
783831
model output is a single score. Alternatively, an instance of
@@ -811,7 +859,30 @@ def run_explainability(
811859
predictor_config.update(predicted_label_config)
812860
else:
813861
_set(model_scores, "label", predictor_config)
814-
analysis_config["methods"] = explainability_config.get_explainability_config()
862+
863+
explainability_methods = {}
864+
if isinstance(explainability_config, list):
865+
if len(explainability_config) == 0:
866+
raise ValueError("Please provide at least one explainability config.")
867+
for config in explainability_config:
868+
explain_config = config.get_explainability_config()
869+
explainability_methods.update(explain_config)
870+
if not len(explainability_methods.keys()) == len(explainability_config):
871+
raise ValueError("Duplicate explainability configs are provided")
872+
if (
873+
"shap" not in explainability_methods
874+
and explainability_methods["pdp"].get("features", None) is None
875+
):
876+
raise ValueError("PDP features must be provided when ShapConfig is not provided")
877+
else:
878+
if (
879+
isinstance(explainability_config, PDPConfig)
880+
and explainability_config.get_explainability_config()["pdp"].get("features", None)
881+
is None
882+
):
883+
raise ValueError("PDP features must be provided when ShapConfig is not provided")
884+
explainability_methods = explainability_config.get_explainability_config()
885+
analysis_config["methods"] = explainability_methods
815886
analysis_config["predictor"] = predictor_config
816887
if job_name is None:
817888
if self.job_name_prefix:

0 commit comments

Comments
 (0)