ValueError when running keras in python
<p>When I train the CNN:</p>

<pre><code>model = Sequential()  
model.add(Convolution2D(4, 5, 5, border_mode='valid', input_shape=(1,28,28)))  
model.add(Activation('tanh'))  
model.add(Convolution2D(8, 3, 3, border_mode='valid'))  
model.add(Activation('tanh'))  
model.add(MaxPooling2D(pool_size=(2, 2)))  
model.add(Convolution2D(16,  3, 3, border_mode='valid'))  
model.add(Activation('tanh'))  
model.add(MaxPooling2D(pool_size=(2, 2)))  
model.add(Flatten())  
model.add(Dense(128, init='normal'))  
model.add(Activation('tanh'))  
model.add(Dense(10, init='normal'))  
model.add(Activation('softmax'))  
sgd = SGD(l2=0.0,lr=0.05, decay=1e-6, momentum=0.9, nesterov=True)  
model.compile(loss='categorical_crossentropy', optimizer=sgd,class_mode="categorical")   
</code></pre>

<p>and the Traceback (most recent call last):</p>

<pre><code>Traceback (most recent call last):
File "F:\eclipse\dasd\aaa\test1.py", line 89, in &lt;module&gt;
model.add(Dense(128, init='normal'))  
File "D:\Anaconda2\lib\site-packages\keras\models.py", line 308, in add
output_tensor = layer(self.outputs[0])
File "D:\Anaconda2\lib\site-packages\keras\engine\topology.py", line 487, in __call__
self.build(input_shapes[0])
File "D:\Anaconda2\lib\site-packages\keras\layers\core.py", line 695, in build
name='{}_W'.format(self.name))
File "D:\Anaconda2\lib\site-packages\keras\initializations.py", line 36, in normal
return K.random_normal_variable(shape, 0.0, scale, name=name)
File "D:\Anaconda2\lib\site-packages\keras\backend\theano_backend.py", line 145, in random_normal_variable
return variable(np.random.normal(loc=0.0, scale=scale, size=shape),
File "mtrand.pyx", line 1903, in mtrand.RandomState.normal   (numpy\random\mtrand\mtrand.c:18479)
File "mtrand.pyx", line 234, in mtrand.cont2_array_sc (numpy\random\mtrand\mtrand.c:3092)
ValueError: negative dimensions are not allowed
</code></pre>

<p>Could you please tell me where problem is?</p>

<p>You need to add <code>dim_ordering='th'</code> in <code>Convolution2D</code> and <code>MaxPooling2D</code> sine your <code>input_shape</code> is <code>(1, 28, 28)</code>. Or else if you don't want to add an <code>dim_ordering</code> , then you can change input shape to <code>(28, 28, 1)</code>.</p>

<pre><code>#!/usr/bin/env python
# coding=utf-8
from keras.models import Sequential
from keras.layers import Dense, Activation, Convolution2D, MaxPooling2D, Flatten
from keras.optimizers import SGD

model = Sequential()  
model.add(Convolution2D(4, 5, 5, border_mode='valid', input_shape=(1,28,28), dim_ordering='th'))  
model.add(Activation('tanh'))  
model.add(Convolution2D(8, 3, 3, border_mode='valid', dim_ordering='th'))  
model.add(Activation('tanh'))  
model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering='th'))  
model.add(Convolution2D(16,  3, 3, border_mode='valid',dim_ordering='th'))  
model.add(Activation('tanh'))  
model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering='th'))  
model.add(Flatten())  
model.add(Dense(128))  
model.add(Activation('tanh'))  
model.add(Dense(10))  
model.add(Activation('softmax'))  
sgd = SGD(lr=0.05, decay=1e-6, momentum=0.9, nesterov=True)  
model.compile(loss='categorical_crossentropy', optimizer=sgd,class_mode="categorical")
print model.summary()
</code></pre>

<p><strong>UPDATE</strong>: You can also add <code>"image_dim_ordering": "th"</code> in <code>~/.keras/keras.json</code>. But, I'm not sure where this file is created on a Windows machine.</p>

python numpy keras