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