Tonny learns the MNIST - Day 2

Published: by

Install theano

At the end of day 1, I face the problem with Theano not installed. With several search trials from Google, I found the conda command which is a tool from Anaconda. Quick command help me understand that I need to install Theano

$conda list | grep theano
$

Ok let try to install it

$ conda install theano
...

$conda list | grep theano
theano                    0.9.0                    py27_0
$

So far so good, no try

$ python
Python 2.7.13 |Anaconda custom (x86_64)| (default, Dec 20 2016, 23:05:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import numpy
>>> print numpy.__version__
1.13.3
>>> import theano
>>> print theano.__version__
0.9.0.dev-c697eeab84e5b8a74908da654b66ec9eca4f1291
>>>
$

Hmm, ok

Install Keras

They said that install Keras is simple as eating a pie, let's see how that cake tastes like.

$pip install keras
...

$ python -c "import keras; print keras.__version__"
Using TensorFlow backend.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/.pyenv/versions/anaconda-4.0.0/lib/python2.7/site-packages/keras/__init__.py", line 3, in <module>
    from . import utils
  File "/home/.pyenv/versions/anaconda-4.0.0/lib/python2.7/site-packages/keras/utils/__init__.py", line 6, in <module>
    from . import conv_utils
  File "/home/.pyenv/versions/anaconda-4.0.0/lib/python2.7/site-packages/keras/utils/conv_utils.py", line 3, in <module>
    from .. import backend as K
  File "/home/.pyenv/versions/anaconda-4.0.0/lib/python2.7/site-packages/keras/backend/__init__.py", line 83, in <module>
    from .tensorflow_backend import *
  File "/home/.pyenv/versions/anaconda-4.0.0/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 1, in <module>
    import tensorflow as tf
ImportError: No module named tensorflow

Whoops, Using TensorFlow? What happened?

Ok, look like we need to specify KERAS_BACKEND to theano before running it

$KERAS_BACKEND=theano python -c "import keras; print keras.__version__"
Using Theano backend.
2.1.2

Good, I don't want to set the environment variable everytime so quick insert it to .profile to forget it

echo "export KERAS_BACKEND=theano" >> ~/.profile
$. ~/.profile
$ python -c "import keras; print keras.__version__"
Using Theano backend.
2.1.2
$

Try to run the example

Now create the file named mnist.py from tutorial:

# 1. Import libraries and modules
import numpy as np
np.random.seed(123)  # for reproducibility

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.datasets import mnist

# 2. Load pre-shuffled MNIST data into train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# 3. Preprocess input data
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28)
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255

# 4. Preprocess class labels
Y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)

# 5. Define model architecture
model = Sequential()

model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(1,28,28)))
model.add(Convolution2D(32, 3, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

# 6. Compile model
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

# 7. Fit model on training data
model.fit(X_train, Y_train,
          batch_size=32, nb_epoch=10, verbose=1)

# 8. Evaluate model on test data
score = model.evaluate(X_test, Y_test, verbose=0)

And run with

$ python mnist.py
Using Theano backend.
Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz
11493376/11490434 [==============================] - 29s 3us/step
11501568/11490434 [==============================] - 29s 3us/step
mnist.py:29: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(32, (3, 3), activation="relu", input_shape=(1, 28, 28...)`
  model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(1,28,28)))
mnist.py:30: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(32, (3, 3), activation="relu")`
  model.add(Convolution2D(32, 3, 3, activation='relu'))
Traceback (most recent call last):
  File "mnist.py", line 35, in <module>
    model.add(Dense(128, activation='relu'))
  File "/home/.pyenv/versions/anaconda-4.0.0/lib/python2.7/site-packages/keras/models.py", line 489, in add
    output_tensor = layer(self.outputs[0])
  File "/home/.pyenv/versions/anaconda-4.0.0/lib/python2.7/site-packages/keras/engine/topology.py", line 576, in __call__
    self.build(input_shapes[0])
  File "/home/.pyenv/versions/anaconda-4.0.0/lib/python2.7/site-packages/keras/layers/core.py", line 830, in build
    constraint=self.kernel_constraint)
  File "/home/.pyenv/versions/anaconda-4.0.0/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
    return func(*args, **kwargs)
  File "/home/.pyenv/versions/anaconda-4.0.0/lib/python2.7/site-packages/keras/engine/topology.py", line 397, in add_weight
    weight = K.variable(initializer(shape),
  File "/home/.pyenv/versions/anaconda-4.0.0/lib/python2.7/site-packages/keras/initializers.py", line 212, in __call__
    dtype=dtype, seed=self.seed)
  File "/home/.pyenv/versions/anaconda-4.0.0/lib/python2.7/site-packages/keras/backend/theano_backend.py", line 2249, in random_uniform
    return rng.uniform(shape, low=minval, high=maxval, dtype=dtype)
  File "/home/.pyenv/versions/anaconda-4.0.0/lib/python2.7/site-packages/theano/sandbox/rng_mrg.py", line 1344, in uniform
    size)
ValueError: ('The specified size contains a dimension with value <= 0', (-768, 128))

Uh-oh, let solve this problem next time.