当切换到双向层时,我的代码给出一个错误。使用标准LSTM层时,该模型可以工作。我得到的错误是:
File "<ipython-input-22-1e9000cb0bf6>", line 12, in <module>
model.build(input_shape)
ValueError: Input 0 is incompatible with layer bidirectional_11: expected ndim=3, found ndim=2
我的代码如下:
input_shape = (lengtharray,1)
model = Sequential()
model.add(Bidirectional(CuDNNLSTM(128, input_shape=input_shape, return_sequences=True)))
model.add(Dropout(0.5))
model.add(Dense(64))
model.add(Bidirectional(CuDNNLSTM(128)))
model.add(Dropout(0.5))
model.add(Dense(64))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
model.build(input_shape)
opt = keras.optimizers.Adam(lr=0.01, decay=0.0001)
model.compile(loss='binary_crossentropy',
optimizer=opt,
metrics=['accuracy'])
callbacks = [EarlyStopping(monitor='val_loss'),
ModelCheckpoint(filepath='best_model.h5', monitor='val_loss', save_best_only=True)]
model.summary()
为什么使用双向而不是标准LSTM时会返回形状错误?