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

**URL:** <https://forum.freecodecamp.org/t/trying-to-run-a-simple-code-working-with-mnist-datasets-using-keras-with-theano-backend-in-python-no-idea-what-the-problem-is/135806>\
**Category:** General\
**Created:** [July 13, 2017, 3:09am UTC](https://forum.freecodecamp.org/t/trying-to-run-a-simple-code-working-with-mnist-datasets-using-keras-with-theano-backend-in-python-no-idea-what-the-problem-is/135806 "2017-07-13T03:09:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![16BF05040](https://avatars.discourse-cdn.com/v4/letter/1/ce7236/32.png) [@16BF05040](https://forum.freecodecamp.org/u/16BF05040)\
**Post date:** [July 13, 2017, 3:09am UTC](https://forum.freecodecamp.org/t/trying-to-run-a-simple-code-working-with-mnist-datasets-using-keras-with-theano-backend-in-python-no-idea-what-the-problem-is/135806/1 "2017-07-13T03:09:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![luojxxx](https://avatars.discourse-cdn.com/v4/letter/l/dbc845/32.png) [@luojxxx](https://forum.freecodecamp.org/u/luojxxx)\
**Post date:** [July 28, 2017, 11:13pm UTC](https://forum.freecodecamp.org/t/trying-to-run-a-simple-code-working-with-mnist-datasets-using-keras-with-theano-backend-in-python-no-idea-what-the-problem-is/135806/2 "2017-07-28T23:13:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lenpetlyak](https://avatars.discourse-cdn.com/v4/letter/l/41988e/32.png) [@lenpetlyak](https://forum.freecodecamp.org/u/lenpetlyak)\
**Post date:** [October 18, 2017, 1:22pm UTC](https://forum.freecodecamp.org/t/trying-to-run-a-simple-code-working-with-mnist-datasets-using-keras-with-theano-backend-in-python-no-idea-what-the-problem-is/135806/3 "2017-10-18T13:22:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![deepaksen1996](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@deepaksen1996](https://forum.freecodecamp.org/u/deepaksen1996)\
**Post date:** [April 17, 2018, 10:15pm UTC](https://forum.freecodecamp.org/t/trying-to-run-a-simple-code-working-with-mnist-datasets-using-keras-with-theano-backend-in-python-no-idea-what-the-problem-is/135806/4 "2018-04-17T22:15:36Z")

</div>

it worked. thanks can you tell me the reason

---

<div class="post-metadata">

**Author:** ![ParvaShah](https://avatars.discourse-cdn.com/v4/letter/p/9d8465/32.png) [@ParvaShah](https://forum.freecodecamp.org/u/ParvaShah)\
**Post date:** [June 15, 2018, 2:30pm UTC](https://forum.freecodecamp.org/t/trying-to-run-a-simple-code-working-with-mnist-datasets-using-keras-with-theano-backend-in-python-no-idea-what-the-problem-is/135806/5 "2018-06-15T14:30:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 5:54pm UTC](https://forum.freecodecamp.org/t/trying-to-run-a-simple-code-working-with-mnist-datasets-using-keras-with-theano-backend-in-python-no-idea-what-the-problem-is/135806/6 "2021-01-16T17:54:02Z")

</div>

Closed on January 16, 2021, 5:54pm UTC


