简体   繁体   English

ValueError:“顺序”层的输入 0 与层不兼容:预期形状 =(无,223461,5),找到形状 =(无,5)

[英]ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 223461, 5), found shape=(None, 5)

I use a model with a combinaison of GRu and Conv1D.我使用结合了 GRu 和 Conv1D 的模型。 When I want to fit the model I get an error in:当我想拟合模型时,出现以下错误:

ValueError: Input 0 of layer "sequential_8" is incompatible with the layer: expected shape=(None, 223461, 5), found shape=(None, 5) ValueError:层“sequential_8”的输入0与层不兼容:预期形状=(无,223461,5),发现形状=(无,5)

The shape of X_train is (223461, 5) , whereas the y_train is (223461,) X_train 的形状是 (223461, 5) ,而y_train 是 (223461,)

This is my code:这是我的代码:

verbose, epochs, batch_size = 0, 100, 64
n_timesteps, n_features, n_outputs = X_train.shape[0], X_train.shape[1], y_train.shape[0]
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps,n_features)))
model.add(MaxPooling1D(pool_size=2))
model.add(GRU(64))
model.add(Dropout(0.4))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(n_outputs, activation='softmax'))
opt = Adam(learning_rate=0.01)
model.compile(loss='categorical_crossentropy', optimizer=opt , metrics=['accuracy'])
model.summary()

The output of summary is:摘要的输出是:

Model: "sequential_8"
_____  Layer (type)                Output Shape              Param #
=====  conv1d_8 (Conv1D)           (None, 223459, 64)        1024
        max_pooling1d_8 (MaxPooling  (None, 111729, 64)       0           1D)

        gru_7 (GRU)                 (None, 64)                24960
        dropout_14 (Dropout)        (None, 64)                0
        flatten_6 (Flatten)         (None, 64)                0
        dense_14 (Dense)            (None, 128)               8320
        dropout_15 (Dropout)        (None, 128)               0
        dense_15 (Dense)            (None, 223461)            28826469

===== Total params: 28,860,773 Trainable params: 28,860,773 Non-trainable params: 0
_____

and here where I face the error:在这里我面临错误:

model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=verbose)
_, accuracy = model.evaluate(X_test, y_test, batch_size=batch_size, verbose=0)

According to your model, your training data x_train and y_train is just a piece of data.根据您的模型,您的训练数据x_trainy_train只是一个数据。

So your training data have to expand the dimension, like this:所以你的训练数据必须扩大维度,像这样:

X_train = X_train[None,:]
y_train = y_train[None,:]

Or use tensorflow function to do this :或者使用 tensorflow 函数来做到这一点:

X_train = tf.expand_dims(X_train, axis=0)
y_train = tf.expand_dims(y_train, axis=0)

The output shape of the model will be (1,223461)模型的输出形状为 (1,223461)

If the output is not what you expected, it means your model design is wrong.如果输出不是你所期望的,这意味着你的模型设计是错误的。

暂无
暂无

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

相关问题 ValueError:“顺序”层的输入 0 与该层不兼容:预期形状 =(None, 455, 30),找到的形状 =(None, 30) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 455, 30), found shape=(None, 30) ValueError: 层“sequential_1”的输入 0 与层不兼容:预期形状=(None, 60, 1),发现形状=(None, 59, 1) - ValueError: Input 0 of layer "sequential_1" is incompatible with the layer: expected shape=(None, 60, 1), found shape=(None, 59, 1) ValueError:层“顺序”的输入0与层不兼容:预期形状=(无,90),发现形状=(无,2,90) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 90), found shape=(None, 2, 90) ValueError:“顺序”层的输入 0 与该层不兼容:预期形状 =(无,33714,12),找到形状 =(无,12) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 33714, 12), found shape=(None, 12) | ValueError:“顺序”层的输入 0 与该层不兼容:预期形状 =(None, 28, 28),找到的形状 =(None, 28, 3) - | ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 28, 28), found shape=(None, 28, 3) ValueError: 层“sequential_32”的输入 0 与层不兼容:预期形状=(None, 3, 1),发现形状=(32, 0, 1) - ValueError: Input 0 of layer "sequential_32" is incompatible with the layer: expected shape=(None, 3, 1), found shape=(32, 0, 1) Keras LSTM ValueError:层“顺序”的输入 0 与层不兼容:预期形状 =(无,478405,33),找到形状 =(1、33) - Keras LSTM ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 478405, 33), found shape=(1, 33) ValueError:“顺序”层的输入 0 与该层不兼容:预期形状=(无,128,128,3),找到形状=(32,128,3) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 128, 128, 3), found shape=(32, 128, 3) ValueError:层“顺序”的输入 0 与层不兼容:预期形状=(无,32,32,3),找到形状=(32,32,3) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3) ValueError: 层“sequential_2”的输入 0 与层不兼容:预期形状=(None, 256, 256, 3),发现形状=(None, 1, 256, 256, 3) - ValueError: Input 0 of layer "sequential_2" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(None, 1, 256, 256, 3)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM