Hello Everyone!

I have recently started learning TensorFlow and so am currently a beginner. Currently, I am trying to perform a classification task using a simple neural network. However, I keep on getting a shape mismatch error when trying to fit the model.

My **Input** input is a 1D array of shape `(45325,)`

and my **Output** labels are also of the same shape. And I want to classify the data points into one of the 4 classes, hence the last layer is a `Dense(4)`

.

I tried searching the problem and found that I basically need to reshape my input tensor to (batch_size, tensor shape). Even after reshaping my tensor to (1, 45325), I am still getting an error *ValueError: Shapes (None, 45325) and (1, 4) are incompatible*. I have even tried using the `tf.keras.Input()`

as the starting layer but nothing seems to be working

Can someone kindly guide me on how to reshape my tensors so they are in the right shape

Thanks!

If the input data contains 45325 samples, each sample having just one feature, you should not reshape it. Just define model input shape as (1,) and pass the input as a numpy array to model.fit()

Inside fit() you can define any batch size that you like or leave the default settings.

The shape mismatch error refers to the fact that your model has 4 output values from the last dense layer but labels represent a 1D array. To avoid this error you can either apply one-hot encoding to the labels array so that it’s shape would be (45325, 4) or pass the labels as they are but in model.compile() specify SparceCategoricalCrossentropy loss.

In shape mismatch error description the first value in each tuple refers to the batch size and the remaining values describe the actual tensor shape - what the model was expecting and what it got.

1 Like

Thanks a lot for the help @Ekaterina_Dranitsyna I finally got the code working. But unfortunately, the results are very poor as I’m only getting a 45% accuracy. Can a 1D CNN model be used in this case? If so, how am I supposed to pass my **Input** tensor to the first Conv1D layer since the required ndim=3. Thanks !

You can find tutorials on various types of problems here Tutorials | TensorFlow Core

Also, at keras.io you can find examples of model architectures and how they are applied to tabular, text and image data.

Usually input data for classification problems has more than one feature. It’s hard to imagine what your data is. Probably, for learning purposes you can use some of tensorflow datasets.

Thanks a lot for the help @Ekaterina_Dranitsyna. I’ll make sure to check out these tutorials