【发布时间】:2022-06-18 01:20:15
【问题描述】:
我使用结合了 GRu 和 Conv1D 的模型。当我想拟合模型时,出现以下错误:
ValueError: Input 0 of layer "sequential_8" is in compatible with the layer: expected shape=(None, 223461, 5), found shape=(None, 5)
X_train 的形状是 (223461, 5),而 y_train 的形状是 (223461,)
这是我的代码:
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()
summary的输出是:
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
_____
在这里我面临错误:
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)
【问题讨论】:
标签: python tensorflow keras conv-neural-network conv1d