0

Full error:

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

The Issue

I have been struggling a lot with setting up a neural network as it constantly complains about the shape received. The x_trian and y_train both have a shape of (20,) but when I input that as the input_shape it says it expected the input shape to have value 20 but instead recieved (None,1).

I do not undersatnd where the (None,1) is coming from because when I print the shape of both the x_train and y_train it gives me (20,). They are both numpy arrays.

The Code

# (the training_data and testing_data are both just numpy arrays with 0 being the data and 1 being the label)
x_train = training_data[:, 0]  # training feature
y_train = training_data[:, 1]  # training label
x_test = testing_data[:, 0]  # testing feature
y_test = testing_data[:, 1]  # testing label

# Create neural network.
from keras.models import Sequential
from keras.layers import Dense

model = Sequential()
model.add(Dense(16, input_dim=20, activation='relu', input_shape=(20,)))
model.add(Dense(12, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(2, activation='softmax'))

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

What I tried

I then changed the input_shape to (None,1) but then it said:

ValueError: Shapes (None, 1) and (None, 1, 2) are incompatible

So, I then changed it to (None, 1, 2) and then it said:

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

Which then sends back me to the original error.

I then found that (20,) is only a dimension of 1 so I changed the input_dim to 1 and got:

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

Conclusion

I am almost certain it has to do with the input_dim, input_shape or the Dense unit (which is 16 on the first model.add (the one the errors complain about) but I am very unsure how to change the values to fit my data.

I know the shape is (20,) and I know the dimension is 1, so it might just have to do with the Dense unit's value (which is 16 on the first model.add which is what the compiler complains about). I read up about what the units measure it but still strugle to understand it.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
DaleJV
  • 370
  • 1
  • 3
  • 15

1 Answers1

0

In x_train, since you have 20 data points and each data point is a single number, the correct shape would be (1,). Keras expects you to input the shape of each datapoint, not the length of the whole dataset itself. For example, if my data looked like

# shape of x_train is (2,3)
# shape of each data point is (3,)    
x_train = np.array([[1,2,1],
                    [1,2,3]])

then my input shape would be (3,).

In your case, you could change the model to look like this:

from keras.layers import Input

model = Sequential()
model.add(Input(shape=(1,)))
model.add(Dense(16, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(2, activation='softmax'))

Also, since you're using loss='categorical_crossentropy', you would have to use one-hot encoding for your y_train, which has to be in the shape (20,2). You can refer here how to convert to one hot encoding: Convert array of indices to 1-hot encoded numpy array

politecat314
  • 141
  • 1
  • 5
  • This is amazing, thank you for all of the work put into it! I just tried to add the model.add(Input(shape=(1,))) and I still get the same error. I followed a few answers on that link but seem to get a errors with most answers. For example, if I do `categorical_labels = np.eye(3)[y_train]` it then complains about that code and says `IndexError: arrays used as indices must be of integer (or boolean) type`. Another answer says `categorical_labels = np.eye(y_train.max()+1)[y_train]` but that gives me a error: `TypeError: cannot perform reduce with flexible type`. – DaleJV Oct 09 '21 at 23:11
  • With `categorical_labels = pd.get_dummies(y_train)` It works, but when trying to fit the model it says `nimplementedError: Cast string to float is not supported` – DaleJV Oct 10 '21 at 02:35