-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathdoc-rnn2.py
167 lines (127 loc) · 4.86 KB
/
doc-rnn2.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import pandas as pd
from keras.models import Model
from keras.layers import Dense, Input, Dropout, MaxPooling1D, Conv1D
from keras.layers import LSTM, Lambda
from keras.layers import TimeDistributed, Bidirectional
from keras.layers.normalization import BatchNormalization
import numpy as np
import tensorflow as tf
import re
import keras.callbacks
import sys
import os
def binarize(x, sz=71):
return tf.to_float(tf.one_hot(x, sz, on_value=1, off_value=0, axis=-1))
def binarize_outshape(in_shape):
return in_shape[0], in_shape[1], 71
def striphtml(html):
p = re.compile(r'<.*?>')
return p.sub('', html)
def clean(s):
return re.sub(r'[^\x00-\x7f]', r'', s)
# record history of training
class LossHistory(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.losses = []
self.accuracies = []
def on_batch_end(self, batch, logs={}):
self.losses.append(logs.get('loss'))
self.accuracies.append(logs.get('acc'))
total = len(sys.argv)
cmdargs = str(sys.argv)
print ("Script name: %s" % str(sys.argv[0]))
checkpoint = None
if len(sys.argv) == 2:
if os.path.exists(str(sys.argv[1])):
print ("Checkpoint : %s" % str(sys.argv[1]))
checkpoint = str(sys.argv[1])
data = pd.read_csv("labeledTrainData.tsv", header=0, delimiter="\t", quoting=3)
txt = ''
docs = []
sentences = []
sentiments = []
for cont, sentiment in zip(data.review, data.sentiment):
sentences = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', clean(striphtml(cont)))
sentences = [sent.lower() for sent in sentences]
docs.append(sentences)
sentiments.append(sentiment)
num_sent = []
for doc in docs:
num_sent.append(len(doc))
for s in doc:
txt += s
chars = set(txt)
print('total chars:', len(chars))
char_indices = dict((c, i) for i, c in enumerate(chars))
indices_char = dict((i, c) for i, c in enumerate(chars))
print('Sample doc{}'.format(docs[1200]))
maxlen = 512
max_sentences = 15
X = np.ones((len(docs), max_sentences, maxlen), dtype=np.int64) * -1
y = np.array(sentiments)
for i, doc in enumerate(docs):
for j, sentence in enumerate(doc):
if j < max_sentences:
for t, char in enumerate(sentence[-maxlen:]):
X[i, j, (maxlen - 1 - t)] = char_indices[char]
print('Sample chars in X:{}'.format(X[1200, 2]))
print('y:{}'.format(y[1200]))
ids = np.arange(len(X))
np.random.shuffle(ids)
# shuffle
X = X[ids]
y = y[ids]
X_train = X[:20000]
X_test = X[20000:]
y_train = y[:20000]
y_test = y[20000:]
filter_length = [5, 3, 3]
nb_filter = [196, 196, 256]
pool_length = 2
# document input
document = Input(shape=(max_sentences, maxlen), dtype='int64')
# sentence input
in_sentence = Input(shape=(maxlen,), dtype='int64')
# char indices to one hot matrix, 1D sequence to 2D
embedded = Lambda(binarize, output_shape=binarize_outshape)(in_sentence)
# embedded: encodes sentence
for i in range(len(nb_filter)):
embedded = Conv1D(filters=nb_filter[i],
kernel_size=filter_length[i],
padding='valid',
activation='relu',
kernel_initializer='glorot_normal',
strides=1)(embedded)
embedded = Dropout(0.1)(embedded)
embedded = MaxPooling1D(pool_size=pool_length)(embedded)
bi_lstm_sent = \
Bidirectional(LSTM(128, return_sequences=False, dropout=0.15, recurrent_dropout=0.15, implementation=0))(embedded)
# sent_encode = merge([forward_sent, backward_sent], mode='concat', concat_axis=-1)
sent_encode = Dropout(0.3)(bi_lstm_sent)
# sentence encoder
encoder = Model(inputs=in_sentence, outputs=sent_encode)
encoder.summary()
encoded = TimeDistributed(encoder)(document)
# encoded: sentences to bi-lstm for document encoding
b_lstm_doc = \
Bidirectional(LSTM(128, return_sequences=False, dropout=0.15, recurrent_dropout=0.15, implementation=0))(encoded)
output = Dropout(0.3)(b_lstm_doc)
output = Dense(128, activation='relu')(output)
output = Dropout(0.3)(output)
output = Dense(1, activation='sigmoid')(output)
model = Model(inputs=document, outputs=output)
model.summary()
if checkpoint:
model.load_weights(checkpoint)
file_name = os.path.basename(sys.argv[0]).split('.')[0]
check_cb = keras.callbacks.ModelCheckpoint('checkpoints/' + file_name + '.{epoch:02d}-{val_loss:.2f}.hdf5',
monitor='val_loss',
verbose=0, save_best_only=True, mode='min')
earlystop_cb = keras.callbacks.EarlyStopping(monitor='val_loss', patience=7, verbose=1, mode='auto')
history = LossHistory()
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, y_train, validation_data=(X_test, y_test), batch_size=10,
epochs=5, shuffle=True, callbacks=[earlystop_cb, check_cb, history])
# just showing access to the history object
print history.losses
print history.accuracies