Skip to content

Commit ff596ea

Browse files
committed
Merge remote-tracking branch 'public/master' into pytorch-release
2 parents 73b0daa + 3008a29 commit ff596ea

15 files changed

+636
-129
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
CHANGELOG
33
=========
44

5+
1.4.2dev
6+
========
7+
8+
* bug-fix: Unit Tests: Improve unit test runtime
9+
* bug-fix: Estimators: Fix attach for LDA
10+
511
1.4.1
612
=====
713

@@ -18,6 +24,7 @@ CHANGELOG
1824
* feature: Analytics: Add functions for metrics in Training and Hyperparameter Tuning jobs
1925
* feature: Estimators: add support for tagging training jobs
2026

27+
2128
1.3.0
2229
=====
2330

README.rst

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Table of Contents
3030
5. `Chainer SageMaker Estimators <#chainer-sagemaker-estimators>`__
3131
6. `AWS SageMaker Estimators <#aws-sagemaker-estimators>`__
3232
7. `BYO Docker Containers with SageMaker Estimators <#byo-docker-containers-with-sagemaker-estimators>`__
33-
8. `BYO Model <#byo-model>`__
33+
8. `SageMaker Automatic Model Tuning <#sagemaker-automatic-model-tuning>`__
34+
9. `BYO Model <#byo-model>`__
3435

3536

3637
Getting SageMaker Python SDK
@@ -263,6 +264,86 @@ Please refer to the full example in the examples repo:
263264
The example notebook is is located here:
264265
``advanced_functionality/scikit_bring_your_own/scikit_bring_your_own.ipynb``
265266

267+
268+
SageMaker Automatic Model Tuning
269+
--------------------------------
270+
271+
All of the estimators can be used with SageMaker Automatic Model Tuning, which performs hyperparameter tuning jobs.
272+
A hyperparameter tuning job runs multiple training jobs that differ by the values of their hyperparameters to find the best training job.
273+
It then chooses the hyperparameter values that result in a model that performs the best, as measured by a metric that you choose.
274+
If you're not using an Amazon ML algorithm, then the metric is defined by a regular expression (regex) you provide for going through the training job's logs.
275+
You can read more about SageMaker Automatic Model Tuning in the `AWS documentation <https://docs.aws.amazon.com/sagemaker/latest/dg/automatic-model-tuning.html>`__.
276+
277+
The SageMaker Python SDK contains a ``HyperparameterTuner`` class for creating and interacting with hyperparameter training jobs.
278+
Here is a basic example of how to use it:
279+
280+
.. code:: python
281+
282+
from sagemaker.tuner import HyperparameterTuner, ContinuousParameter
283+
284+
# Configure HyperparameterTuner
285+
my_tuner = HyperparameterTuner(estimator=my_estimator, # previously-configured Estimator object
286+
objective_metric_name='validation-accuracy',
287+
hyperparameter_ranges={'learning-rate': ContinuousParameter(0.05, 0.06)},
288+
metric_definitions=[{'Name': 'validation-accuracy', 'Regex': 'validation-accuracy=(\d\.\d+)'}],
289+
max_jobs=100,
290+
max_parallel_jobs=10)
291+
292+
# Start hyperparameter tuning job
293+
my_tuner.fit({'train': 's3://my_bucket/my_training_data', 'test': 's3://my_bucket_my_testing_data'})
294+
295+
# Deploy best model
296+
my_predictor = my_tuner.deploy(initial_instance_count=1, instance_type='ml.m4.xlarge')
297+
298+
# Make a prediction against the SageMaker endpoint
299+
response = my_predictor.predict(my_prediction_data)
300+
301+
# Tear down the SageMaker endpoint
302+
my_tuner.delete_endpoint()
303+
304+
This example shows a hyperparameter tuning job that creates up to 100 training jobs, running up to 10 at a time.
305+
Each training job's learning rate will be a value between 0.05 and 0.06, but this value will differ between training jobs.
306+
You can read more about how these values are chosen in the `AWS documentation <https://docs.aws.amazon.com/sagemaker/latest/dg/automatic-model-tuning-how-it-works.html>`__.
307+
308+
A hyperparameter range can be one of three types: continuous, integer, or categorical.
309+
The SageMaker Python SDK provides corresponding classes for defining these different types.
310+
You can define up to 20 hyperparameters to search over, but each value of a categorical hyperparameter range counts against that limit.
311+
312+
If you are using an Amazon ML algorithm, you don't need to pass in anything for ``metric_definitions``.
313+
In addition, the ``fit()`` call uses a list of ``RecordSet`` objects instead of a dictionary:
314+
315+
.. code:: python
316+
317+
# Create RecordSet object for each data channel
318+
train_records = RecordSet(...)
319+
test_records = RecordSet(...)
320+
321+
# Start hyperparameter tuning job
322+
my_tuner.fit([train_records, test_records])
323+
324+
There is also an analytics object associated with each ``HyperparameterTuner`` instance that presents useful information about the hyperparameter tuning job.
325+
For example, the ``dataframe`` method gets a pandas dataframe summarizing the associated training jobs:
326+
327+
.. code:: python
328+
329+
# Retrieve analytics object
330+
my_tuner_analytics = my_tuner.analytics()
331+
332+
# Look at summary of associated training jobs
333+
my_dataframe = my_tuner_analytics.dataframe()
334+
335+
For more detailed examples of running hyperparameter tuning jobs, see:
336+
337+
- `Using the TensorFlow estimator with hyperparameter tuning <https://github.com/awslabs/amazon-sagemaker-examples/blob/master/hyperparameter_tuning/tensorflow_mnist/hpo_tensorflow_mnist.ipynb>`__
338+
- `Bringing your own estimator for hyperparameter tuning <https://github.com/awslabs/amazon-sagemaker-examples/blob/master/hyperparameter_tuning/r_bring_your_own/hpo_r_bring_your_own.ipynb>`__
339+
- `Analyzing results <https://github.com/awslabs/amazon-sagemaker-examples/blob/master/hyperparameter_tuning/analyze_results/HPO_Analyze_TuningJob_Results.ipynb>`__
340+
341+
For more detailed explanations of the classes that this library provides for automatic model tuning, see:
342+
343+
- `API docs for HyperparameterTuner and parameter range classes <https://sagemaker.readthedocs.io/en/latest/tuner.html>`__
344+
- `API docs for analytics classes <https://sagemaker.readthedocs.io/en/latest/analytics.html>`__
345+
346+
266347
FAQ
267348
---
268349

doc/analytics.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Analytics
2+
---------
3+
4+
.. autoclass:: sagemaker.analytics.AnalyticsMetricsBase
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:
8+
9+
.. autoclass:: sagemaker.analytics.HyperparameterTuningJobAnalytics
10+
:members:
11+
:undoc-members:
12+
:show-inheritance:
13+
14+
.. autoclass:: sagemaker.analytics.TrainingJobAnalytics
15+
:members:
16+
:undoc-members:
17+
:show-inheritance:

doc/index.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Amazon SageMaker Python SDK is an open source library for training and deploying
44

55
With the SDK, you can train and deploy models using popular deep learning frameworks: **Apache MXNet** and **TensorFlow**. You can also train and deploy models with **algorithms provided by Amazon**, these are scalable implementations of core machine learning algorithms that are optimized for SageMaker and GPU training. If you have **your own algorithms** built into SageMaker-compatible Docker containers, you can train and host models using these as well.
66

7-
Here you'll find API docs for SageMaker Python SDK. The project home-page is in Github: https://github.com/aws/sagemaker-python-sdk, there you can find the SDK source, installation instructions and a general overview of the library there.
7+
Here you'll find API docs for SageMaker Python SDK. The project home-page is in Github: https://github.com/aws/sagemaker-python-sdk, there you can find the SDK source, installation instructions and a general overview of the library there.
88

99
Overview
1010
----------
@@ -14,9 +14,11 @@ The SageMaker Python SDK consists of a few primary interfaces:
1414
:maxdepth: 2
1515

1616
estimators
17+
tuner
1718
predictors
1819
session
1920
model
21+
analytics
2022

2123
MXNet
2224
----------

doc/tuner.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
HyperparameterTuner
2+
-------------------
3+
4+
.. autoclass:: sagemaker.tuner.HyperparameterTuner
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:
8+
9+
.. autoclass:: sagemaker.tuner.ContinuousParameter
10+
:members:
11+
:undoc-members:
12+
:show-inheritance:
13+
14+
.. autoclass:: sagemaker.tuner.IntegerParameter
15+
:members:
16+
:undoc-members:
17+
:show-inheritance:
18+
19+
.. autoclass:: sagemaker.tuner.CategoricalParameter
20+
:members:
21+
:undoc-members:
22+
:show-inheritance:

src/sagemaker/amazon/lda.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ def __init__(self, role, train_instance_type, num_topics,
7878
tol (float): Optional. Target error tolerance for the ALS phase of the algorithm.
7979
**kwargs: base class keyword argument values.
8080
"""
81-
8281
# this algorithm only supports single instance training
82+
if kwargs.pop('train_instance_count', 1) != 1:
83+
print('LDA only supports single instance training. Defaulting to 1 {}.'.format(train_instance_type))
84+
8385
super(LDA, self).__init__(role, 1, train_instance_type, **kwargs)
8486
self.num_topics = num_topics
8587
self.alpha0 = alpha0

src/sagemaker/analytics.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,24 @@ def _fetch_dataframe(self):
6464
pass
6565

6666
def clear_cache(self):
67-
"""Clears the object of all local caches of API methods, so
67+
"""Clear the object of all local caches of API methods, so
6868
that the next time any properties are accessed they will be refreshed from
6969
the service.
7070
"""
7171
self._dataframe = None
7272

7373

7474
class HyperparameterTuningJobAnalytics(AnalyticsMetricsBase):
75-
"""Fetches results about this tuning job and makes them accessible for analytics.
75+
"""Fetch results about a hyperparameter tuning job and make them accessible for analytics.
7676
"""
7777

7878
def __init__(self, hyperparameter_tuning_job_name, sagemaker_session=None):
79-
"""Initialize an ``HyperparameterTuningJobAnalytics`` instance.
79+
"""Initialize a ``HyperparameterTuningJobAnalytics`` instance.
8080
8181
Args:
82-
hyperparameter_tuning_job_name (str): name of the HyperparameterTuningJob to
83-
analyze.
82+
hyperparameter_tuning_job_name (str): name of the HyperparameterTuningJob to analyze.
8483
sagemaker_session (sagemaker.session.Session): Session object which manages interactions with
85-
Amazon SageMaker APIs and any other AWS services needed. If not specified, the estimator creates one
84+
Amazon SageMaker APIs and any other AWS services needed. If not specified, one is created
8685
using the default AWS configuration chain.
8786
"""
8887
sagemaker_session = sagemaker_session or Session()
@@ -100,16 +99,16 @@ def __repr__(self):
10099
return "<sagemaker.HyperparameterTuningJobAnalytics for %s>" % self.name
101100

102101
def clear_cache(self):
103-
"""Clears the object of all local caches of API methods.
102+
"""Clear the object of all local caches of API methods.
104103
"""
105104
super(HyperparameterTuningJobAnalytics, self).clear_cache()
106105
self._tuning_job_describe_result = None
107106
self._training_job_summaries = None
108107

109108
def _fetch_dataframe(self):
110-
"""Returns a pandas dataframe with all the training jobs, their
111-
hyperparameters, results, and metadata about the training jobs.
112-
Includes a column to indicate that any job was the best seen so far.
109+
"""Return a pandas dataframe with all the training jobs, along with their
110+
hyperparameters, results, and metadata. This also includes a column to indicate
111+
if a training job was the best seen so far.
113112
"""
114113
def reshape(training_summary):
115114
# Helper method to reshape a single training job summary into a dataframe record
@@ -139,8 +138,8 @@ def reshape(training_summary):
139138

140139
@property
141140
def tuning_ranges(self):
142-
"""A dict describing the ranges of all tuned hyperparameters.
143-
Dict's key is the name of the hyper param. Dict's value is the range.
141+
"""A dictionary describing the ranges of all tuned hyperparameters.
142+
The keys are the names of the hyperparameter, and the values are the ranges.
144143
"""
145144
out = {}
146145
for _, ranges in self.description()['HyperParameterTuningJobConfig']['ParameterRanges'].items():
@@ -149,10 +148,13 @@ def tuning_ranges(self):
149148
return out
150149

151150
def description(self, force_refresh=False):
152-
"""Response to DescribeHyperParameterTuningJob
151+
"""Call ``DescribeHyperParameterTuningJob`` for the hyperparameter tuning job.
153152
154153
Args:
155154
force_refresh (bool): Set to True to fetch the latest data from SageMaker API.
155+
156+
Returns:
157+
dict: The Amazon SageMaker response for ``DescribeHyperParameterTuningJob``.
156158
"""
157159
if force_refresh:
158160
self.clear_cache()
@@ -163,10 +165,13 @@ def description(self, force_refresh=False):
163165
return self._tuning_job_describe_result
164166

165167
def training_job_summaries(self, force_refresh=False):
166-
"""A list of everything (paginated) from ListTrainingJobsForTuningJob
168+
"""A (paginated) list of everything from ``ListTrainingJobsForTuningJob``.
167169
168170
Args:
169171
force_refresh (bool): Set to True to fetch the latest data from SageMaker API.
172+
173+
Returns:
174+
dict: The Amazon SageMaker response for ``ListTrainingJobsForTuningJob``.
170175
"""
171176
if force_refresh:
172177
self.clear_cache()
@@ -191,19 +196,19 @@ def training_job_summaries(self, force_refresh=False):
191196

192197

193198
class TrainingJobAnalytics(AnalyticsMetricsBase):
194-
"""Fetches training curve data from CloudWatch Metrics for a specific training job.
199+
"""Fetch training curve data from CloudWatch Metrics for a specific training job.
195200
"""
196201

197202
CLOUDWATCH_NAMESPACE = '/aws/sagemaker/HyperParameterTuningJobs'
198203

199204
def __init__(self, training_job_name, metric_names, sagemaker_session=None):
200-
"""Initialize an ``TrainingJobAnalytics`` instance.
205+
"""Initialize a ``TrainingJobAnalytics`` instance.
201206
202207
Args:
203208
training_job_name (str): name of the TrainingJob to analyze.
204209
metric_names (list): string names of all the metrics to collect for this training job
205210
sagemaker_session (sagemaker.session.Session): Session object which manages interactions with
206-
Amazon SageMaker APIs and any other AWS services needed. If not specified, the estimator creates one
211+
Amazon SageMaker APIs and any other AWS services needed. If not specified, one is specified
207212
using the default AWS configuration chain.
208213
"""
209214
sagemaker_session = sagemaker_session or Session()
@@ -223,7 +228,7 @@ def __repr__(self):
223228
return "<sagemaker.TrainingJobAnalytics for %s>" % self.name
224229

225230
def clear_cache(self):
226-
"""Clears the object of all local caches of API methods, so
231+
"""Clear the object of all local caches of API methods, so
227232
that the next time any properties are accessed they will be refreshed from
228233
the service.
229234
"""
@@ -232,7 +237,7 @@ def clear_cache(self):
232237
self._time_interval = self._determine_timeinterval()
233238

234239
def _determine_timeinterval(self):
235-
"""Returns a dict with two datetime objects, start_time and end_time
240+
"""Return a dictionary with two datetime objects, start_time and end_time,
236241
covering the interval of the training job
237242
"""
238243
description = self._sage_client.describe_training_job(TrainingJobName=self.name)
@@ -249,7 +254,7 @@ def _fetch_dataframe(self):
249254
return pd.DataFrame(self._data)
250255

251256
def _fetch_metric(self, metric_name):
252-
"""Fetches all the values of a named metric, and adds them to _data
257+
"""Fetch all the values of a named metric, and add them to _data
253258
"""
254259
request = {
255260
'Namespace': self.CLOUDWATCH_NAMESPACE,
@@ -284,7 +289,7 @@ def _fetch_metric(self, metric_name):
284289
self._add_single_metric(elapsed_seconds, metric_name, value)
285290

286291
def _add_single_metric(self, timestamp, metric_name, value):
287-
"""Stores a single metric in the _data dict which can be
292+
"""Store a single metric in the _data dict which can be
288293
converted to a dataframe.
289294
"""
290295
# note that this method is built this way to make it possible to

src/sagemaker/estimator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def delete_endpoint(self):
319319

320320
@property
321321
def training_job_analytics(self):
322-
"""Returns a TrainingJobAnalytics object for the current training job.
322+
"""Return a ``TrainingJobAnalytics`` object for the current training job.
323323
"""
324324
if self._current_job_name is None:
325325
raise ValueError('Estimator is not associated with a TrainingJob')

0 commit comments

Comments
 (0)