5

Here is the little project of Cancer detection, and it has already has the dataset and colab code, but I get an error when I execute

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

The error is:

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

When I look at the comments, other people are having this issue

DeadChex
  • 4,379
  • 1
  • 27
  • 34
Weber Wang
  • 51
  • 1
  • 1
  • 5
  • Hi and welcome to StackOverflow! Please do not post links to external videos/tutorials. Instead, just include a sample of data (for example `df.head().to_dict()`) and the relevant code in your question so we can copy-paste to reproduce your error. – not_speshal Nov 22 '21 at 15:57

3 Answers3

5

The Tensorflow model expects the first dimension of the input to be the batch size, in the model declaration however they set the input shape to be the same shape as the input. To fix this you can change the input shape of the model to be the number of feature in the dataset.

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

The number of rows in the .csv files will be the number of samples in your dataset. Since you're not using batches, the model will evaluate the whole dataset at once every epoch

Tobiaaa
  • 96
  • 8
  • Mine seemed to work using `model = Sequential() model.add(Dense(512, input_shape=(X.shape[1],), activation='relu', input_dim=3)) model.add(Dense(512, activation='relu')) model.add(Dense(1)) model.compile(optimizer='adam', loss='mae', metrics=['mae'])` I only had to insert one instance of `input_shape=(X.shape[1])`. Is that the correct way? Why is that extra comma after the subscript`(X_train.shape[1],)` needed? – Edison Jul 02 '22 at 07:51
  • 1
    Yes you only need the input shapes for the first layer, all other shapes are inferred. The comma is because shapes are given as tuples – Tobiaaa Jul 03 '22 at 08:25
1

I tried this solution as above :

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

but same problem occurred so i tried to define the inpute_shape of model separately as below code and it works hope this help you:

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

0

Args input_shape Shape tuple (not including the batch axis), or TensorShape instance (not including the batch axis). the input shape does include batch axis as per documentation of keras so try giving input_shape=(30,) instead of input_shape=(455,30)

sangwan
  • 11