Skip to content

Commit a438e68

Browse files
authored
Add SageMaker analytics example (#66)
1 parent 4bef399 commit a438e68

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

README.rst

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,70 @@ Installation
105105
106106
Examples
107107
--------
108-
See: `sagemaker-experiments <https://github.com/awslabs/amazon-sagemaker-examples/tree/master/sagemaker-experiments>`_ in `AWS Labs Amazon SageMaker Examples <https://github.com/awslabs/amazon-sagemaker-examples>`_.
108+
109+
.. code-block:: python
110+
111+
import boto3
112+
import pickle, gzip, numpy, urllib.request, json
113+
import io
114+
import numpy as np
115+
import sagemaker.amazon.common as smac
116+
import sagemaker
117+
from sagemaker import get_execution_role
118+
from sagemaker import analytics
119+
from smexperiments import experiment
120+
121+
# Specify training container
122+
from sagemaker.amazon.amazon_estimator import get_image_uri
123+
container = get_image_uri(boto3.Session().region_name, 'linear-learner')
124+
125+
# Load the dataset
126+
urllib.request.urlretrieve("http://deeplearning.net/data/mnist/mnist.pkl.gz", "mnist.pkl.gz")
127+
with gzip.open('mnist.pkl.gz', 'rb') as f:
128+
train_set, valid_set, test_set = pickle.load(f, encoding='latin1')
129+
130+
vectors = np.array([t.tolist() for t in train_set[0]]).astype('float32')
131+
labels = np.where(np.array([t.tolist() for t in train_set[1]]) == 0, 1, 0).astype('float32')
132+
133+
buf = io.BytesIO()
134+
smac.write_numpy_to_dense_tensor(buf, vectors, labels)
135+
buf.seek(0)
136+
137+
key = 'recordio-pb-data'
138+
bucket = '{YOUR-BUCKET}'
139+
prefix = 'sagemaker/DEMO-linear-mnist'
140+
boto3.resource('s3').Bucket(bucket).Object(os.path.join(prefix, 'train', key)).upload_fileobj(buf)
141+
s3_train_data = 's3://{}/{}/train/{}'.format(bucket, prefix, key)
142+
output_location = 's3://{}/{}/output'.format(bucket, prefix)
143+
144+
my_experiment = experiment.Experiment.create(experiment_name='MNIST')
145+
my_trial = my_experiment.create_trial(trial_name='linear-learner')
146+
147+
role = get_execution_role()
148+
sess = sagemaker.Session()
149+
150+
linear = sagemaker.estimator.Estimator(container,
151+
role,
152+
train_instance_count=1,
153+
train_instance_type='ml.c4.xlarge',
154+
output_path=output_location,
155+
sagemaker_session=sess)
156+
linear.set_hyperparameters(feature_dim=784,
157+
predictor_type='binary_classifier',
158+
mini_batch_size=200)
159+
160+
linear.fit(inputs={'train': s3_train_data}, experiment_config={
161+
"ExperimentName": my_experiment.experiment_name,
162+
"TrialName": my_trial.trial_name,
163+
"TrialComponentDisplayName": "MNIST-linear-learner",
164+
},)
165+
166+
trial_component_analytics = analytics.ExperimentAnalytics(experiment_name=my_experiment.experiment_name)
167+
168+
analytic_table = trial_component_analytics.dataframe()
169+
analytic_table
170+
171+
For more examples, check out: `sagemaker-experiments <https://github.com/awslabs/amazon-sagemaker-examples/tree/master/sagemaker-experiments>`_ in `AWS Labs Amazon SageMaker Examples <https://github.com/awslabs/amazon-sagemaker-examples>`_.
109172

110173
License
111174
-------

0 commit comments

Comments
 (0)