|
13 | 13 | import gzip
|
14 | 14 | import pickle
|
15 | 15 | import sys
|
| 16 | +import time |
16 | 17 |
|
17 | 18 | import boto3
|
18 | 19 | import os
|
@@ -53,3 +54,45 @@ def test_factorization_machines():
|
53 | 54 | assert len(result) == 10
|
54 | 55 | for record in result:
|
55 | 56 | assert record.label["score"] is not None
|
| 57 | + |
| 58 | + |
| 59 | +def test_async_factorization_machines(): |
| 60 | + |
| 61 | + training_job_name = "" |
| 62 | + endpoint_name = name_from_base('factorization_machines') |
| 63 | + sagemaker_session = sagemaker.Session(boto_session=boto3.Session(region_name=REGION)) |
| 64 | + |
| 65 | + with timeout(minutes=5): |
| 66 | + |
| 67 | + data_path = os.path.join(DATA_DIR, 'one_p_mnist', 'mnist.pkl.gz') |
| 68 | + pickle_args = {} if sys.version_info.major == 2 else {'encoding': 'latin1'} |
| 69 | + |
| 70 | + # Load the data into memory as numpy arrays |
| 71 | + with gzip.open(data_path, 'rb') as f: |
| 72 | + train_set, _, _ = pickle.load(f, **pickle_args) |
| 73 | + |
| 74 | + fm = FactorizationMachines(role='SageMakerRole', train_instance_count=1, |
| 75 | + train_instance_type='ml.c4.xlarge', |
| 76 | + num_factors=10, predictor_type='regressor', |
| 77 | + epochs=2, clip_gradient=1e2, eps=0.001, rescale_grad=1.0 / 100, |
| 78 | + sagemaker_session=sagemaker_session, base_job_name='test-fm') |
| 79 | + |
| 80 | + # training labels must be 'float32' |
| 81 | + fm.fit(fm.record_set(train_set[0][:200], train_set[1][:200].astype('float32')), wait=False) |
| 82 | + training_job_name = fm.latest_training_job.name |
| 83 | + |
| 84 | + print("Detached from training job. Will re-attach in 20 seconds") |
| 85 | + time.sleep(20) |
| 86 | + print("attaching now...") |
| 87 | + |
| 88 | + with timeout_and_delete_endpoint_by_name(endpoint_name, sagemaker_session, minutes=35): |
| 89 | + estimator = FactorizationMachines.attach(training_job_name=training_job_name, |
| 90 | + sagemaker_session=sagemaker_session) |
| 91 | + model = FactorizationMachinesModel(estimator.model_data, role='SageMakerRole', |
| 92 | + sagemaker_session=sagemaker_session) |
| 93 | + predictor = model.deploy(1, 'ml.c4.xlarge', endpoint_name=endpoint_name) |
| 94 | + result = predictor.predict(train_set[0][:10]) |
| 95 | + |
| 96 | + assert len(result) == 10 |
| 97 | + for record in result: |
| 98 | + assert record.label["score"] is not None |
0 commit comments