在python中运行keras时出现ValueError



当我训练CNN时:

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

和Traceback(最近一次调用):

Traceback (most recent call last):
File "F:eclipsedasdaaatest1.py", line 89, in <module>
model.add(Dense(128, init='normal'))  
File "D:Anaconda2libsite-packageskerasmodels.py", line 308, in add
output_tensor = layer(self.outputs[0])
File "D:Anaconda2libsite-packageskerasenginetopology.py", line 487, in __call__
self.build(input_shapes[0])
File "D:Anaconda2libsite-packageskeraslayerscore.py", line 695, in build
name='{}_W'.format(self.name))
File "D:Anaconda2libsite-packageskerasinitializations.py", line 36, in normal
return K.random_normal_variable(shape, 0.0, scale, name=name)
File "D:Anaconda2libsite-packageskerasbackendtheano_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   (numpyrandommtrandmtrand.c:18479)
File "mtrand.pyx", line 234, in mtrand.cont2_array_sc (numpyrandommtrandmtrand.c:3092)
ValueError: negative dimensions are not allowed

你能告诉我问题在哪里吗?

您需要在Convolution2DMaxPooling2D中添加dim_ordering='th',因为您的input_shape(1, 28, 28)。或者,如果你不想添加dim_ordering,那么你可以将输入形状更改为(28, 28, 1)

#!/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()

UPDATE:也可以在~/.keras/keras.json中添加"image_dim_ordering": "th"。但是,我不确定这个文件是在Windows机器上创建的

相关内容

  • 没有找到相关文章

最新更新