Skip to content

Commit 9005589

Browse files
author
Venkatesan
committed
Mxnet BYOM Verbosity matched with other datasets
1 parent c16d216 commit 9005589

File tree

1 file changed

+110
-9
lines changed

1 file changed

+110
-9
lines changed

sagemaker-python-sdk/mxnet_mnist_byom/mxnet_mnist.ipynb

Lines changed: 110 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,73 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"# Mxnet MNIST BYOM. Train locally and deploy on SageMaker."
7+
"# Mxnet BYOM: Train locally and deploy on SageMaker.\n",
8+
"\n",
9+
"1. [Introduction](#Introduction)\n",
10+
"2. [Prerequisites and Preprocessing](#Prequisites-and-Preprocessing)\n",
11+
" 1. [Permissions and environment variables](#Permissions-and-environment-variables)\n",
12+
" 2. [Data Setup](#Data-setup)\n",
13+
"3. [Training the network locally](#Training)\n",
14+
"4. [Set up hosting for the model](#Set-up-hosting-for-the-model)\n",
15+
" 1. [Export from mxnet](#Export-the-model-from-mxnet)\n",
16+
" 2. [Import model into SageMaker](#Import-model-into-SageMaker)\n",
17+
" 3. [Create endpoint](#Create-endpoint) \n",
18+
"5. [Validate the ebdpoint for use](#Validate-the-endpoint-for-use)"
819
]
920
},
1021
{
1122
"cell_type": "markdown",
1223
"metadata": {},
1324
"source": [
14-
"In this notebook, we will train a model locally on the notebook instance and will deploy and predict from Sagemaker. This can easily be extended to a model trained anywhere else as well. All that is needed is the exported model file and the entry point file containing model definitions. \n",
25+
"## Introduction\n",
26+
"In this notebook, we will train a neural network locally on the location from where this notebook is run using MXNet. We will then see how to create an endpoint from the trained MXNet model and deploy it on SageMaker. We will then inference from the newly created SageMaker endpoint. \n",
27+
"\n",
28+
"The neural network that we will use is a simple fully-connected neural network. The definition of the neural network can be found in the accompanying [mnist.py](mnist.py) file. The ``build_graph`` method contains the model defnition (shown below).\n",
29+
"\n",
30+
"```python\n",
31+
"def build_graph():\n",
32+
" data = mx.sym.var('data')\n",
33+
" data = mx.sym.flatten(data=data)\n",
34+
" fc1 = mx.sym.FullyConnected(data=data, num_hidden=128)\n",
35+
" act1 = mx.sym.Activation(data=fc1, act_type=\"relu\")\n",
36+
" fc2 = mx.sym.FullyConnected(data=act1, num_hidden=64)\n",
37+
" act2 = mx.sym.Activation(data=fc2, act_type=\"relu\")\n",
38+
" fc3 = mx.sym.FullyConnected(data=act2, num_hidden=10)\n",
39+
" return mx.sym.SoftmaxOutput(data=fc3, name='softmax')\n",
40+
"```\n",
41+
"\n",
42+
"From this definitnion we can see that there are two fully-connected layers of 128 and 64 neurons each. The activations of the last fully-connected layer is then fed into a Softmax layer of 10 neurons. We use 10 neurons here because the datatset on which we are going to predict is the MNIST dataset of hand-written digit recognition which has 10 classes. More details can be found about the dataset on the [creator's webpage](http://yann.lecun.com/exdb/mnist/)."
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"## Prequisites and Preprocessing\n",
50+
"\n",
51+
"### Permissions and environment variables\n",
1552
"\n",
16-
"First, let us begin by downloading the mnist data using the mxnet utilities."
53+
"Here we set up the linkage and authentication to AWS services. In this notebook we only need the roles used to give learning and hosting access to your data. The Sagemaker SDK will use S3 defualt buckets when needed. Supply the role in the variable below."
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": 2,
59+
"metadata": {
60+
"collapsed": true
61+
},
62+
"outputs": [],
63+
"source": [
64+
"role = '<your SageMaker execution role here>'"
65+
]
66+
},
67+
{
68+
"cell_type": "markdown",
69+
"metadata": {},
70+
"source": [
71+
"### Data setup\n",
72+
"\n",
73+
"Next, we need to pull the data from the author's site to our local box. Since we have ``mxnet`` utilities, we will use the utilities to download the dataset locally."
1774
]
1875
},
1976
{
@@ -34,7 +91,34 @@
3491
"collapsed": true
3592
},
3693
"source": [
37-
"Train a typical mxnet model for lenet."
94+
"### Training\n",
95+
"\n",
96+
"It is time to train the network. Since we are training the network locally, we can make use of mxnet training tools. The training method is also in the accompanying [mnist.py](mnist.py) file. The method is shown below. \n",
97+
"\n",
98+
"```python \n",
99+
"def train(data, hyperparameters= {'learning_rate': 0.11}, num_cpus=0, num_gpus =1 , **kwargs):\n",
100+
" train_labels = data['train_label']\n",
101+
" train_images = data['train_data']\n",
102+
" test_labels = data['test_label']\n",
103+
" test_images = data['test_data']\n",
104+
" batch_size = 100\n",
105+
" train_iter = mx.io.NDArrayIter(train_images, train_labels, batch_size, shuffle=True)\n",
106+
" val_iter = mx.io.NDArrayIter(test_images, test_labels, batch_size)\n",
107+
" logging.getLogger().setLevel(logging.DEBUG)\n",
108+
" mlp_model = mx.mod.Module(\n",
109+
" symbol=build_graph(),\n",
110+
" context=get_train_context(num_cpus, num_gpus))\n",
111+
" mlp_model.fit(train_iter,\n",
112+
" eval_data=val_iter,\n",
113+
" optimizer='sgd',\n",
114+
" optimizer_params={'learning_rate': float(hyperparameters.get(\"learning_rate\", 0.1))},\n",
115+
" eval_metric='acc',\n",
116+
" batch_end_callback=mx.callback.Speedometer(batch_size, 100),\n",
117+
" num_epoch=10)\n",
118+
" return mlp_model\n",
119+
"```\n",
120+
"\n",
121+
"The method above collects the ``data`` variable that ``get_mnist`` method gives you (which is a dictionary of data arrays) along with a dictionary of ``hyperparameters`` which only contains learning rate, and other parameters. It creates a [``mxnet.mod.Module``](https://mxnet.incubator.apache.org/api/python/module.html) from the network graph we built in the ``build_graph`` method and trains the network using the ``mxnet.mod.Module.fit`` method. "
38122
]
39123
},
40124
{
@@ -53,7 +137,11 @@
53137
"cell_type": "markdown",
54138
"metadata": {},
55139
"source": [
56-
"Export the model and save it down. Analogous to the tensorflow example, some structure needs to be followed, which is explained in the following code."
140+
"## Set up hosting for the model\n",
141+
"\n",
142+
"### Export the model from mxnet\n",
143+
"\n",
144+
"In order to set up hosting, we have to import the model from training to hosting. We will begin by exporting the model from mxnet and saving it down. Analogous to the [tensorflow example](../tensorflow_iris_byom/tensorflow_BYOM_iris.ipynb), some structure needs to be followed. The exported model has to be converted into a form that is readable by ``sagemaker.mxnet.model.MXNetModel``. The following code describes exporting the model in a form that does the same:"
57145
]
58146
},
59147
{
@@ -76,7 +164,9 @@
76164
"cell_type": "markdown",
77165
"metadata": {},
78166
"source": [
79-
"Open a sagemaker session and upload the model on to the default S3 bucket."
167+
"### Import model into SageMaker\n",
168+
"\n",
169+
"Open a new sagemaker session and upload the model on to the default S3 bucket. We can use the ``sagemaker.Session.upload_data`` method to do this. We need the location of where we exported the model from MXNet and where in our default bucket we want to store the model(``/model``). The default S3 bucket can be found using the ``sagemaker.Session.default_bucket`` method."
80170
]
81171
},
82172
{
@@ -97,7 +187,7 @@
97187
"cell_type": "markdown",
98188
"metadata": {},
99189
"source": [
100-
"Use the ``sagemaker.mxnet.model.MXNetModel`` to create a new model that can be deployed."
190+
"Use the ``sagemaker.mxnet.model.MXNetModel`` to import the model into SageMaker that can be deployed. We need the location of the S3 bucket where we have the model, the role for authentication and the entry_point where the model defintion is stored (``mnist.py``). The import call is the following:"
101191
]
102192
},
103193
{
@@ -118,7 +208,9 @@
118208
"cell_type": "markdown",
119209
"metadata": {},
120210
"source": [
121-
"Deploy the model"
211+
"### Create endpoint\n",
212+
"\n",
213+
"Now the model is ready to be deployed at a SageMaker endpoint. We can use the ``sagemaker.mxnet.model.MXNetModel.deploy`` method to do this. Unless you have created or prefer other instances, we recommend using 1 ``'ml.c4.xlarge'`` instance for this training. These are supplied as arguments. "
122214
]
123215
},
124216
{
@@ -137,7 +229,9 @@
137229
"cell_type": "markdown",
138230
"metadata": {},
139231
"source": [
140-
"We can now use this predictor to classify hand-written digits."
232+
"### Validate the endpoint for use\n",
233+
"\n",
234+
"We can now use this endpoint to classify hand-written digits."
141235
]
142236
},
143237
{
@@ -187,6 +281,13 @@
187281
"sagemaker.Session().delete_endpoint(predictor.endpoint)"
188282
]
189283
},
284+
{
285+
"cell_type": "markdown",
286+
"metadata": {},
287+
"source": [
288+
"Clear all stored model data so that we don't overwrite them the next time. "
289+
]
290+
},
190291
{
191292
"cell_type": "code",
192293
"execution_count": null,

0 commit comments

Comments
 (0)