Skip to content

Instantly share code, notes, and snippets.

@ceshine
Created September 30, 2017 02:10
Show Gist options
  • Save ceshine/00c69e52860e0ba19aaa51a0634b6198 to your computer and use it in GitHub Desktop.
Save ceshine/00c69e52860e0ba19aaa51a0634b6198 to your computer and use it in GitHub Desktop.
Key Code Blocks of Keras LSTM Dropout Implementation
# https://github.com/tensorflow/tensorflow/blob/v1.3.0/tensorflow/contrib/keras/python/keras/layers/recurrent.py#L1174
class LSTM(Recurrent):
#...
def get_constants(self, inputs, training=None):
#...
ones = K.ones_like(K.reshape(inputs[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, self.units))
def dropped_inputs(): # pylint: disable=function-redefined
return K.dropout(ones, self.recurrent_dropout)
rec_dp_mask = [
K.in_train_phase(dropped_inputs, ones, training=training)
for _ in range(4)
]
#...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment