1
$\begingroup$

I am making a sequential neural network for classification, with 3 dense layers, which will be trained on a simple synthetic dataset. The description of dataset is as follows:

  • Data and class labels are integers. They are 2000 each.
  • There is only a single feature column (populated by np.arange(2000) * 3)
  • There is only a single label which indicates last digit of number (populated by np.arange(2000) *3 % 10).

After making the model, I am encountering the following error when calling model.fit():

ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 1500 but received input with shape (100, 1)

I have uploaded the commented Jupyter Notebook for this code on Google Collab: https://colab.research.google.com/drive/14v92NTBxIEIFJh2BhybfqhawHYIBvKnm?usp=sharing

Any suggestion about how to fix this error and get reasonable accuracy on training set?

$\endgroup$
2
$\begingroup$

You set the input shape to (1500, 2) whereas your data only contains a single feature. You should therefore change the shape to (1,) or (None, 1) to match the shape of the input data.

$\endgroup$
4
  • $\begingroup$ It works! Thanks for the answer. Can you explain why does (None, 1) also work? $\endgroup$
    – hhj8i
    Mar 28 at 17:11
  • 1
    $\begingroup$ @hhj8i (1,) and (None,1) are identical in this case $\endgroup$
    – Nikos M.
    Mar 28 at 17:32
  • $\begingroup$ None can stand for any number, in this case the number of samples in a single batch. $\endgroup$
    – Oxbowerce
    Mar 28 at 17:38
  • $\begingroup$ I was also wondering that the feature and label columns in training data, have 1500 values each. But an array with shape (1, ) can contain a single value only. So will the training be accurate when using input_shape = (1, )? So far, I am getting 9% accuracy on training data. $\endgroup$
    – hhj8i
    Mar 28 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.