ValueError: ‘The specified size contains a dimension with value <= 0’的解决方法
在keras下搭建神经网络,运行demo报错。
错误文本:
C:\ProgramData\Anaconda2\python.exe "F:/Program Files (x86)/JetBrains/PycharmProjects/untitled1/CNN3.py"
Using Theano backend.
Traceback (most recent call last):
File "F:/Program Files (x86)/JetBrains/PycharmProjects/untitled1/CNN3.py", line 43, in <module>
model = baseline_model()
File "F:/Program Files (x86)/JetBrains/PycharmProjects/untitled1/CNN3.py", line 37, in baseline_model
model.add(Dense(128, activation='relu'))
File "C:\ProgramData\Anaconda2\lib\site-packages\keras\models.py", line 455, in add
output_tensor = layer(self.outputs[0])
File "C:\ProgramData\Anaconda2\lib\site-packages\keras\engine\topology.py", line 528, in __call__
self.build(input_shapes[0])
File "C:\ProgramData\Anaconda2\lib\site-packages\keras\layers\core.py", line 827, in build
constraint=self.kernel_constraint)
File "C:\ProgramData\Anaconda2\lib\site-packages\keras\engine\topology.py", line 364, in add_weight
weight = K.variable(initializer(shape), dtype=K.floatx(), name=name)
File "C:\ProgramData\Anaconda2\lib\site-packages\keras\initializers.py", line 205, in __call__
dtype=dtype, seed=self.seed)
File "C:\ProgramData\Anaconda2\lib\site-packages\keras\backend\theano_backend.py", line 1969, in random_uniform
return rng.uniform(shape, low=minval, high=maxval, dtype=dtype)
File "C:\ProgramData\Anaconda2\lib\site-packages\theano\sandbox\rng_mrg.py", line 1344, in uniform
size)
ValueError: ('The specified size contains a dimension with value <= 0', (-768, 128))
运行截图如下:
解决方式:
修改keras.json文件
修改后:
这是因为后台不同,我选择的是Theano后台:
Tensorflow ordering (tf):
Shapes are expected to be (size_lines,size_columns,channel)
Theano ordering (th):
Shapes are expected to be (channel,size_lines,size_columns)
If you change the ordering line in the keras.json file to “image_dim_ordering”: “th” it should work. (i’d bet that’s what’s in your Elementary OS keras.json).