简体   繁体   English

输入 0 与层 model_2 不兼容

[英]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)

See this :看到 这个

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.

相关问题 ValueError:输入 0 与层 model_2 不兼容:预期形状 =(None, 160, 160, 3),找到的形状 =(32, 160, 3) - ValueError: Input 0 is incompatible with layer model_2: expected shape=(None, 160, 160, 3), found shape=(32, 160, 3) 输入与聊天机器人预测层不兼容 model - Input incompatible with the layer for chatbot prediction model Python Keras model 输入与层不兼容 - Python Keras model input incompatible with layer ValueError: TensorFlow2 Input 0 is in compatible with layer model - ValueError: TensorFlow2 Input 0 is incompatible with layer model ResNet: ValueError: Input 0 is in compatible with layer model_7 - ResNet: ValueError: Input 0 is incompatible with layer model_7 ValueError: Input 0 of layer "sequential_8" is in compatible with the layer - 深度学习 model - ValueError: Input 0 of layer "sequential_8" is incompatible with the layer - deep learning model 层顺序的输入 0 与层不兼容: - Input 0 of layer sequential is incompatible with the layer: 层序的输入0与层不兼容 - Input 0 of layer sequential is incompatible with the layer 猫狗分类 CNN 中的不兼容输入层大小错误 model - incompatible input layer size error in a Cat Dog Classification CNN model 由于输入层不兼容,深度学习模型没有给出预测 - Deep learning model not giving predictions as input layer is incompatible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM