Trying to run a simple code working with mnist datasets using keras with theano backend in python. No idea what the problem is

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))

Well, I’m not fully sure either, but you can see one of your dimensions is negative. -416, 10

You’re gonna want to make sure your preprocessed training X & Y are the right dimensions.

Otherwise you’ll need to see what’s up with your neural network model. I would recommend going with something simpler like just 1-2 dense layers to see if that clears up the problem, that way you isolate what part of the model is causing this error that way you can understand that specific part better and hopefully understand how to properly implement it.

replace to this ‘model.add(Convolution2D(32, (3, 3), activation=‘relu’, input_shape=(28,28,1)))’

1 Like

it worked. thanks can you tell me the reason

Let’s say you’re working with 128x128 pixel RGB images (that’s 128x128 pixels with 3 color channels).

When you put such an image into a numpy array you can either store it with a shape of (128, 128, 3) or with a shape of (3, 128, 128).

The dimension ordering specifies if the color channel comes first (as with theano / “th”) or if it comes last (as with tensorflow / “tf”).

I guess you are using TensorFlow backend!!