You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/05/31 18:41:18 UTC

[GitHub] indhub commented on a change in pull request #11002: [MXNET-433] Tutorial on saving and loading gluon models

indhub commented on a change in pull request #11002: [MXNET-433] Tutorial on saving and loading gluon models
URL: https://github.com/apache/incubator-mxnet/pull/11002#discussion_r192198413
 
 

 ##########
 File path: docs/tutorials/gluon/save_load_params.md
 ##########
 @@ -0,0 +1,257 @@
+# Saving and Loading Gluon Models
+
+Training large models take a lot of time and it is a good idea to save the trained models to files to avoid training them again and again. There is a number of reasons to do this. For example, you might want to do inference on a machine that is different from the one where the model was trained. Sometimes model's performance on validation set decreases towards the end of the training because of overfitting. If you saved your model parameters after every epoch, at the end you can decide to use the model that performs best on the validation set.
+
+In this tutorials we will learn ways to save and load Gluon models. Let's start by importing the modules we'll need.
+
+```python
+import mxnet as mx
+import mxnet.ndarray as nd
+from mxnet import nd, autograd, gluon
+
+import numpy as np
+```
+
+## Build and train a simple model
+
+We need a trained model before we can save it to a file. So let's go ahead and build a very simple convolutional network and train it on MNIST data.
+
+Let's define a helper function to build a LeNet model and another helper to train LeNet with MNIST.
+
+```python
+# Use GPU if one exists, else use CPU
+ctx = mx.gpu() if mx.test_utils.list_gpus() else mx.cpu()
+
+# MNIST images are 28x28. Total pixels in input layer is 28x28 = 784
+num_inputs = 784
+# Clasify the images into one of the 10 digits
+num_outputs = 10
+# 64 images in a batch
+batch_size = 64
+
+# Helper to preprocess data for training
+def transform(data, label):
+    return nd.transpose(data.astype(np.float32), (2,0,1))/255, label.astype(np.float32)
+
+# Load the training data
+train_data = gluon.data.DataLoader(gluon.data.vision.MNIST(train=True, transform=transform), 
+                                   batch_size, shuffle=True)
+
+# Build a simple convolutional network
+def build_lenet(net):    
 
 Review comment:
   So that, I can use this function to build either Block or HybridBlock. I'm building the network as a Block to demonstrate saving and loading parameters. I'm then building the network as HybridBlock to demonstrate saving and loading parameters and model architecture.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services