[英]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)
我有一个生成器rev_generator
生成两个元素的元组(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然后是一个简单的 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
但是当我打电话给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)
If you are using the tf.data.Dataset
API, you should set the batch size explicitly and not in model.fit
:如果您使用的是tf.data.Dataset
API,您应该明确设置批量大小,而不是在model.fit
中:
train_ds = train_ds.batch(32)
...
...
model.fit(train_ds,epochs=epochs)
Integer or None. Integer 或无。 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).如果未指定,batch_size 将默认为 32。如果您的数据是数据集、生成器或 keras.utils.Sequence 实例(因为它们生成批次)的形式,请不要指定 batch_size。
Also note that your input shape does not match your input data.另请注意,您的输入形状与您的输入数据不匹配。 You are mixing up 1279 and 1297. Here is a working example:您正在混淆 1279 和 1297。这是一个工作示例:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.