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