Skip to content

Instantly share code, notes, and snippets.

@milhidaka
Created June 19, 2017 09:01
Show Gist options
  • Save milhidaka/a1849913dab1b5cbfc32b210aa07af0a to your computer and use it in GitHub Desktop.
Save milhidaka/a1849913dab1b5cbfc32b210aa07af0a to your computer and use it in GitHub Desktop.
runs model trained by imdb_lstm.py and gets input-output pair
# runs model trained by imdb_lstm.py and gets input-output pair
import numpy as np
import keras
from keras.preprocessing import sequence
from keras.datasets import imdb
max_features = 20000
maxlen = 80 # cut texts after this number of words (among top max_features most common words)
batch_size = 32
print('Loading data...')
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
print(len(x_train), 'train sequences')
print(len(x_test), 'test sequences')
print('Pad sequences (samples x time)')
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
print('x_train shape:', x_train.shape)
print('x_test shape:', x_test.shape)
model = keras.models.load_model("imdb_lstm.h5")
pred_test = model.predict(x_test, batch_size=batch_size)
np.savez("imdb_lstm_result.npz", y_test=y_test, x_test=x_test, pred_test=pred_test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment