![](/img/trans.png)
[英]ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 455, 30), found shape=(None, 30)
[英]ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 223461, 5), found shape=(None, 5)
我使用結合了 GRu 和 Conv1D 的模型。 當我想擬合模型時,出現以下錯誤:
ValueError:層“sequential_8”的輸入0與層不兼容:預期形狀=(無,223461,5),發現形狀=(無,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()
摘要的輸出是:
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)
根據您的模型,您的訓練數據x_train
和y_train
只是一個數據。
所以你的訓練數據必須擴大維度,像這樣:
X_train = X_train[None,:]
y_train = y_train[None,:]
或者使用 tensorflow 函數來做到這一點:
X_train = tf.expand_dims(X_train, axis=0)
y_train = tf.expand_dims(y_train, axis=0)
模型的輸出形狀為 (1,223461)
如果輸出不是你所期望的,這意味着你的模型設計是錯誤的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.