3. Import libraries and modules
import numpy as np
np.random.seed(123) # for reproducibility
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.datasets import mnist
4. Load pre-shuffled MNIST data into train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
5. 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
6. Preprocess class labels
Y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)
7. Define model architecture
model = Sequential()
#model.add(ZeroPadding2D((1,1),input_shape=(1,28,28)))
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’))
8. Compile model
model.compile(loss=‘categorical_crossentropy’,
optimizer=‘adam’,
metrics=[‘accuracy’])
9. Fit model on training data
model.fit(X_train, Y_train,
batch_size=32, nb_epoch=10, verbose=1)
10. Evaluate model on test data
score = model.evaluate(X_test, Y_test, verbose=0)
print(score[1]*100)
After running I get this:
runfile(‘E:/Spyder Python Scripts/Testing/mnist_try.py’, wdir=‘E:/Spyder Python Scripts/Testing’)
Reloaded modules: lazylinker_ext, lazylinker_ext.lazylinker_ext, tmp00ea_ixx, tmp00ea_ixx.mb5360f1f5a6c119d83d350a65640a94c, cutils_ext, cutils_ext.cutils_ext, tmpd2242kyq, tmpd2242kyq.m55304382275c7a7d18275ba6d4b81a2b, tmpu574uvo_, tmpu574uvo_.md5b67700df7fb7efe3308aca43cd7a84, tmpqevp4vr_, tmpqevp4vr_.m7ffcf2ca1a5f1162a290822b55f0bce5, tmp1dh9sfn2, tmp1dh9sfn2.m513e0f69a10a50009861dc76330f38b9, tmpc62f275m, tmpc62f275m.m339214d6c000dcd0a04d617e25f87e5c, tmpeir28b1g, tmpeir28b1g.ma4367bc2872dbda151dd2083676a0e2d
E:/Spyder Python Scripts/Testing/mnist_try.py:36: 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)))
Traceback (most recent call last):
File “”, line 1, in
runfile(‘E:/Spyder Python Scripts/Testing/mnist_try.py’, wdir=‘E:/Spyder Python Scripts/Testing’)
File “C:\Users\HIT501-05\Anaconda3\envs\keras_env\lib\site-packages\spyder\utils\site\sitecustomize.py”, line 880, in runfile
execfile(filename, namespace)
File “C:\Users\HIT501-05\Anaconda3\envs\keras_env\lib\site-packages\spyder\utils\site\sitecustomize.py”, line 102, in execfile
exec(compile(f.read(), filename, ‘exec’), namespace)
File “E:/Spyder Python Scripts/Testing/mnist_try.py”, line 44, in
model.add(Dense(10, activation=‘softmax’))
File “C:\Users\HIT501-05\Anaconda3\envs\keras_env\lib\site-packages\keras\models.py”, line 476, in add
output_tensor = layer(self.outputs[0])
File “C:\Users\HIT501-05\Anaconda3\envs\keras_env\lib\site-packages\keras\engine\topology.py”, line 569, in call
self.build(input_shapes[0])
File “C:\Users\HIT501-05\Anaconda3\envs\keras_env\lib\site-packages\keras\layers\core.py”, line 830, in build
constraint=self.kernel_constraint)
File “C:\Users\HIT501-05\Anaconda3\envs\keras_env\lib\site-packages\keras\legacy\interfaces.py”, line 88, in wrapper
return func(*args, **kwargs)
File “C:\Users\HIT501-05\Anaconda3\envs\keras_env\lib\site-packages\keras\engine\topology.py”, line 391, in add_weight
weight = K.variable(initializer(shape), dtype=dtype, name=name)
File “C:\Users\HIT501-05\Anaconda3\envs\keras_env\lib\site-packages\keras\initializers.py”, line 208, in call
dtype=dtype, seed=self.seed)
File “C:\Users\HIT501-05\Anaconda3\envs\keras_env\lib\site-packages\keras\backend\theano_backend.py”, line 2123, in random_uniform
return rng.uniform(shape, low=minval, high=maxval, dtype=dtype)
File “C:\Users\HIT501-05\Anaconda3\envs\keras_env\lib\site-packages\theano\sandbox\rng_mrg.py”, line 839, in uniform
size)
ValueError: (‘The specified size contains a dimension with value <= 0’, (-416, 10))