16
16
import json
17
17
import os
18
18
import pickle
19
+ import random
20
+ import string
19
21
import sys
20
22
import time
21
23
32
34
from sagemaker .predictor import json_deserializer
33
35
from sagemaker .pytorch import PyTorch
34
36
from sagemaker .tensorflow import TensorFlow
35
- from sagemaker .utils import name_from_base
36
37
from sagemaker .tuner import IntegerParameter , ContinuousParameter , CategoricalParameter , HyperparameterTuner , \
37
38
WarmStartConfig , WarmStartTypes , create_transfer_learning_tuner , create_identical_dataset_and_algorithm_tuner
38
39
from tests .integ import DATA_DIR , PYTHON_VERSION , TUNING_DEFAULT_TIMEOUT_MINUTES
@@ -59,7 +60,7 @@ def kmeans_estimator(sagemaker_session):
59
60
60
61
kmeans = KMeans (role = 'SageMakerRole' , train_instance_count = 1 ,
61
62
train_instance_type = 'ml.c4.xlarge' ,
62
- k = 10 , sagemaker_session = sagemaker_session , base_job_name = 'tk' ,
63
+ k = 10 , sagemaker_session = sagemaker_session ,
63
64
output_path = 's3://{}/' .format (sagemaker_session .default_bucket ()))
64
65
# set kmeans specific hp
65
66
kmeans .init_method = 'random'
@@ -124,21 +125,24 @@ def _tune(kmeans_estimator, kmeans_train_set, tuner=None,
124
125
return tuner
125
126
126
127
128
+ def _job_name (base ):
129
+ return base + '' .join (random .choice (string .ascii_letters + string .digits ) for _ in range (8 ))
130
+
131
+
127
132
@pytest .mark .continuous_testing
128
- def test_tuning_kmeans (sagemaker_session ,
129
- kmeans_train_set ,
130
- kmeans_estimator ,
133
+ def test_tuning_kmeans (sagemaker_session , kmeans_train_set , kmeans_estimator ,
131
134
hyperparameter_ranges ):
132
- _tune_and_deploy (kmeans_estimator , kmeans_train_set , sagemaker_session , hyperparameter_ranges = hyperparameter_ranges )
135
+ _tune_and_deploy (kmeans_estimator , kmeans_train_set , sagemaker_session ,
136
+ hyperparameter_ranges = hyperparameter_ranges , job_name = _job_name ('tune-kmeans' ))
133
137
134
138
135
139
@pytest .mark .continuous_testing
136
140
def test_tuning_kmeans_identical_dataset_algorithm_tuner_raw (sagemaker_session ,
137
141
kmeans_train_set ,
138
142
kmeans_estimator ,
139
143
hyperparameter_ranges ):
140
- parent_tuning_job_name = name_from_base ( " kmeans-identical" , max_length = 32 , short = True )
141
- child_tuning_job_name = name_from_base ( " c-kmeans-identical" , max_length = 32 , short = True )
144
+ parent_tuning_job_name = _job_name ( ' kmeans-identical' )
145
+ child_tuning_job_name = _job_name ( ' c-kmeans-identical' )
142
146
_tune (kmeans_estimator , kmeans_train_set , job_name = parent_tuning_job_name ,
143
147
hyperparameter_ranges = hyperparameter_ranges , max_parallel_jobs = 1 , max_jobs = 1 )
144
148
child_tuner = _tune (kmeans_estimator , kmeans_train_set , job_name = child_tuning_job_name ,
@@ -149,7 +153,7 @@ def test_tuning_kmeans_identical_dataset_algorithm_tuner_raw(sagemaker_session,
149
153
150
154
child_warm_start_config_response = WarmStartConfig .from_job_desc (
151
155
sagemaker_session .sagemaker_client .describe_hyper_parameter_tuning_job (
152
- HyperParameterTuningJobName = child_tuning_job_name )[" WarmStartConfig" ])
156
+ HyperParameterTuningJobName = child_tuning_job_name )[' WarmStartConfig' ])
153
157
154
158
assert child_warm_start_config_response .type == child_tuner .warm_start_config .type
155
159
assert child_warm_start_config_response .parents == child_tuner .warm_start_config .parents
@@ -160,10 +164,10 @@ def test_tuning_kmeans_identical_dataset_algorithm_tuner(sagemaker_session,
160
164
kmeans_estimator ,
161
165
hyperparameter_ranges ):
162
166
"""Tests Identical dataset and algorithm use case with one parent and child job launched with
163
- .identical_dataset_and_algorithm_tuner() """
167
+ .identical_dataset_and_algorithm_tuner()"""
164
168
165
- parent_tuning_job_name = name_from_base ( " km-iden1-parent" , max_length = 32 , short = True )
166
- child_tuning_job_name = name_from_base ( " km-iden1-child" , max_length = 32 , short = True )
169
+ parent_tuning_job_name = _job_name ( ' km-iden1-parent' )
170
+ child_tuning_job_name = _job_name ( ' km-iden1-child' )
167
171
168
172
parent_tuner = _tune (kmeans_estimator , kmeans_train_set , job_name = parent_tuning_job_name ,
169
173
hyperparameter_ranges = hyperparameter_ranges )
@@ -174,7 +178,7 @@ def test_tuning_kmeans_identical_dataset_algorithm_tuner(sagemaker_session,
174
178
175
179
child_warm_start_config_response = WarmStartConfig .from_job_desc (
176
180
sagemaker_session .sagemaker_client .describe_hyper_parameter_tuning_job (
177
- HyperParameterTuningJobName = child_tuning_job_name )[" WarmStartConfig" ])
181
+ HyperParameterTuningJobName = child_tuning_job_name )[' WarmStartConfig' ])
178
182
179
183
assert child_warm_start_config_response .type == child_tuner .warm_start_config .type
180
184
assert child_warm_start_config_response .parents == child_tuner .warm_start_config .parents
@@ -186,10 +190,10 @@ def test_create_tuning_kmeans_identical_dataset_algorithm_tuner(sagemaker_sessio
186
190
kmeans_estimator ,
187
191
hyperparameter_ranges ):
188
192
"""Tests Identical dataset and algorithm use case with one parent and child job launched with
189
- .create_identical_dataset_and_algorithm_tuner() """
193
+ .create_identical_dataset_and_algorithm_tuner()"""
190
194
191
- parent_tuning_job_name = name_from_base ( " km-iden2-parent" , max_length = 32 , short = True )
192
- child_tuning_job_name = name_from_base ( " km-iden2-child" , max_length = 32 , short = True )
195
+ parent_tuning_job_name = _job_name ( ' km-iden2-parent' )
196
+ child_tuning_job_name = _job_name ( ' km-iden2-child' )
193
197
194
198
parent_tuner = _tune (kmeans_estimator , kmeans_train_set , job_name = parent_tuning_job_name ,
195
199
hyperparameter_ranges = hyperparameter_ranges , max_parallel_jobs = 1 , max_jobs = 1 )
@@ -202,7 +206,7 @@ def test_create_tuning_kmeans_identical_dataset_algorithm_tuner(sagemaker_sessio
202
206
203
207
child_warm_start_config_response = WarmStartConfig .from_job_desc (
204
208
sagemaker_session .sagemaker_client .describe_hyper_parameter_tuning_job (
205
- HyperParameterTuningJobName = child_tuning_job_name )[" WarmStartConfig" ])
209
+ HyperParameterTuningJobName = child_tuning_job_name )[' WarmStartConfig' ])
206
210
207
211
assert child_warm_start_config_response .type == child_tuner .warm_start_config .type
208
212
assert child_warm_start_config_response .parents == child_tuner .warm_start_config .parents
@@ -213,10 +217,10 @@ def test_transfer_learning_tuner(sagemaker_session,
213
217
kmeans_estimator ,
214
218
hyperparameter_ranges ):
215
219
"""Tests Transfer learning use case with one parent and child job launched with
216
- .transfer_learning_tuner() """
220
+ .transfer_learning_tuner()"""
217
221
218
- parent_tuning_job_name = name_from_base ( " km-tran1-parent" , max_length = 32 , short = True )
219
- child_tuning_job_name = name_from_base ( " km-tran1-child" , max_length = 32 , short = True )
222
+ parent_tuning_job_name = _job_name ( ' km-tran1-parent' )
223
+ child_tuning_job_name = _job_name ( ' km-tran1-child' )
220
224
221
225
parent_tuner = _tune (kmeans_estimator , kmeans_train_set , job_name = parent_tuning_job_name ,
222
226
hyperparameter_ranges = hyperparameter_ranges , max_jobs = 1 , max_parallel_jobs = 1 )
@@ -227,7 +231,7 @@ def test_transfer_learning_tuner(sagemaker_session,
227
231
228
232
child_warm_start_config_response = WarmStartConfig .from_job_desc (
229
233
sagemaker_session .sagemaker_client .describe_hyper_parameter_tuning_job (
230
- HyperParameterTuningJobName = child_tuning_job_name )[" WarmStartConfig" ])
234
+ HyperParameterTuningJobName = child_tuning_job_name )[' WarmStartConfig' ])
231
235
232
236
assert child_warm_start_config_response .type == child_tuner .warm_start_config .type
233
237
assert child_warm_start_config_response .parents == child_tuner .warm_start_config .parents
@@ -239,10 +243,10 @@ def test_create_transfer_learning_tuner(sagemaker_session,
239
243
kmeans_estimator ,
240
244
hyperparameter_ranges ):
241
245
"""Tests Transfer learning use case with two parents and child job launched with
242
- create_transfer_learning_tuner() """
243
- parent_tuning_job_name_1 = name_from_base ( " km-tran2-parent1" , max_length = 32 , short = True )
244
- parent_tuning_job_name_2 = name_from_base ( " km-tran2-parent2" , max_length = 32 , short = True )
245
- child_tuning_job_name = name_from_base ( " km-tran2-child" , max_length = 32 , short = True )
246
+ create_transfer_learning_tuner()"""
247
+ parent_tuning_job_name_1 = _job_name ( ' km-tran2-parent1' )
248
+ parent_tuning_job_name_2 = _job_name ( ' km-tran2-parent2' )
249
+ child_tuning_job_name = _job_name ( ' km-tran2-child' )
246
250
247
251
parent_tuner_1 = _tune (kmeans_estimator , kmeans_train_set , job_name = parent_tuning_job_name_1 ,
248
252
hyperparameter_ranges = hyperparameter_ranges , max_parallel_jobs = 1 , max_jobs = 1 )
@@ -258,7 +262,7 @@ def test_create_transfer_learning_tuner(sagemaker_session,
258
262
259
263
child_warm_start_config_response = WarmStartConfig .from_job_desc (
260
264
sagemaker_session .sagemaker_client .describe_hyper_parameter_tuning_job (
261
- HyperParameterTuningJobName = child_tuning_job_name )[" WarmStartConfig" ])
265
+ HyperParameterTuningJobName = child_tuning_job_name )[' WarmStartConfig' ])
262
266
263
267
assert child_warm_start_config_response .type == child_tuner .warm_start_config .type
264
268
assert child_warm_start_config_response .parents == child_tuner .warm_start_config .parents
@@ -270,9 +274,9 @@ def test_tuning_kmeans_identical_dataset_algorithm_tuner_from_non_terminal_paren
270
274
kmeans_estimator ,
271
275
hyperparameter_ranges ):
272
276
"""Tests Identical dataset and algorithm use case with one non terminal parent and child job launched with
273
- .identical_dataset_and_algorithm_tuner() """
274
- parent_tuning_job_name = name_from_base ( " km-non-term" , max_length = 32 , short = True )
275
- child_tuning_job_name = name_from_base ( " km-non-term-child" , max_length = 32 , short = True )
277
+ .identical_dataset_and_algorithm_tuner()"""
278
+ parent_tuning_job_name = _job_name ( ' km-non-term' )
279
+ child_tuning_job_name = _job_name ( ' km-non-term-child' )
276
280
277
281
parent_tuner = _tune (kmeans_estimator , kmeans_train_set , job_name = parent_tuning_job_name ,
278
282
hyperparameter_ranges = hyperparameter_ranges , wait_till_terminal = False , max_parallel_jobs = 1 ,
@@ -296,7 +300,7 @@ def test_tuning_lda(sagemaker_session):
296
300
feature_num = int (all_records [0 ].features ['values' ].float32_tensor .shape [0 ])
297
301
298
302
lda = LDA (role = 'SageMakerRole' , train_instance_type = 'ml.c4.xlarge' , num_topics = 10 ,
299
- sagemaker_session = sagemaker_session , base_job_name = 'test-lda' )
303
+ sagemaker_session = sagemaker_session )
300
304
301
305
record_set = prepare_record_set_from_local_files (data_path , lda .data_location ,
302
306
len (all_records ), feature_num , sagemaker_session )
@@ -313,7 +317,7 @@ def test_tuning_lda(sagemaker_session):
313
317
hyperparameter_ranges = hyperparameter_ranges , objective_type = 'Maximize' , max_jobs = 2 ,
314
318
max_parallel_jobs = 2 )
315
319
316
- tuner .fit ([record_set , test_record_set ], mini_batch_size = 1 )
320
+ tuner .fit ([record_set , test_record_set ], mini_batch_size = 1 , job_name = _job_name ( 'tune-lda' ) )
317
321
318
322
print ('Started hyperparameter tuning job with name:' + tuner .latest_tuning_job .name )
319
323
@@ -337,8 +341,7 @@ def test_stop_tuning_job(sagemaker_session):
337
341
train_input = np .random .rand (1000 , feature_num )
338
342
339
343
rcf = RandomCutForest (role = 'SageMakerRole' , train_instance_count = 1 , train_instance_type = 'ml.c4.xlarge' ,
340
- num_trees = 50 , num_samples_per_tree = 20 , sagemaker_session = sagemaker_session ,
341
- base_job_name = 'test-randomcutforest' )
344
+ num_trees = 50 , num_samples_per_tree = 20 , sagemaker_session = sagemaker_session )
342
345
343
346
records = rcf .record_set (train_input )
344
347
records .distribution = 'FullyReplicated'
@@ -354,7 +357,7 @@ def test_stop_tuning_job(sagemaker_session):
354
357
hyperparameter_ranges = hyperparameter_ranges , objective_type = 'Maximize' , max_jobs = 2 ,
355
358
max_parallel_jobs = 2 )
356
359
357
- tuner .fit ([records , test_records ])
360
+ tuner .fit ([records , test_records ], job_name = _job_name ( 'tune-rcf' ) )
358
361
359
362
time .sleep (15 )
360
363
@@ -394,7 +397,7 @@ def test_tuning_mxnet(sagemaker_session):
394
397
key_prefix = 'integ-test-data/mxnet_mnist/train' )
395
398
test_input = estimator .sagemaker_session .upload_data (path = os .path .join (data_path , 'test' ),
396
399
key_prefix = 'integ-test-data/mxnet_mnist/test' )
397
- tuner .fit ({'train' : train_input , 'test' : test_input })
400
+ tuner .fit ({'train' : train_input , 'test' : test_input }, job_name = _job_name ( 'tune-mxnet' ) )
398
401
399
402
print ('Started hyperparameter tuning job with name:' + tuner .latest_tuning_job .name )
400
403
@@ -409,7 +412,7 @@ def test_tuning_mxnet(sagemaker_session):
409
412
410
413
411
414
@pytest .mark .continuous_testing
412
- @pytest .mark .skipif (PYTHON_VERSION != 'py2' , reason = " TensorFlow image supports only python 2." )
415
+ @pytest .mark .skipif (PYTHON_VERSION != 'py2' , reason = ' TensorFlow image supports only Python 2.' )
413
416
def test_tuning_tf (sagemaker_session ):
414
417
with timeout (minutes = TUNING_DEFAULT_TIMEOUT_MINUTES ):
415
418
script_path = os .path .join (DATA_DIR , 'iris' , 'iris-dnn-classifier.py' )
@@ -433,7 +436,7 @@ def test_tuning_tf(sagemaker_session):
433
436
tuner = HyperparameterTuner (estimator , objective_metric_name , hyperparameter_ranges , metric_definitions ,
434
437
objective_type = 'Minimize' , max_jobs = 2 , max_parallel_jobs = 2 )
435
438
436
- tuner .fit (inputs )
439
+ tuner .fit (inputs , job_name = _job_name ( 'tune-tf' ) )
437
440
438
441
print ('Started hyperparameter tuning job with name:' + tuner .latest_tuning_job .name )
439
442
@@ -481,7 +484,7 @@ def test_tuning_chainer(sagemaker_session):
481
484
tuner = HyperparameterTuner (estimator , objective_metric_name , hyperparameter_ranges , metric_definitions ,
482
485
max_jobs = 2 , max_parallel_jobs = 2 )
483
486
484
- tuner .fit ({'train' : train_input , 'test' : test_input })
487
+ tuner .fit ({'train' : train_input , 'test' : test_input }, job_name = 'tune-chainer' )
485
488
486
489
print ('Started hyperparameter tuning job with name:' + tuner .latest_tuning_job .name )
487
490
@@ -525,7 +528,7 @@ def test_attach_tuning_pytorch(sagemaker_session):
525
528
526
529
training_data = estimator .sagemaker_session .upload_data (path = os .path .join (mnist_dir , 'training' ),
527
530
key_prefix = 'integ-test-data/pytorch_mnist/training' )
528
- tuner .fit ({'training' : training_data })
531
+ tuner .fit ({'training' : training_data }, job_name = _job_name ( 'tune-pytorch' ) )
529
532
530
533
tuning_job_name = tuner .latest_tuning_job .name
531
534
@@ -591,7 +594,8 @@ def test_tuning_byo_estimator(sagemaker_session):
591
594
hyperparameter_ranges = hyperparameter_ranges ,
592
595
max_jobs = 2 , max_parallel_jobs = 2 )
593
596
594
- tuner .fit ({'train' : s3_train_data , 'test' : s3_train_data }, include_cls_metadata = False )
597
+ tuner .fit ({'train' : s3_train_data , 'test' : s3_train_data }, include_cls_metadata = False ,
598
+ job_name = _job_name ('tune-byo' ))
595
599
596
600
print ('Started hyperparameter tuning job with name:' + tuner .latest_tuning_job .name )
597
601
0 commit comments