Skip to content
Advertisement

Input 0 is incompatible with layer model_2

i have a generator rev_generator that yields a tuple of two elements (numpyarray of shape (1279,300,1) , int value: 0 or 1)

then i pass it to:

train_ds = tf.data.Dataset.from_generator(rev_generator,
                                      output_signature=(tf.TensorSpec(shape=(1279,300,1),dtype=tf.float32),
                                                       tf.TensorSpec(shape=(), dtype=tf.int32)))

and then a simple model

inputs=tf.keras.Input(shape=(1279,300,1,))
x=tf.keras.layers.Conv2D(16, 3, padding='same', activation='relu')(inputs)
x=tf.keras.layers.MaxPooling2D()(x)
x=tf.keras.layers.Flatten()(x)
x=tf.keras.layers.Dense(64, activation='relu')(x)
outputs=tf.keras.layers.Dense(1, activation='relu')(x)
model = tf.keras.Model(inputs, outputs)
model.compile(...)

but when i call fit

model.fit(train_ds,epochs=epochs, batch_size=32)

it throws me an error:

ValueError: Input 0 is incompatible with layer model_2: expected shape=(None, 1279, 300, 1), found shape=(1279, 300, 1)

Advertisement

Answer

If you are using the tf.data.Dataset API, you should set the batch size explicitly and not in model.fit:

train_ds = train_ds.batch(32)
...
...
model.fit(train_ds,epochs=epochs)

See this:

Integer or None. Number of samples per gradient update. If unspecified, batch_size will default to 32. Do not specify the batch_size if your data is in the form of datasets, generators, or keras.utils.Sequence instances (since they generate batches).

Also note that your input shape does not match your input data. You are mixing up 1279 and 1297. Here is a working example:

def gen():
  yield tf.random.normal((1279,300,1)), tf.random.uniform((), maxval=2, dtype=tf.int32)

train_ds = tf.data.Dataset.from_generator(gen,
                                      output_signature=(tf.TensorSpec(shape=(1279,300,1),dtype=tf.float32),
                                                       tf.TensorSpec(shape=(), dtype=tf.int32)))

inputs=tf.keras.Input(shape=(1279,300,1,))
x=tf.keras.layers.Conv2D(16, 3, padding='same', activation='relu')(inputs)
x=tf.keras.layers.MaxPooling2D()(x)
x=tf.keras.layers.Flatten()(x)
x=tf.keras.layers.Dense(64, activation='relu')(x)
outputs=tf.keras.layers.Dense(1, activation='relu')(x)
model = tf.keras.Model(inputs, outputs)
model.compile(optimizer='adam', loss='mse')
model.fit(train_ds.batch(32),epochs=5)
7 People found this is helpful
Advertisement