forked from ultimate010/crnn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsst1_cnn_rnn.py
143 lines (117 loc) · 4.47 KB
/
sst1_cnn_rnn.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
'''
Run on GPU: THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python
'''
from __future__ import print_function
import numpy as np
np.random.seed(3435) # for reproducibility, should be first
from keras.preprocessing import sequence
from keras.models import Sequential, Graph
from keras.layers import Dropout, Activation, Flatten, \
Embedding, Convolution1D, MaxPooling1D, AveragePooling1D, \
Input, Dense, merge
from keras.regularizers import l2
from keras.layers.recurrent import LSTM, GRU, SimpleRNN
from keras.constraints import maxnorm
from keras.datasets import imdb
from keras import callbacks
from keras.utils import generic_utils
from keras.models import Model
from keras.optimizers import Adadelta
from keras.utils import np_utils
import time
batch_size = 50
nb_filter = 200
filter_length = 4
hidden_dims = nb_filter * 2
nb_epoch = 60
RNN = GRU
rnn_output_size = 100
folds = 10
nb_classes = 5
print('Loading data...')
from process_sst_data import SentimentPhrase
from sst1_data import load_data
X_train, y_train, X_test, y_test, W, W2 = load_data()
y_train = np_utils.to_categorical(y_train, nb_classes)
y_test = np_utils.to_categorical(y_test, nb_classes)
maxlen = X_train.shape[1]
max_features = len(W)
embedding_dims = len(W[0])
print('Train...')
accs = []
first_run = True
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)
rand_idx = np.random.permutation(range(len(X_train)))
X_train = X_train[rand_idx]
y_train = y_train[rand_idx]
def build_model():
main_input = Input(shape=(maxlen, ), dtype='int32', name='main_input')
embedding = Embedding(max_features, embedding_dims,
weights=[np.matrix(W)], input_length=maxlen,
name='embedding')(main_input)
embedding = Dropout(0.50)(embedding)
conv4 = Convolution1D(nb_filter=nb_filter,
filter_length=4,
border_mode='valid',
activation='relu',
subsample_length=1,
name='conv4')(embedding)
maxConv4 = MaxPooling1D(pool_length=2,
name='maxConv4')(conv4)
conv5 = Convolution1D(nb_filter=nb_filter,
filter_length=5,
border_mode='valid',
activation='relu',
subsample_length=1,
name='conv5')(embedding)
maxConv5 = MaxPooling1D(pool_length=2,
name='maxConv5')(conv5)
x = merge([maxConv4, maxConv5], mode='concat')
x = Dropout(0.15)(x)
x = RNN(rnn_output_size)(x)
x = Dense(hidden_dims, activation='relu', init='he_normal',
W_constraint = maxnorm(3), b_constraint=maxnorm(3),
name='mlp')(x)
x = Dropout(0.10, name='drop')(x)
output = Dense(nb_classes, init='he_normal',
activation='softmax', name='output')(x)
model = Model(input=main_input, output=output)
model.compile(loss={'output':'categorical_crossentropy'},
optimizer=Adadelta(lr=0.95, epsilon=1e-06),
metrics=["accuracy"])
return model
model = build_model()
if first_run:
first_run = False
print(model.summary())
best_val_acc = 0
best_test_acc = 0
for j in xrange(nb_epoch):
a = time.time()
his = model.fit(X_train, y_train,
batch_size=batch_size,
validation_data=[X_test, y_test],
shuffle=True,
nb_epoch=1, verbose=1)
print('Epoch %d/%d\t%s' % (j + 1, nb_epoch, str(his.history)))
if his.history['val_acc'][0] >= best_val_acc:
score, acc = model.evaluate(X_test, y_test,
batch_size=batch_size,
verbose=2)
best_val_acc = his.history['val_acc'][0]
best_test_acc = acc
print('Got best epoch best val acc is %f test acc is %f' %
(best_val_acc, best_test_acc))
if len(accs) > 0:
print('Current avg test acc:', str(np.mean(accs)))
b = time.time()
cost = b - a
left = (nb_epoch - j - 1)
print('One round cost %ds, %d round %ds %dmin left' % (cost, left,
cost * left,
cost * left / 60.0))
accs.append(best_test_acc)
print('Avg test acc:', str(np.mean(accs)))