Search code examples
pythonmachine-learningkeras

ValueError: Found Shape in Keras Does Not Match Input Shape


I am using a tutorial on youtube. I took my own spin on it and although the code is essentially identical, I am getting an error message saying that the found shape does not match input shape

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 621, 17), found shape=(None, 17)

The code leading to this looks like this

import pandas as pd
from sklearn.model_selection import train_test_split
import tensorflow as tf

datasetFull = pd.read_csv('College_Data.csv')
dataset = datasetFull.drop("Name", axis=1)

x = dataset.drop("Private", axis=1)

y = dataset["Private"]
y.replace('Yes', 1)
y.replace('No', 0)

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)

print(x_train.shape)

model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Dense(256, input_shape=x_train.shape, activation='sigmoid'))
model.add(tf.keras.layers.Dense(256, activation='sigmoid'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))

model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

tf.reshape(x_train, [621, 17])

print(x_train.shape)

model.fit(x_train, y_train, epochs=1000)

Not sure if this is relevant, but I also get error messages like this that do not seem to actually stop the process.

This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: SSE SSE2 SSE3 SSE4.1 SSE4.2 AVX AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.

As you can see, I tried to print the shape of the input data before I take it as the input shape and before I submit it to fit

I also tried reshaping the input data right before submitting it to Keras with tf.reshape(x_train, [621, 17])

No matter what I did/do the same error always comes up:

 ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 621, 17), found shape=(None, 17)

Solution

  • A quick fix would be -

    model.add(tf.keras.layers.Dense(256, input_shape=(x_train.shape[1], ), activation='sigmoid'))
    

    The input shape must be an iterable and represent the number of independent variables, or in simple terms (IN YOUR CASE), the number of columns in x_train

    Improvements

    Rather than using -

    y = dataset["Private"]
    y.replace('Yes', 1)
    y.replace('No', 0)
    

    use LabelEncoder, the added advantage would be simple, efficient and easy to convert from strings to numbers and vice versa. Also, I am not sure if you are actually converting the strings into numbers just by using y.replace('Yes', 1). You are not setting the updated values back into y. According to me, it should have been y = y.replace('Yes', 1). Same for replacing "no"

    Input and Output Shapes in MLP

    You can refer this to know more about input and output shapes of multilayer perceptrons.

    Warnings by Tensorflow

    Those other error messages are not relevant but some harmless warnings; you can refer to this answer which explains it beautifully.