如何在 Keras 功能模型中添加 Dropout?

How to add Dropout in Keras functional model?

假设我在 Keras 中有一个 LSTM 层,如下所示:

x = Input(shape=(input_shape), dtype='int32')

x = LSTM(128,return_sequences=True)(x)

现在我尝试使用以下方法将 Dropout 添加到该层:

X = Dropout(0.5)

但这会产生错误,我假设上面的行是重新定义 X 而不是向其添加 Dropout。 如何解决这个问题?

只需像这样添加 x = Dropout(0.5)(x)

x = Input(shape=(input_shape), dtype='int32')
x = LSTM(128,return_sequences=True)(x)
x = Dropout(0.5)(x)