简体   繁体   English

在 keras 中训练 CNN 进行文本分类时出错“ValueError:输入 0 与图层不兼容”

[英]Error while training CNN for text classification in keras "ValueError: Input 0 is incompatible with layer"

I am building a prediction model for sequence data using conv1d layer provided by Keras. This is how I did我正在使用 Keras 提供的 conv1d 层为序列数据构建预测 model。我就是这样做的

input_layer = Input(shape=(500,)) 
layer = Conv1D(128,5,activation="relu")(input_layer)
layer = MaxPooling1D(pool_size=2)(layer)
layer = Flatten()(layer)
layer = Dense(128, activation='relu')(layer)
output_layer = Dense(10, activation='softmax')(layer)
classifier = Model(input_layer, output_layer)
classifier.summary()
classifier.compile(optimizer=optimizers.Adam(), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
return classifier

However, am facing the following error:但是,我面临以下错误:

Traceback (most recent call last):
File "train.py", line 71, in <module>
classifier = create_cnn_model()
File "train.py", line 60, in create_cnn_model
layer = Conv1D(128,5, activation="relu")(input_layer)
File "C:\Python368\lib\site-packages\keras\backend\tensorflow_backend.py", line 75, in symbolic_fn
_wrapper
return func(*args, **kwargs)
File "C:\Python368\lib\site-packages\keras\engine\base_layer.py", line 446, in __call__
self.assert_input_compatibility(inputs)
File "C:\Python368\lib\site-packages\keras\engine\base_layer.py", line 342, in assert_input_compat
ibility
str(K.ndim(x)))
ValueError: Input 0 is incompatible with layer conv1d_1: expected ndim=3, found ndim=2

I think the input_shape in the first layer is not setup right.我认为第一层的 input_shape 设置不正确。 How to set it up?如何设置?

Right, conv layers need 3 dimensional input.对,转换层需要 3 维输入。 I am assuming you have a univariate time series with 500 samples.我假设您有一个包含 500 个样本的单变量时间序列。 You need to write a function to split the time series into steps.您需要编写一个 function 将时间序列拆分为步骤。 For example:例如:

     x             y
[t-n,...,t-2,t-1]  t

So you are basically using the last n values to predict the next value in your series.因此,您基本上是使用最后 n 个值来预测系列中的下一个值。 Then your input shape will be [len(x), n, 1]那么你的输入形状将是 [len(x), n, 1]

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

相关问题 猫狗分类 CNN 中的不兼容输入层大小错误 model - incompatible input layer size error in a Cat Dog Classification CNN model Keras CNN错误conv1d_13层的输入0与层不兼容::预期min_ndim=3,发现ndim=2 - Keras CNN error Input 0 of layer conv1d_13 is incompatible with the layer: : expected min_ndim=3, found ndim=2 Keras ValueError:输入0与图层flatten_11不兼容 - Keras ValueError: input 0 is incompatible with layer flatten_11 Keras {ValueError}:输入0与层conv2d_2不兼容 - Keras {ValueError}: Input 0 is incompatible with layer conv2d_2 文本分类 CNN 过拟合训练 - Text classification CNN overfits training Keras-CNN输入形状不兼容 - Keras - CNN input shape incompatible ValueError: 层序列 10 的输入 0 与层不兼容:预期 ndim=5,发现 ndim=4。 Alexnet(cnn) + LSTM - ValueError: Input 0 of layer sequential_10 is incompatible with the layer: expected ndim=5, found ndim=4. Alexnet(cnn) + LSTM ValueError:层顺序的输入0与层不兼容(重塑错误) - ValueError: Input 0 of layer sequential is incompatible with the layer(Reshape error) ValueError:密集层的输入 0 不兼容 - ValueError: Input 0 of layer dense is incompatible 预训练精度计算给出ValueError: Input 0 of layer sequence is in compatible with the layer: expected ndim=3, found ndim=4 - Pre-training accuracy calculation gives ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=3, found ndim=4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM