How to add Dropout in Keras functional model?

A.Razavi picture A.Razavi · Feb 7, 2018 · Viewed 7.5k times · Source

Let's say I have an LSTM layer in Keras like this:

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

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

Now I am trying to add Dropout to this layer using:

X = Dropout(0.5)

but this gives error, which I am assuming the above line is redefining X instead of adding Dropout to it. How to fix this?

Answer

Ioannis Nasios picture Ioannis Nasios · Feb 7, 2018

Just add x = Dropout(0.5)(x) like this:

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