-
Notifications
You must be signed in to change notification settings - Fork 4
/
model.py
286 lines (232 loc) · 9.91 KB
/
model.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import os
import numpy as np
import tensorflow as tf
import math
from utils import Log, pad_sequences
import config
seed = 13
np.random.seed(seed)
class CnnModel:
def __init__(self, embeddings):
self.embeddings = embeddings
self.w2v_dim = config.W2V_DIM
self.max_length = config.MAX_LENGTH
self.cnn_config = config.CNN_CONFIG
self.hidden_layers = config.HIDDEN_LAYERS
self.all_labels = config.ALL_LABELS
self.num_of_class = len(config.ALL_LABELS)
self.loaded = None
self.session = tf.Session()
def restore_session(self, model_name):
if self.loaded != model_name:
saver = tf.train.Saver()
saver.restore(self.session, 'data/trained_model/{}'.format(model_name))
self.loaded = model_name
def save_session(self, model_name):
if not os.path.exists('data/trained_model/'):
os.makedirs('data/trained_model/')
saver = tf.train.Saver()
saver.save(self.session, 'data/trained_model/{}'.format(model_name))
def _add_placeholders(self):
"""
Adds placeholders to self
"""
self.word_ids = tf.placeholder(name='word_ids', shape=[None, None], dtype=tf.int32)
self.labels = tf.placeholder(name='labels', shape=[None], dtype=tf.int32)
self.dropout_embedding = tf.placeholder(dtype=tf.float32, shape=[], name="dropout_embedding")
self.dropout_cnn = tf.placeholder(dtype=tf.float32, shape=[], name='dropout_cnn')
self.dropout_hidden = tf.placeholder(dtype=tf.float32, shape=[], name='dropout_hidden')
self.is_training = tf.placeholder(tf.bool, name='phase')
def _add_word_embeddings_op(self):
"""
Adds word embeddings to self
"""
with tf.variable_scope("embedding"):
_embeddings = tf.Variable(self.embeddings, name="lut", dtype=tf.float32, trainable=False)
self.embeddings = tf.nn.embedding_lookup(
_embeddings, self.word_ids,
name="embeddings"
)
self.embeddings = tf.nn.dropout(self.embeddings, self.dropout_embedding)
self.embedding_dim = self.w2v_dim
def _add_logits_op(self):
"""
Adds logits to self
"""
with tf.variable_scope('cnn'):
cnn_input = tf.expand_dims(self.embeddings, -1)
cnn_outputs = []
for k in self.cnn_config:
with tf.variable_scope('cnn-{}'.format(k)):
filters = self.cnn_config[k]
height = k
pad_top = math.floor((k - 1) / 2)
pad_bottom = math.ceil((k - 1) / 2)
cnn_input = tf.pad(cnn_input, [[0, 0], [pad_top, pad_bottom], [0, 0], [0, 0]])
cnn_op = tf.layers.conv2d(
cnn_input, filters=filters,
kernel_size=(height, self.embedding_dim),
padding='valid', name='cnn-{}'.format(k),
kernel_initializer=tf.contrib.layers.xavier_initializer(),
kernel_regularizer=tf.contrib.layers.l2_regularizer(1e-4),
activation=tf.nn.relu,
)
cnn_outputs.append(tf.reduce_max(cnn_op, axis=[1, 2]))
cnn_output = tf.concat(cnn_outputs, axis=-1)
cnn_output = tf.nn.dropout(cnn_output, self.dropout_cnn)
with tf.variable_scope('logit'):
output = cnn_output
for i, v in enumerate(self.hidden_layers, start=1):
output = tf.layers.dense(
inputs=output, units=v, name='hidden_{}'.format(i),
kernel_initializer=tf.contrib.layers.xavier_initializer(),
kernel_regularizer=tf.contrib.layers.l2_regularizer(1e-4),
activation=tf.nn.tanh,
)
output = tf.nn.dropout(output, self.dropout_hidden)
self.logits = tf.layers.dense(
inputs=output, units=self.num_of_class, name='final_dense',
kernel_initializer=tf.contrib.layers.xavier_initializer(),
kernel_regularizer=tf.contrib.layers.l2_regularizer(1e-4)
)
self.pred_class = tf.cast(tf.argmax(self.logits, axis=-1), tf.int32)
self.pred_prop = tf.nn.softmax(self.logits)
def _add_loss_op(self):
"""
Adds loss to self
"""
with tf.variable_scope('loss_layers'):
losses = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.logits, labels=self.labels)
self.loss = tf.reduce_mean(losses)
regularizer = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
self.loss += tf.reduce_sum(regularizer)
def _add_train_op(self):
"""
Add train_op to self
"""
with tf.variable_scope("train_step"):
tvars = tf.trainable_variables()
grad, _ = tf.clip_by_global_norm(tf.gradients(self.loss, tvars), 100.0)
optimizer = tf.train.AdamOptimizer(learning_rate=1e-3)
self.train_op = optimizer.apply_gradients(zip(grad, tvars))
def build(self):
self._add_placeholders()
self._add_word_embeddings_op()
self._add_logits_op()
self._add_loss_op()
self._add_train_op()
# f = tf.summary.FileWriter("tensorboard")
# f.add_graph(tf.get_default_graph())
# f.close()
# exit(0)
def _loss(self, feed_dict):
feed_dict = feed_dict
feed_dict[self.dropout_embedding] = 1.0
feed_dict[self.dropout_cnn] = 1.0
feed_dict[self.dropout_hidden] = 1.0
feed_dict[self.is_training] = False
loss = self.session.run(self.loss, feed_dict=feed_dict)
return loss
def _next_batch(self, dataset, batch_size):
"""
:param dataset.Dataset dataset:
:return:
"""
start = 0
while start < len(dataset.words):
w_batch = dataset.words[start:start + batch_size]
word_ids, _ = pad_sequences(w_batch, pad_tok=0, max_sent_length=self.max_length)
if dataset.labels is not None:
labels = dataset.labels[start:start + batch_size]
else:
labels = None
start += batch_size
yield {
self.word_ids: word_ids,
self.labels: labels
} if labels is not None else {
self.word_ids: word_ids
}
def train(self, model_name, train, validation=None, epochs=1000, batch_size=128, early_stopping=True, patience=10, verbose=True, cont=None):
"""
:param cont:
:param model_name:
:param verbose:
:param patience:
:param early_stopping:
:param batch_size:
:param epochs:
:param dataset.Dataset train:
:param dataset.Dataset validation:
:return:
"""
print('Number of training examples:', len(train.labels))
if validation is not None:
print('Number of validation examples:', len(validation.labels))
elif early_stopping:
raise ValueError('Specify validation dataset to use early stopping')
if cont is not None:
self.restore_session(cont)
else:
self.session.run(tf.global_variables_initializer())
Log.verbose = verbose
best_loss = float('inf')
nepoch_noimp = 0
for e in range(epochs):
train.shuffle_data()
for idx, batch_data in enumerate(self._next_batch(dataset=train, batch_size=batch_size)):
feed_dict = {
**batch_data,
self.dropout_embedding: 0.5,
self.dropout_cnn: 0.5,
self.dropout_hidden: 0.5,
self.is_training: True,
}
_, loss_train = self.session.run([self.train_op, self.loss], feed_dict=feed_dict)
if idx % 5 == 0:
Log.log("Iter {}, Loss: {} ".format(idx, loss_train))
Log.log("End epochs {}".format(e + 1))
# stop by loss
if early_stopping:
total_loss = []
for batch_data in self._next_batch(dataset=validation, batch_size=batch_size):
loss = self._loss(feed_dict=batch_data)
total_loss.append(loss)
val_loss = np.mean(total_loss)
Log.log('Val loss: {}'.format(val_loss))
if val_loss < best_loss:
self.save_session(model_name)
Log.log('Save the model at epoch {}'.format(e + 1))
best_loss = val_loss
nepoch_noimp = 0
else:
nepoch_noimp += 1
Log.log("Number of epochs with no improvement: {}".format(nepoch_noimp))
if nepoch_noimp >= patience:
Log.log('Best loss: {}'.format(best_loss))
break
if not early_stopping:
self.save_session(model_name)
def predict(self, test, model_name, batch_size=128, pred_class=True):
"""
:param batch_size:
:param model_name:
:param dataset.Dataset test:
:return:
"""
self.restore_session(model_name)
y_pred = []
for batch_data in self._next_batch(dataset=test, batch_size=batch_size):
feed_dict = {
**batch_data,
self.dropout_embedding: 1.0,
self.dropout_cnn: 1.0,
self.dropout_hidden: 1.0,
self.is_training: False,
}
if pred_class:
preds = self.session.run(self.pred_class, feed_dict=feed_dict)
else:
preds = self.session.run(self.pred_prop, feed_dict=feed_dict)
y_pred.extend(preds)
return y_pred