wepe / MachineLearning

Basic Machine Learning and Deep Learning
5.07k stars 3.16k forks source link

some problems about Dense layer #13

Closed CCV-Edward closed 7 years ago

CCV-Edward commented 7 years ago

When I ran 'MachineLearning-master/DeepLearning Tutorials/keras_usage/cnn.py', it occur these following problems. can you give me some advice? `C:\Users\Edward\Anaconda2\python.exe "D:/project/Github/MachineLearning-master/DeepLearning Tutorials/keras_usage/cnn.py" Using Theano backend.

42000 samples Traceback (most recent call last): File "D:/project/Github/MachineLearning-master/DeepLearning Tutorials/keras_usage/cnn.py", line 79, in model.add(Dense(128, init='normal', input_dim=16_4_4)) File "C:\Users\Edward\Anaconda2\lib\site-packages\keras\models.py", line 308, in add output_tensor = layer(self.outputs[0]) File "C:\Users\Edward\Anaconda2\lib\site-packages\keras\engine\topology.py", line 487, in call self.build(input_shapes[0]) File "C:\Users\Edward\Anaconda2\lib\site-packages\keras\layers\core.py", line 695, in build name='{}_W'.format(self.name)) File "C:\Users\Edward\Anaconda2\lib\site-packages\keras\initializations.py", line 36, in normal return K.random_normal_variable(shape, 0.0, scale, name=name) File "C:\Users\Edward\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

Process finished with exit code 1 `

wepe commented 7 years ago

in line 79

model.add(Dense(128, init='normal', input_dim=1644))

input_dim is required when using as the first layer in a model, not necessary to set this argument here (keras auto-infer the input_dim ).

this may cause the error

CCV-Edward commented 7 years ago

Thanks firstly. Unluckily, there are some new problems such as below: Traceback (most recent call last): File "D:/project/Github/MachineLearning-master/DeepLearning Tutorials/keras_usage/cnn.py", line 79, in <module> model.add(Dense(128, init='normal')) File "C:\Users\Edward\Anaconda2\lib\site-packages\keras\models.py", line 308, in add output_tensor = layer(self.outputs[0]) File "C:\Users\Edward\Anaconda2\lib\site-packages\keras\engine\topology.py", line 487, in __call__ self.build(input_shapes[0]) File "C:\Users\Edward\Anaconda2\lib\site-packages\keras\layers\core.py", line 695, in build name='{}_W'.format(self.name)) File "C:\Users\Edward\Anaconda2\lib\site-packages\keras\initializations.py", line 36, in normal return K.random_normal_variable(shape, 0.0, scale, name=name) File "C:\Users\Edward\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

And I am not clearly about the usage of Dense layer, please give me some advice...

wepe commented 7 years ago

I know what is wrong. You use theano as backend but your K.image_dim_ordering() is tf. To solve this problem, just reshape your data, please refer to https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py#L35 .

And check the file .keras/keras.json , if your use theano, it should be

{
    "image_dim_ordering": "th",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "theano"
}
CCV-Edward commented 7 years ago

Thanks a lot! This problem has been successfully sloved.