I'm running the mnist example of mxnet in Spyder IDE. The training does not progress, just as if the learning rate were 0
(see output below).
If I run the same file on the console using either python
or ipython
, it works as expected (seeing improvements in the second epoch).
If I run the file using the normal Python console of Spyder, it also works.
But if I run the file using the ipython
console, I get the output shown below.
I'm new to Python. Any ideas?
Source code
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#%%
import mxnet as mx
#%% load mnist
mnist = mx.test_utils.get_mnist()
#%% define data iterators
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)
#%% create input variable
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)
#%% Multilayer Perceptron with softmax
# 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')
#%% Training
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=10) # train for at most 10 dataset passes
Output of ipython
console in Spyder
runfile('/home/thomas/workspace/clover-python/mnist.py', wdir='/home/thomas/workspace/clover-python')
INFO:root:train-labels-idx1-ubyte.gz exists, skipping download
INFO:root:train-images-idx3-ubyte.gz exists, skipping download
INFO:root:t10k-labels-idx1-ubyte.gz exists, skipping download
INFO:root:t10k-images-idx3-ubyte.gz exists, skipping download
INFO:root:Epoch[0] Batch [100] Speed: 1304.05 samples/sec accuracy=0.097921
INFO:root:Epoch[0] Batch [200] Speed: 1059.19 samples/sec accuracy=0.098200
INFO:root:Epoch[0] Batch [300] Speed: 1178.64 samples/sec accuracy=0.099600
INFO:root:Epoch[0] Batch [400] Speed: 1292.71 samples/sec accuracy=0.098900
INFO:root:Epoch[0] Batch [500] Speed: 1394.21 samples/sec accuracy=0.096500
INFO:root:Epoch[0] Train-accuracy=0.101212
INFO:root:Epoch[0] Time cost=47.798
INFO:root:Epoch[0] Validation-accuracy=0.098000
INFO:root:Epoch[1] Batch [100] Speed: 1247.47 samples/sec accuracy=0.097921
INFO:root:Epoch[1] Batch [200] Speed: 1673.79 samples/sec accuracy=0.098200
INFO:root:Epoch[1] Batch [300] Speed: 1283.91 samples/sec accuracy=0.099600
INFO:root:Epoch[1] Batch [400] Speed: 1247.79 samples/sec accuracy=0.098900
INFO:root:Epoch[1] Batch [500] Speed: 1371.93 samples/sec accuracy=0.096500
INFO:root:Epoch[1] Train-accuracy=0.101212
INFO:root:Epoch[1] Time cost=44.201
INFO:root:Epoch[1] Validation-accuracy=0.098000
INFO:root:Epoch[2] Batch [100] Speed: 1387.72 samples/sec accuracy=0.097921
INFO:root:Epoch[2] Batch [200] Speed: 1196.37 samples/sec accuracy=0.098200
INFO:root:Epoch[2] Batch [300] Speed: 1220.44 samples/sec accuracy=0.099600
INFO:root:Epoch[2] Batch [400] Speed: 1387.75 samples/sec accuracy=0.098900
INFO:root:Epoch[2] Batch [500] Speed: 1279.58 samples/sec accuracy=0.096500
INFO:root:Epoch[2] Train-accuracy=0.101212
INFO:root:Epoch[2] Time cost=46.929
INFO:root:Epoch[2] Validation-accuracy=0.098000
INFO:root:Epoch[3] Batch [100] Speed: 1266.24 samples/sec accuracy=0.097921
and so on…