Permalink
Browse files

Fix up a few example

  • Loading branch information...
fchollet committed Jul 17, 2016
1 parent f3e7245 commit 01d5e7bc4782daafcfa99e035c1bdbe13a985145
Showing with 14 additions and 10 deletions.
  1. +2 −2 examples/imdb_cnn_lstm.py
  2. +11 −8 examples/lstm_text_generation.py
  3. +1 −0 keras/preprocessing/text.py
@@ -22,9 +22,9 @@
embedding_size = 128
# Convolution
filter_length = 3
filter_length = 5
nb_filter = 64
pool_length = 2
pool_length = 4
# LSTM
lstm_output_size = 70
@@ -14,6 +14,7 @@
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.layers import LSTM
from keras.optimizers import RMSprop
from keras.utils.data_utils import get_file
import numpy as np
import random
@@ -50,20 +51,22 @@
# build the model: 2 stacked LSTM
print('Build model...')
model = Sequential()
model.add(LSTM(512, return_sequences=True, input_shape=(maxlen, len(chars))))
model.add(LSTM(512, return_sequences=False))
model.add(Dropout(0.2))
model.add(LSTM(128, input_shape=(maxlen, len(chars))))
model.add(Dense(len(chars)))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
optimizer = RMSprop(lr=0.01)
model.compile(loss='categorical_crossentropy', optimizer=optimizer)
def sample(a, temperature=1.0):
def sample(preds, temperature=1.0):
# helper function to sample an index from a probability array
a = np.log(a) / temperature
a = np.exp(a) / np.sum(np.exp(a))
return np.argmax(np.random.multinomial(1, a, 1))
preds = np.asarray(preds).astype('float64')
preds = np.log(preds) / temperature
exp_preds = np.exp(preds)
preds = exp_preds / np.sum(exp_preds)
probas = np.random.multinomial(1, preds, 1)
return np.argmax(probas)
# train the model, output generated text after each iteration
for iteration in range(1, 60):
@@ -99,6 +99,7 @@ def fit_on_texts(self, texts):
wcounts = list(self.word_counts.items())
wcounts.sort(key=lambda x: x[1], reverse=True)
sorted_voc = [wc[0] for wc in wcounts]
# note that index 0 is reserved, never assigned to an existing word
self.word_index = dict(list(zip(sorted_voc, list(range(1, len(sorted_voc) + 1)))))
self.index_docs = {}

1 comment on commit 01d5e7b

@hdmetor

This comment has been minimized.

Show comment
Hide comment
@hdmetor

hdmetor Aug 18, 2016

I referenced this commit here. I was wondering if the removal of the 2 LSTM is wanted, or just a copy paste from other examples (imdb_lstm maybe?)

hdmetor commented on 01d5e7b Aug 18, 2016

I referenced this commit here. I was wondering if the removal of the 2 LSTM is wanted, or just a copy paste from other examples (imdb_lstm maybe?)

Please sign in to comment.