web-dev-qa-db-ja.com

ValueError:入力0はレイヤーconv1d_1と互換性がありません:ndim = 3が必要です、ndim = 4が見つかりました

Kerasが提供するconv1dレイヤーを使用してシーケンスデータの予測モデルを構築しています。これは私がやった方法です

_model= Sequential()
model.add(Conv1D(60,32, strides=1, activation='relu',padding='causal',input_shape=(None,64,1)))
model.add(Conv1D(80,10, strides=1, activation='relu',padding='causal'))
model.add(Dropout(0.25))
model.add(Conv1D(100,5, strides=1, activation='relu',padding='causal'))
model.add(MaxPooling1D(1))
model.add(Dropout(0.25))
model.add(Dense(300,activation='relu'))
model.add(Dense(1,activation='relu'))
print(model.summary())
_

ただし、デバッグ情報には

_Traceback (most recent call last):
File "processing_2a_1.py", line 96, in <module>
model.add(Conv1D(60,32, strides=1, activation='relu',padding='causal',input_shape=(None,64,1)))
File "build/bdist.linux-x86_64/Egg/keras/models.py", line 442, in add
File "build/bdist.linux-x86_64/Egg/keras/engine/topology.py", line 558, in __call__
File "build/bdist.linux-x86_64/Egg/keras/engine/topology.py", line 457, in assert_input_compatibility
ValueError: Input 0 is incompatible with layer conv1d_1: expected ndim=3, found ndim=4
_

トレーニングデータと検証データの形状は次のとおりです

_('X_train shape ', (1496000, 64, 1))
('Y_train shape ', (1496000, 1))
('X_val shape ', (374000, 64, 1))
('Y_val shape ', (374000, 1))
_

最初のレイヤーの_input_shape_が正しく設定されていなかったと思います。設定方法は?


pdateinput_shape=(64,1)を使用した後、モデルの概要が実行されているにもかかわらず、次のエラーメッセージが表示されました

_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
conv1d_1 (Conv1D)            (None, 64, 60)            1980
_________________________________________________________________
conv1d_2 (Conv1D)            (None, 64, 80)            48080
_________________________________________________________________
dropout_1 (Dropout)          (None, 64, 80)            0
_________________________________________________________________
conv1d_3 (Conv1D)            (None, 64, 100)           40100
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 64, 100)           0
_________________________________________________________________
dropout_2 (Dropout)          (None, 64, 100)           0
_________________________________________________________________
dense_1 (Dense)              (None, 64, 300)           30300
_________________________________________________________________
dense_2 (Dense)              (None, 64, 1)             301
=================================================================
Total params: 120,761
Trainable params: 120,761
Non-trainable params: 0
_________________________________________________________________
None
Traceback (most recent call last):
  File "processing_2a_1.py", line 125, in <module>
    history=model.fit(X_train, Y_train, batch_size=batch_size, validation_data=(X_val,Y_val), epochs=nr_of_epochs,verbose=2)
  File "build/bdist.linux-x86_64/Egg/keras/models.py", line 871, in fit
  File "build/bdist.linux-x86_64/Egg/keras/engine/training.py", line 1524, in fit
  File "build/bdist.linux-x86_64/Egg/keras/engine/training.py", line 1382, in _standardize_user_data
  File "build/bdist.linux-x86_64/Egg/keras/engine/training.py", line 132, in _standardize_input_data
ValueError: Error when checking target: expected dense_2 to have 3 dimensions, but got array with shape (1496000, 1)
_
6
user288609

input_shapeを次のように変更する必要があります

input_shape=(64,1)

...またはbatch_input_shapeを使用:

batch_input_shape=(None, 64, 1)

このディスカッション は、ケラの2つの違いを詳細に説明しています。

3
Maxim