1

Keras throws an error message on this trivial example. Need help. Also, is there a documentation on what the tensor dimensions it expects for Conv2D and Input? Spent too much time trying to find solutions and mutate/rotate the tensor every each way...

My specs: Windows 10 x64, Python 3.6 (from Anaconda 3 x64), Keras 2.09, TensorFlow 1.4.0

import numpy as np
from keras.models import Model
from keras.layers import Conv2D, Input
from keras.utils.np_utils import to_categorical

n_samples, n_row, n_col, n_channels = 1006, 99, 81, 1

tX = np.random.rand(n_samples, n_row, n_col, n_channels)
tY = np.random.randint(0,5,n_samples)
inp = Input(shape=(n_row, n_col, n_channels))
lr = Conv2D(16, kernel_size=2, padding='same')(inp)
M = Model(inputs=inp, outputs=lr)
M.compile(optimizer='Adam',loss='categorical_crossentropy')
M.fit(tX, to_categorical(tY, num_classes=None))

gives the error message:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-11-27bd9e59639d>", line 14, in <module>
    M.fit(tX, to_categorical(tY, num_classes=None))
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\keras\engine\training.py", line 1581, in fit
    batch_size=batch_size)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\keras\engine\training.py", line 1418, in _standardize_user_data
    exception_prefix='target')
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\keras\engine\training.py", line 141, in _standardize_input_data
    str(array.shape))
ValueError: Error when checking target: expected conv2d_3 to have 4 dimensions, but got array with shape (1006, 5)
4
  • You are using categorical cross entropy in your loss function while you are generating only one label with ty. That's why it's throwing this error. Either have a number of categories in your labels and have them one-hot encoded or change your loss function accordingly Commented Dec 26, 2017 at 6:07
  • Nain, thanks for noting. It was my typo in this short example. I corrected it, but the same error persists. Any thoughts? Commented Dec 26, 2017 at 6:37
  • Did you convert the labels to categorical? Commented Dec 26, 2017 at 6:47
  • Yes, I used to_categorical from Keras, but to no avail. Still throws the same error. I've also updated all key packages to the latest (tensorflow, keras, ...) Commented Dec 26, 2017 at 6:57

1 Answer 1

1

Well the problem is that your model makes no sense. The output of your convolution has shape (1006, 99, 81, 16), and since you built a model with this output, it is the output of your model as well. Your labels have shape (1006, 5). Both shapes have to match in order to compute the loss function, but since they don't, then Keras raises an error as the output shape is unexpected.

An easy way to fix it is to add a Dense(5, activation = "softmax") layer after he convolution, and making this the model output, as then the output shape will be (1006, 5). This layer just acts as a classifier over the convolution output.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Matias, thanks for the answer. Would you please post your code? Does it work on your machine? When i applied your suggestion as lr=Dense(5, activation = "softmax")(lr), I still have the same error. I'm beginning to think that it is something wrong with the python environment, not with the code itself.
@OlegMelnikov I don't think you are getting the same error message, it is probably different, so include it here.
Matias, I think you also mean to add Flatten() before Dense() :) It works when I add both transformations the fit. Still checking a few things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.