forked from laxmimerit/NLP-Models-Tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
63.deep-pyramid-cnn.py
250 lines (197 loc) · 6.98 KB
/
63.deep-pyramid-cnn.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
from utils import *
import tensorflow as tf
from sklearn.cross_validation import train_test_split
import time
import random
import os
# In[ ]:
trainset = sklearn.datasets.load_files(container_path = 'data', encoding = 'UTF-8')
trainset.data, trainset.target = separate_dataset(trainset,1.0)
print (trainset.target_names)
print (len(trainset.data))
print (len(trainset.target))
# In[ ]:
concat = ' '.join(trainset.data).split()
vocabulary_size = len(list(set(concat)))
data, count, dictionary, rev_dictionary = build_dataset(concat, vocabulary_size)
print('vocab from size: %d'%(vocabulary_size))
print('Most common words', count[4:10])
print('Sample data', data[:10], [rev_dictionary[i] for i in data[:10]])
# In[ ]:
GO = dictionary['GO']
PAD = dictionary['PAD']
EOS = dictionary['EOS']
UNK = dictionary['UNK']
# In[ ]:
embedding_size = 128
dimension_output = len(trainset.target_names)
maxlen = 50
batch_size = 32
kernel_size = 3
num_filters = 150
# In[ ]:
class Model:
def __init__(self,
maxlen,
dimension_output,
vocab_size,
embedding_size,
kernel_size,
num_filters,
learning_rate):
self.X = tf.placeholder(tf.int32,[None, maxlen])
self.Y = tf.placeholder(tf.int32,[None])
embeddings = tf.Variable(tf.random_uniform([vocab_size, embedding_size], -1, 1))
embedded = tf.nn.embedding_lookup(embeddings, self.X)
first_region = tf.layers.conv1d(
embedded,
num_filters,
kernel_size = kernel_size,
strides = 1,
padding = 'valid'
)
forward = tf.nn.relu(first_region)
forward = tf.layers.conv1d(
forward,
num_filters,
kernel_size = kernel_size,
strides = 1,
padding = 'same'
)
forward = tf.layers.batch_normalization(forward)
forward = tf.nn.relu(first_region)
forward = tf.layers.conv1d(
forward,
num_filters,
kernel_size = kernel_size,
strides = 1,
padding = 'same'
)
forward = tf.layers.batch_normalization(forward)
forward = tf.nn.relu(first_region)
forward = forward + first_region
def _block(x):
x = tf.pad(x, paddings=[[0, 0], [0, 1], [0, 0]])
px = tf.layers.max_pooling1d(x, 3, 2)
x = tf.nn.relu(px)
x = tf.layers.conv1d(
x,
num_filters,
kernel_size = kernel_size,
strides = 1,
padding = 'same'
)
x = tf.layers.batch_normalization(x)
x = tf.nn.relu(x)
x = tf.layers.conv1d(
x,
num_filters,
kernel_size = kernel_size,
strides = 1,
padding = 'same'
)
x = tf.layers.batch_normalization(x)
x = x + px
return x
while forward.get_shape().as_list()[1] >= 2:
forward = _block(forward)
self.logits = tf.reduce_sum(tf.layers.conv1d(
forward, dimension_output, kernel_size = 1, strides = 1, padding = 'SAME'
), 1)
self.cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=self.logits,
labels=self.Y))
self.optimizer = tf.train.AdamOptimizer(learning_rate).minimize(self.cost)
correct_pred = tf.equal(tf.argmax(self.logits, 1,output_type=tf.int32), self.Y)
self.accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
# In[ ]:
tf.reset_default_graph()
sess = tf.InteractiveSession()
model = Model(maxlen, dimension_output, len(dictionary), embedding_size,
kernel_size, num_filters, 1e-3)
sess.run(tf.global_variables_initializer())
# In[ ]:
vectors = str_idx(trainset.data,dictionary,maxlen)
train_X, test_X, train_Y, test_Y = train_test_split(vectors, trainset.target,test_size = 0.2)
# In[ ]:
from tqdm import tqdm
import time
EARLY_STOPPING, CURRENT_CHECKPOINT, CURRENT_ACC, EPOCH = 3, 0, 0, 0
while True:
lasttime = time.time()
if CURRENT_CHECKPOINT == EARLY_STOPPING:
print('break epoch:%d\n' % (EPOCH))
break
train_acc, train_loss, test_acc, test_loss = 0, 0, 0, 0
pbar = tqdm(
range(0, len(train_X), batch_size), desc = 'train minibatch loop'
)
for i in pbar:
batch_x = train_X[i : min(i + batch_size, train_X.shape[0])]
batch_y = train_Y[i : min(i + batch_size, train_X.shape[0])]
batch_x_expand = np.expand_dims(batch_x,axis = 1)
acc, cost, _ = sess.run(
[model.accuracy, model.cost, model.optimizer],
feed_dict = {
model.Y: batch_y,
model.X: batch_x
},
)
assert not np.isnan(cost)
train_loss += cost
train_acc += acc
pbar.set_postfix(cost = cost, accuracy = acc)
pbar = tqdm(range(0, len(test_X), batch_size), desc = 'test minibatch loop')
for i in pbar:
batch_x = test_X[i : min(i + batch_size, test_X.shape[0])]
batch_y = test_Y[i : min(i + batch_size, test_X.shape[0])]
batch_x_expand = np.expand_dims(batch_x,axis = 1)
acc, cost = sess.run(
[model.accuracy, model.cost],
feed_dict = {
model.Y: batch_y,
model.X: batch_x
},
)
test_loss += cost
test_acc += acc
pbar.set_postfix(cost = cost, accuracy = acc)
train_loss /= len(train_X) / batch_size
train_acc /= len(train_X) / batch_size
test_loss /= len(test_X) / batch_size
test_acc /= len(test_X) / batch_size
if test_acc > CURRENT_ACC:
print(
'epoch: %d, pass acc: %f, current acc: %f'
% (EPOCH, CURRENT_ACC, test_acc)
)
CURRENT_ACC = test_acc
CURRENT_CHECKPOINT = 0
else:
CURRENT_CHECKPOINT += 1
print('time taken:', time.time() - lasttime)
print(
'epoch: %d, training loss: %f, training acc: %f, valid loss: %f, valid acc: %f\n'
% (EPOCH, train_loss, train_acc, test_loss, test_acc)
)
EPOCH += 1
# In[ ]:
real_Y, predict_Y = [], []
pbar = tqdm(
range(0, len(test_X), batch_size), desc = 'validation minibatch loop'
)
for i in pbar:
batch_x = test_X[i : min(i + batch_size, test_X.shape[0])]
batch_y = test_Y[i : min(i + batch_size, test_X.shape[0])]
predict_Y += np.argmax(
sess.run(
model.logits, feed_dict = {model.X: batch_x, model.Y: batch_y}
),
1,
).tolist()
real_Y += batch_y
# In[ ]:
print(metrics.classification_report(real_Y, predict_Y, target_names = trainset.target_names))