forked from textclf/fancy-cnn
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel-test.py
150 lines (104 loc) · 4.85 KB
/
model-test.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import logging
from keras.layers.recurrent import LSTM, GRU
from keras.models import Sequential, model_from_json, Graph
from keras.layers.core import Dense, Dropout, MaxoutDense, Activation
from keras.layers.advanced_activations import PReLU
from keras.callbacks import EarlyStopping, ModelCheckpoint, Callback
from keras.optimizers import SGD
from cnn.layers.convolutions import *
from cnn.layers.embeddings import *
import numpy as np
import cPickle as pickle
LOGGER_PREFIX = ' %s'
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def log(msg, logger=logger):
logger.info(LOGGER_PREFIX % msg)
if __name__ == '__main__':
WV_FILE = './data/wv/IMDB-GloVe-100dim-glovebox.pkl'
MODEL_FILE = './test-model.h5'
# -- load in all the data
train, test = {}, {}
log('Loading training data')
train['text'] = np.load('IMDB_train_glove_X.npy')
train['labels'] = np.load('IMDB_train_glove_y.npy')
log('Shuffling training data')
shuff = range(train['text'].shape[0])
np.random.shuffle(shuff)
train['text'], train['labels'] = train['text'][shuff], train['labels'][shuff]
# -- flatten across paragraph dimension, will later be reconstructed in the embedding
train['text'] = train['text'].reshape(train['text'].shape[0], -1)
weights = 1.0 * (train['text'] > 0)
del shuff
log('Loading testing data')
# -- testing data
test['text'] = np.load('IMDB_test_glove_X.npy')
test['text'] = test['text'].reshape(test['text'].shape[0], -1)
test['labels'] = np.load('IMDB_test_glove_y.npy')
log('Loading IMDB trained word vectors')
gb = pickle.load(open(WV_FILE, 'rb'))
WV_PARAMS = {
'floating_wv' :
{
'vocab_size' : gb.W.shape[0],
'init' : gb.W,
'fixed' : False
}
}
NGRAMS = [2, 3, 4, 5, 7, 9]
NFILTERS = 32
SENTENCE_LENGTH = 50
PARAGRAPH_LENGTH = 50
log('Making graph model')
graph = Graph()
graph.add_input(name='text', input_shape=(-1, ), dtype='int')
log('Making embedding')
embed = paragraph_embedding(PARAGRAPH_LENGTH, WV_PARAMS, WV_PARAMS['floating_wv']['init'].shape[1])
graph.add_node(embed, name='embedding', input='text')
# graph.add_node(Embedding(WV_PARAMS['floating_wv']['vocab_size'], ), name='embedding', input='text')
log('Adding convolved n-grams')
# for n in [4, 5]:
for n in NGRAMS:
graph.add_node(
TimeDistributedConvolution2D(NFILTERS, n, WV_PARAMS['floating_wv']['init'].shape[1], activation='relu'),
name='conv{}gram'.format(n), input='embedding')
graph.add_node(
TimeDistributedMaxPooling2D(pool_size=(SENTENCE_LENGTH - n + 1, 1)),
name='maxpool{}gram'.format(n), input='conv{}gram'.format(n))
graph.add_node(
Dropout(0.7),
name='dropout{}gram'.format(n), input='maxpool{}gram'.format(n))
graph.add_node(
TimeDistributedFlatten(),
name='flatten{}gram'.format(n), input='dropout{}gram'.format(n))
log('Adding bi-directional GRU')
graph.add_node(GRU(25), name='gru_forwards', inputs=['flatten{}gram'.format(n) for n in NGRAMS], concat_axis=-1)
graph.add_node(GRU(25, go_backwards=True), name='gru_backwards', inputs=['flatten{}gram'.format(n) for n in NGRAMS], concat_axis=-1)
# graph.add_node(GRU(16), name='gru', input='flatten4gram')
graph.add_node(Dropout(0.5), name='gru_dropout', inputs=['gru_forwards', 'gru_backwards'])
graph.add_node(Dense(1, activation='sigmoid'), name='probability', input='gru_dropout')
graph.add_output(name='prediction', input='probability')
log('Compiling model (Veuillez patienter)...')
sgd = SGD(lr=0.01, momentum=0.8, decay=0.0001, nesterov=True)
graph.compile(sgd, {'prediction': 'binary_crossentropy'})
log('Fitting! Hit CTRL-C to stop early...')
try:
history = graph.fit(
{'text': train['text'], 'prediction': train['labels']},
validation_split=0.35, batch_size=28, nb_epoch=100,
verbose=2, # -- for logging purposes
sample_weight = {'prediction' : weights}, callbacks =
[
EarlyStopping(verbose=True, patience=30, monitor='val_loss'),
ModelCheckpoint(MODEL_FILE, monitor='val_loss', verbose=True, save_best_only=True)
]
)
except KeyboardInterrupt:
log('Training stopped early!')
log('Loading best weights...')
graph.load_weights(MODEL_FILE)
log('getting predictions on the test set')
yhat = graph.predict({'text': test['text']}, verbose=True, batch_size=50)
acc = ((yhat['prediction'].ravel() > 0.5) == (test['labels'] > 0.5)).mean()
log('Test set accuracy of {}%.'.format(acc * 100.0))
log('Test set error of {}%. Exiting...'.format((1 - acc) * 100.0))