Recurrent Neural Networks (RNNs) for Language Modeling with Keras
David Cecchini
Data Scientist
Sentence is determined by punctuation. For example, .
(period), !
(exclamation) or ?
(question).
There is a sentence token, e.g. <SENT>
and </SENT>
, that determines when a sentence begins and ends.
sentence = ''
# Loop until end of sentence while next_char != '.':
# Predict next char: Get pred array in position 0 pred = model.predict(X)[0]
char_index = np.argmax(pred)
next_char = index_to_char(char_index)
# Concatenate to sentence sentence = sentence + next_char
Scale the probability distribution.
def scale_softmax(softmax_pred, temperature=1.0): # Take the logarithm scaled_pred = np.log(softmax_pred) / temperature
# Re-apply the exponential scaled_pred = np.exp(scaled_pred)
# Build probability distribution scaled_pred = scaled_pred / np.sum(scaled_pred)
# Simulate multinomial scaled_pred = np.random.multinomial(1, scaled_pred, 1)
# Return simulated class return np.argmax(scaled_pred)
Recurrent Neural Networks (RNNs) for Language Modeling with Keras