forked from eelanagaraj/imdb_experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LSTM_keras.py
79 lines (63 loc) · 2.93 KB
/
LSTM_keras.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from __future__ import absolute_import
from __future__ import print_function
import numpy as np
import cPickle as pkl
from keras.preprocessing import sequence, text
from keras.optimizers import SGD, RMSprop, Adagrad
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.layers.embeddings import Embedding
from keras.layers.recurrent import LSTM, GRU
from text_processing import load_imdb_data
'''
Modified version of keras LSTM example: trains an LSTM network on
the imdb sentiment analysis data set. In addition to predicting on
test data, also stores the model's weights and intermediate
activation values for training and test data.
GPU command:
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python imdb_lstm.py
'''
max_features=20000
maxlen = 100 # cut texts after this number of words (among top max_features most common words)
batch_size = 16
# had some luck with seed 111
print("Loading data...")
(X_train, y_train), (X_test, y_test), w = load_imdb_data(
binary=True, max_features=max_features, maxlen=maxlen, seed=37)
print(len(X_train), 'train sequences')
print(len(X_test), 'test sequences')
print('X_train shape:', X_train.shape)
print('X_test shape:', X_test.shape)
print('Build model...')
model = Sequential()
model.add(Embedding(max_features, 256))
model.add(LSTM(256, 128)) # try using a GRU instead, for fun
model.add(Dropout(0.5))
model.add(Dense(128, 1))
model.add(Activation('sigmoid'))
# try using different optimizers and different optimizer configs
model.compile(loss='binary_crossentropy', optimizer='adam', class_mode="binary")
print("Train...")
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=5, validation_split=0.1, show_accuracy=True)
score = model.evaluate(X_test, y_test, batch_size=batch_size)
print('Test score:', score)
classes = model.predict_classes(X_test, batch_size=batch_size)
acc = np_utils.accuracy(classes, y_test)
print('Test accuracy:', acc)
store_weights = {}
for layer in model.layers :
store_weights[layer] = layer.get_weights()
# create a new model of same structure minus last layers, to explore intermediate outputs
print('Build truncated model')
chopped_model = Sequential()
chopped_model.add(Embedding(max_features, 256, weights=model.layers[0].get_weights()))
chopped_model.add(LSTM(256, 128, weights=model.layers[1].get_weights()))
chopped_model.compile(loss='binary_crossentropy', optimizer='adam', class_mode="binary")
# pickle intermediate outputs, model weights
train_activations = chopped_model.predict(X_train, batch_size=batch_size)
test_activations = chopped_model.predict(X_test, batch_size=batch_size)
outputs = dict(final=classes, acc=acc, weights=store_weights, y_train=y_train, y_test=y_test,
train_activations=train_activations, test_activations=test_activations)
pkl.dump(outputs, open('results/predicted_activations.pkl', 'wb'),
protocol=pkl.HIGHEST_PROTOCOL)