Skip to content

Commit 84c9637

Browse files
authored
update gluon examples (aws#4)
1 parent 77b6c40 commit 84c9637

File tree

4 files changed

+57
-43
lines changed

4 files changed

+57
-43
lines changed

im-python-sdk/mxnet_gluon_cifar10/cifar10.ipynb

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,16 @@
1616
{
1717
"cell_type": "code",
1818
"execution_count": null,
19-
"metadata": {
20-
"collapsed": true
21-
},
19+
"metadata": {},
2220
"outputs": [],
2321
"source": [
24-
"import credentials # put your credentials in credentials.py \n",
25-
"import logging \n",
22+
"import credentials # put your credentials in credentials.py\n",
2623
"import os\n",
24+
"import boto3\n",
2725
"import im\n",
2826
"from im.mxnet import MXNet\n",
29-
"from im.session import s3_input \n",
3027
"from mxnet import gluon\n",
3128
"\n",
32-
"os.environ['AWS_DEFAULT_REGION']='us-west-2'\n",
33-
"\n",
34-
"# Session will use your default credentials\n",
35-
"# e.g. from environment variables or your ~/.aws/credentials file.\n",
3629
"ims = im.Session()\n",
3730
"\n",
3831
"# Replace with a role that gives IM access to s3 and cloudwatch\n",
@@ -71,9 +64,7 @@
7164
{
7265
"cell_type": "code",
7366
"execution_count": null,
74-
"metadata": {
75-
"collapsed": true
76-
},
67+
"metadata": {},
7768
"outputs": [],
7869
"source": [
7970
"inputs = ims.upload_data(path='data', key_prefix='data/gluon-cifar10')\n",
@@ -94,9 +85,7 @@
9485
{
9586
"cell_type": "code",
9687
"execution_count": null,
97-
"metadata": {
98-
"scrolled": false
99-
},
88+
"metadata": {},
10089
"outputs": [],
10190
"source": [
10291
"!cat 'cifar10.py'"
@@ -126,8 +115,7 @@
126115
" hyperparameters={'batch_size': 128, \n",
127116
" 'epochs': 50, \n",
128117
" 'learning_rate': 0.1, \n",
129-
" 'momentum': 0.9,\n",
130-
" '_ps_verbose': 0})"
118+
" 'momentum': 0.9})"
131119
]
132120
},
133121
{
@@ -160,9 +148,7 @@
160148
{
161149
"cell_type": "code",
162150
"execution_count": null,
163-
"metadata": {
164-
"collapsed": true
165-
},
151+
"metadata": {},
166152
"outputs": [],
167153
"source": [
168154
"predictor = m.deploy(min_instances=1, max_instances=1, instance_type='c4.xlarge')"
@@ -225,14 +211,13 @@
225211
"cell_type": "code",
226212
"execution_count": null,
227213
"metadata": {
228-
"collapsed": true,
229214
"scrolled": true
230215
},
231216
"outputs": [],
232217
"source": [
233218
"for i, img in enumerate(image_data):\n",
234219
" response = predictor.predict(img)\n",
235-
" print('image {}: class: {}'.format(i, int(response)))\n"
220+
" print('image {}: class: {}'.format(i, int(response)))"
236221
]
237222
},
238223
{
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
22

3-
os.environ['AWS_ACCESS_KEY_ID'] = 'type your aws access key id here'
4-
os.environ['AWS_SECRET_ACCESS_KEY'] = 'type your aws secret access key here'
3+
# uncomment these lines and add your credentials
4+
# os.environ['AWS_ACCESS_KEY_ID'] = 'type your aws access key id here'
5+
# os.environ['AWS_SECRET_ACCESS_KEY'] = 'type your aws secret access key here'

im-python-sdk/mxnet_gluon_mnist/mnist.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from mxnet.gluon import nn
77
import numpy as np
88
import json
9+
import time
910

1011

1112
logging.basicConfig(level=logging.DEBUG)
@@ -48,6 +49,7 @@ def train(channel_input_dirs, hyperparameters, **kwargs):
4849
for epoch in range(epochs):
4950
# reset data iterator and metric at begining of epoch.
5051
metric.reset()
52+
btic = time.time()
5153
for i, (data, label) in enumerate(train_data):
5254
# Copy data to ctx if necessary
5355
data = data.as_in_context(ctx)
@@ -65,7 +67,10 @@ def train(channel_input_dirs, hyperparameters, **kwargs):
6567

6668
if i % log_interval == 0 and i > 0:
6769
name, acc = metric.get()
68-
print('[Epoch %d Batch %d] Training: %s=%f' % (epoch, i, name, acc))
70+
print('[Epoch %d Batch %d] Training: %s=%f, %f samples/s' %
71+
(epoch, i, name, acc, batch_size / (time.time() - btic)))
72+
73+
btic = time.time()
6974

7075
name, acc = metric.get()
7176
print('[Epoch %d] Training: %s=%f' % (epoch, name, acc))

im-python-sdk/mxnet_gluon_mnist/mnist_with_gluon.ipynb

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818
},
1919
"outputs": [],
2020
"source": [
21-
"import os\n",
22-
"\n",
2321
"import credentials # put your credentials in credentials.py\n",
22+
"import os\n",
23+
"import boto3\n",
2424
"import im\n",
2525
"from im.mxnet import MXNet\n",
26-
"from im.session import s3_input \n",
2726
"from mxnet import gluon\n",
2827
"\n",
29-
"os.environ['AWS_DEFAULT_REGION']='us-west-2'\n",
3028
"\n",
3129
"ims = im.Session()\n",
3230
"\n",
@@ -45,7 +43,9 @@
4543
{
4644
"cell_type": "code",
4745
"execution_count": null,
48-
"metadata": {},
46+
"metadata": {
47+
"collapsed": true
48+
},
4949
"outputs": [],
5050
"source": [
5151
"gluon.data.vision.MNIST('./data/train', train=True)\n",
@@ -64,7 +64,9 @@
6464
{
6565
"cell_type": "code",
6666
"execution_count": null,
67-
"metadata": {},
67+
"metadata": {
68+
"collapsed": true
69+
},
6870
"outputs": [],
6971
"source": [
7072
"inputs = ims.upload_data(path='data', key_prefix='data/mnist')"
@@ -84,7 +86,9 @@
8486
{
8587
"cell_type": "code",
8688
"execution_count": null,
87-
"metadata": {},
89+
"metadata": {
90+
"collapsed": true
91+
},
8892
"outputs": [],
8993
"source": [
9094
"!cat 'mnist.py'"
@@ -96,7 +100,7 @@
96100
"source": [
97101
"## Run the training script on IM\n",
98102
"\n",
99-
"The ```MXNet``` class allows us to run our training function on IM infrastructure. We need to configure it with our training script, an IAM role, the number of training instances, and the training instance type. In this case we will run our training job on a single m4.xlarge instance. "
103+
"The ```MXNet``` class allows us to run our training function on IM infrastructure. We need to configure it with our training script, an IAM role, the number of training instances, and the training instance type. In this case we will run our training job on a single c4.xlarge instance. "
100104
]
101105
},
102106
{
@@ -110,9 +114,10 @@
110114
"m = MXNet(\"mnist.py\", \n",
111115
" role=role, \n",
112116
" train_instance_count=1, \n",
113-
" train_instance_type=\"m4.xlarge\",\n",
117+
" train_instance_type=\"c4.xlarge\",\n",
118+
" enable_cloudwatch_metrics=True,\n",
114119
" hyperparameters={'batch_size': 100, \n",
115-
" 'epochs': 10, \n",
120+
" 'epochs': 20, \n",
116121
" 'learning_rate': 0.1, \n",
117122
" 'momentum': 0.9, \n",
118123
" 'log_interval': 100})"
@@ -129,6 +134,7 @@
129134
"cell_type": "code",
130135
"execution_count": null,
131136
"metadata": {
137+
"collapsed": true,
132138
"scrolled": true
133139
},
134140
"outputs": [],
@@ -148,10 +154,13 @@
148154
{
149155
"cell_type": "code",
150156
"execution_count": null,
151-
"metadata": {},
157+
"metadata": {
158+
"collapsed": true,
159+
"scrolled": true
160+
},
152161
"outputs": [],
153162
"source": [
154-
"predictor = m.deploy(instance_type = 'm4.xlarge', min_instances = 1, max_instances = 1)"
163+
"predictor = m.deploy(instance_type = 'c4.xlarge', min_instances = 1, max_instances = 1)"
155164
]
156165
},
157166
{
@@ -164,7 +173,9 @@
164173
{
165174
"cell_type": "code",
166175
"execution_count": null,
167-
"metadata": {},
176+
"metadata": {
177+
"collapsed": true
178+
},
168179
"outputs": [],
169180
"source": [
170181
"from IPython.display import HTML\n",
@@ -182,12 +193,22 @@
182193
"cell_type": "code",
183194
"execution_count": null,
184195
"metadata": {
196+
"collapsed": true,
185197
"scrolled": true
186198
},
187199
"outputs": [],
188200
"source": [
189201
"response = predictor.predict(data)\n",
190-
"print int(response)\n"
202+
"print int(response)"
203+
]
204+
},
205+
{
206+
"cell_type": "markdown",
207+
"metadata": {},
208+
"source": [
209+
"## Cleanup\n",
210+
"\n",
211+
"After you have finished with this example, remember to delete the prediction endpoint to release the instance(s) associated with it."
191212
]
192213
},
193214
{
@@ -197,14 +218,16 @@
197218
"collapsed": true
198219
},
199220
"outputs": [],
200-
"source": []
221+
"source": [
222+
"m.delete_endpoint()"
223+
]
201224
}
202225
],
203226
"metadata": {
204227
"kernelspec": {
205-
"display_name": "Python 2",
228+
"display_name": "Python [conda env:mxnet_p27]",
206229
"language": "python",
207-
"name": "python2"
230+
"name": "conda-env-mxnet_p27-py"
208231
},
209232
"language_info": {
210233
"codemirror_mode": {

0 commit comments

Comments
 (0)