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/01/12 19:31:26 UTC
[GitHub] piiswrong closed pull request #9174: Adding test for forward backward compatibility
piiswrong closed pull request #9174: Adding test for forward backward compatibility
URL: https://github.com/apache/incubator-mxnet/pull/9174
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/load_inference.py b/load_inference.py
new file mode 100644
index 0000000000..fc58595f12
--- /dev/null
+++ b/load_inference.py
@@ -0,0 +1,33 @@
+import mxnet as mx
+mnist = mx.test_utils.get_mnist()
+batch_size = 100
+train_iter = mx.io.NDArrayIter(mnist['train_data'], mnist['train_label'], batch_size, shuffle=True)
+val_iter = mx.io.NDArrayIter(mnist['test_data'], mnist['test_label'], batch_size)
+data = mx.sym.var('data')
+# Flatten the data from 4-D shape into 2-D (batch_size, num_channel*width*height)
+data = mx.sym.flatten(data=data)
+# The first fully-connected layer and the corresponding activation function
+fc1 = mx.sym.FullyConnected(data=data, num_hidden=128)
+act1 = mx.sym.Activation(data=fc1, act_type="relu")
+
+# The second fully-connected layer and the corresponding activation function
+fc2 = mx.sym.FullyConnected(data=act1, num_hidden = 64)
+act2 = mx.sym.Activation(data=fc2, act_type="relu")
+# MNIST has 10 classes
+fc3 = mx.sym.FullyConnected(data=act2, num_hidden=10)
+# Softmax with cross entropy loss
+mlp = mx.sym.SoftmaxOutput(data=fc3, name='softmax')
+import logging
+logging.getLogger().setLevel(logging.DEBUG) # logging to stdout
+# create a trainable module on CPU
+mlp_model = mx.mod.Module(symbol=mlp, context=mx.cpu())
+mlp_model.bind(data_shapes=train_iter.provide_data, label_shapes=train_iter.provide_label)
+mlp_model.load_params('mymodel')
+test_iter = mx.io.NDArrayIter(mnist['test_data'], None, batch_size)
+prob = mlp_model.predict(test_iter)
+assert prob.shape == (10000, 10)
+test_iter = mx.io.NDArrayIter(mnist['test_data'], mnist['test_label'], batch_size)
+# predict accuracy of mlp
+acc = mx.metric.Accuracy()
+mlp_model.score(test_iter, acc)
+print(acc)
diff --git a/train_models.py b/train_models.py
new file mode 100644
index 0000000000..1fa8b58afa
--- /dev/null
+++ b/train_models.py
@@ -0,0 +1,36 @@
+# Example taken from https://mxnet.incubator.apache.org/tutorials/python/mnist.html
+import mxnet as mx
+mnist = mx.test_utils.get_mnist()
+batch_size = 100
+train_iter = mx.io.NDArrayIter(mnist['train_data'], mnist['train_label'], batch_size, shuffle=True)
+val_iter = mx.io.NDArrayIter(mnist['test_data'], mnist['test_label'], batch_size)
+data = mx.sym.var('data')
+# Flatten the data from 4-D shape into 2-D (batch_size, num_channel*width*height)
+data = mx.sym.flatten(data=data)
+# The first fully-connected layer and the corresponding activation function
+fc1 = mx.sym.FullyConnected(data=data, num_hidden=128)
+act1 = mx.sym.Activation(data=fc1, act_type="relu")
+
+# The second fully-connected layer and the corresponding activation function
+fc2 = mx.sym.FullyConnected(data=act1, num_hidden = 64)
+act2 = mx.sym.Activation(data=fc2, act_type="relu")
+# MNIST has 10 classes
+fc3 = mx.sym.FullyConnected(data=act2, num_hidden=10)
+# Softmax with cross entropy loss
+mlp = mx.sym.SoftmaxOutput(data=fc3, name='softmax')
+import logging
+logging.getLogger().setLevel(logging.DEBUG) # logging to stdout
+# create a trainable module on CPU
+mlp_model = mx.mod.Module(symbol=mlp, context=mx.cpu())
+mlp_model.fit(train_iter, # train data
+ eval_data=val_iter, # validation data
+ optimizer='sgd', # use SGD to train
+ optimizer_params={'learning_rate':0.1}, # use fixed learning rate
+ eval_metric='acc', # report accuracy during training
+ batch_end_callback = mx.callback.Speedometer(batch_size, 100), # output progress for each 100 data batches
+ num_epoch=1) # train for at most 1 dataset passes modified
+
+
+# save a model to mymodel-symbol.json and mymodel-0100.params
+filename='mymodel'
+mlp_model.save_params(filename)
----------------------------------------------------------------
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