简体   繁体   English

Tensorflow Model 输入形状错误:层顺序_11的输入0与层不兼容:未定义等级,但层需要定义的等级

[英]Tensorflow Model Input Shape Error: Input 0 of layer sequential_11 incompatible with layer: rank undefined, but the layer requires a defined rank

I am trying to train a 1D CNN model in TensorFlow with data of the input shape (14400,1), but I am receiving an error that the input shape is incompatible with the model.我正在尝试使用输入形状 (14400,1) 的数据在 TensorFlow 中训练一维 CNN model,但我收到输入形状与 Z20F35E630DAF3594DBFA4C3F68D 不兼容的错误I have ensured that my input data is of the correct shape.我已确保我的输入数据具有正确的形状。 I am using TensorFlow version 2.3.0我正在使用 TensorFlow 版本 2.3.0

Batches Snippet (32 examples per batch, data shape - (14400,1), label shape - (1,1) )批次片段(每批次 32 个示例,数据形状 - (14400,1),label 形状 - (1,1))

batch:  0
Data shape:  (32, 14400, 1) (32, 1, 1)
batch:  1
Data shape:  (32, 14400, 1) (32, 1, 1)
batch:  2
Data shape:  (32, 14400, 1) (32, 1, 1)
batch:  3
Data shape:  (32, 14400, 1) (32, 1, 1)
batch:  4
Data shape:  (32, 14400, 1) (32, 1, 1)
batch:  5
Data shape:  (32, 14400, 1) (32, 1, 1)

CNN Model CNN Model

model = Sequential()
model.add(Conv1D(128, kernel_size=5, activation='relu', input_shape=(14400,1)))
model.add(BatchNormalization())
model.add(Dropout(.2))
model.add(Conv1D(32, kernel_size=5, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(.2))
model.add(Dense(64, activation='relu'))
model.add(Dropout(.2))
model.add(Dense(32, activation='relu'))
model.add(Dropout(.2))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.summary()

Model Summary Model 总结

Model: "sequential_11"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_19 (Conv1D)           (None, 14396, 128)        768       
_________________________________________________________________
batch_normalization_10 (Batc (None, 14396, 128)        512       
_________________________________________________________________
dropout_40 (Dropout)         (None, 14396, 128)        0         
_________________________________________________________________
conv1d_20 (Conv1D)           (None, 14392, 32)         20512     
_________________________________________________________________
batch_normalization_11 (Batc (None, 14392, 32)         128       
_________________________________________________________________
dropout_41 (Dropout)         (None, 14392, 32)         0         
_________________________________________________________________
flatten_8 (Flatten)          (None, 460544)            0         
_________________________________________________________________
dense_32 (Dense)             (None, 128)               58949760  
_________________________________________________________________
dropout_42 (Dropout)         (None, 128)               0         
_________________________________________________________________
dense_33 (Dense)             (None, 64)                8256      
_________________________________________________________________
dropout_43 (Dropout)         (None, 64)                0         
_________________________________________________________________
dense_34 (Dense)             (None, 32)                2080      
_________________________________________________________________
dropout_44 (Dropout)         (None, 32)                0         
_________________________________________________________________
dense_35 (Dense)             (None, 1)                 33        
=================================================================
Total params: 58,982,049
Trainable params: 58,981,729
Non-trainable params: 320
_________________________________________________________________

Code that causes the error导致错误的代码

history = model.fit(train_ds, validation_data=val_ds, epochs=10)

Error Message错误信息

ValueError: in user code:

    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:806 train_function  *
        return step_function(self, iterator)
    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:796 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
        return fn(*args, **kwargs)
    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:789 run_step  **
        outputs = model.train_step(data)
    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:747 train_step
        y_pred = self(x, training=True)
    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py:976 __call__
        self.name)
    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/input_spec.py:168 assert_input_compatibility
        layer_name + ' is incompatible with the layer: '

    ValueError: Input 0 of layer sequential_11 is incompatible with the layer: its rank is undefined, but the layer requires a defined rank.

I would greatly appreciate the help.我将非常感谢您的帮助。

you can see the documentation of Conv1D here [https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv1D]您可以在此处查看 Conv1D 的文档 [https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv1D]

The input shape of your first layer should include the batch size.第一层的输入形状应包括批量大小。 (32,14440,1) if you try this script below you will have the same error (32,14440,1) 如果您在下面尝试此脚本,您将遇到相同的错误

input_shape = (14440,1)
x = tf.random.normal(input_shape)
y = tf.keras.layers.Conv1D(128, 5,   activation='relu',input_shape=input_shape)(x)
y.shape

but with the input shape (32,14440,1) it works.但使用输入形状 (32,14440,1) 它可以工作。

I figured out my problem.我发现了我的问题。 It was coming from the custom generator I built with the tf.data.Dataset.from_generator function.它来自我使用 tf.data.Dataset.from_generator function 构建的自定义生成器。 Since I didn't specify the output shapes of the data and labels, then these shapes were defined as unknown, and the input layer of the network couldn't figure out the shape of the data.由于我没有指定数据和标签的 output 形状,因此这些形状被定义为未知,网络的输入层无法确定数据的形状。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:密集层的输入 0 与该层不兼容:其等级未定义,但该层需要定义的等级 - ValueError: Input 0 of layer dense is incompatible with the layer: its rank is undefined, but the layer requires a defined rank 错误:其等级未定义,但该层需要已定义等级 - Error: its rank is undefined, but the layer requires a defined rank Tensorflow Conv2D 层 input_shape 配置错误:ValueError: Input 0 of layer "sequential" is in compatible with the layer: - Tensorflow Conv2D layers input_shape configuration error: ValueError: Input 0 of layer "sequential" is incompatible with the layer: 设置tensorflow序列模型输入层的形状 - Setting the shape of tensorflow sequential model input layer TensorFlow ValueError:层顺序的输入0与层不兼容 - TensorFlow ValueError: Input 0 of layer sequential is incompatible with the layer 层序的输入0与层不兼容 - Input 0 of layer sequential is incompatible with the layer 层顺序的输入 0 与层不兼容: - Input 0 of layer sequential is incompatible with the layer: Keras:层顺序的输入 0 与层不兼容 - Keras: Input 0 of layer sequential is incompatible with the layer "ValueError: Input 0 of layer "sequential" is in compatible with the layer" 在预测中 - "ValueError: Input 0 of layer "sequential" is incompatible with the layer" In prediction 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM