-
Notifications
You must be signed in to change notification settings - Fork 1
/
training.py
262 lines (223 loc) · 9.6 KB
/
training.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
import numpy as np
import re
import csv
from tensorflow import keras as tfkeras
from keras.models import Model
from keras.layers import Input, Dense, Concatenate
from keras.layers import Convolution1D
from keras.layers import GlobalMaxPooling1D
from keras.layers import Embedding
from keras.layers import AlphaDropout
from keras.callbacks import TensorBoard
class CharCNNKim(object):
"""
Class to implement the Character Level Convolutional Neural Network
as described in Kim et al., 2015 (https://arxiv.org/abs/1508.06615)
Their model has been adapted to perform text classification instead of
language modelling by replacing subsequent recurrent layers with dense
layer(s) to perform softmax over classes.
"""
def __init__(self, input_size, alphabet_size, embedding_size,
conv_layers, fully_connected_layers,
num_of_classes, dropout_p,
optimizer='adam', loss='categorical_crossentropy'):
"""
Initialization for the Character Level CNN model.
Args:
input_size (int): Size of input features
alphabet_size (int): Size of alphabets to create embeddings for
embedding_size (int): Size of embeddings
conv_layers (list[list[int]]): List of Convolution layers for model
fully_connected_layers (list[list[int]]): List of Fully Connected layers for model
num_of_classes (int): Number of classes in data
dropout_p (float): Dropout Probability
optimizer (str): Training optimizer
loss (str): Loss function
"""
self.input_size = input_size
self.alphabet_size = alphabet_size
self.embedding_size = embedding_size
self.conv_layers = conv_layers
self.fully_connected_layers = fully_connected_layers
self.num_of_classes = num_of_classes
self.dropout_p = dropout_p
self.optimizer = optimizer
self.loss = loss
self._build_model() # builds self.model variable
def _build_model(self):
"""
Build and compile the Character Level CNN model
Returns: None
"""
# Input layer
inputs = Input(shape=(self.input_size,), name='sent_input', dtype='int64')
# Embedding layers
x = Embedding(self.alphabet_size + 1, self.embedding_size, input_length=self.input_size)(inputs)
# Convolution layers
convolution_output = []
for num_filters, filter_width in self.conv_layers:
conv = Convolution1D(filters=num_filters,
kernel_size=filter_width,
activation='tanh',
name='Conv1D_{}_{}'.format(num_filters, filter_width))(x)
pool = GlobalMaxPooling1D(name='MaxPoolingOverTime_{}_{}'.format(num_filters, filter_width))(conv)
convolution_output.append(pool)
x = Concatenate()(convolution_output)
# Fully connected layers
for fl in self.fully_connected_layers:
x = Dense(fl, activation='selu', kernel_initializer='lecun_normal')(x)
x = AlphaDropout(self.dropout_p)(x)
# Output layer
predictions = Dense(self.num_of_classes, activation='softmax')(x)
# Build and compile model
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer=self.optimizer, loss=self.loss,
metrics=["accuracy"])
self.model = model
print("CharCNNKim model built: ")
self.model.summary()
def train(self, training_inputs, training_labels,
validation_inputs, validation_labels,
epochs, batch_size, checkpoint_every=100):
"""
Training function
Args:
training_inputs (numpy.ndarray): Training set inputs
training_labels (numpy.ndarray): Training set labels
validation_inputs (numpy.ndarray): Validation set inputs
validation_labels (numpy.ndarray): Validation set labels
epochs (int): Number of training epochs
batch_size (int): Batch size
checkpoint_every (int): Interval for logging to Tensorboard
Returns: None
"""
# Create callbacks
tensorboard = TensorBoard(log_dir='logs', histogram_freq=checkpoint_every, batch_size=batch_size,
write_graph=False, write_grads=True, write_images=False,
embeddings_freq=checkpoint_every,
embeddings_layer_names=None)
# Start training
print("Training CharCNNKim model: ")
self.model.fit(training_inputs, training_labels,
validation_data=(validation_inputs, validation_labels),
epochs=epochs,
batch_size=batch_size,
verbose=2,
callbacks=[tensorboard])
def test(self, testing_inputs, testing_labels, batch_size):
"""
Testing function
Args:
testing_inputs (numpy.ndarray): Testing set inputs
testing_labels (numpy.ndarray): Testing set labels
batch_size (int): Batch size
Returns: None
"""
# Evaluate inputs
res = self.model.evaluate(testing_inputs, testing_labels, batch_size=batch_size, verbose=1)
print(res)
class Data(object):
"""
Class to handle loading and processing of raw datasets.
"""
def __init__(self, data_source, alphabet,
input_size=256, num_of_classes=2):
"""
Initialization of a Data object.
Args:
data_source (str): Raw data file path
alphabet (str): Alphabet of characters to index
input_size (int): Size of input features
num_of_classes (int): Number of classes in data
"""
self.alphabet = alphabet
self.alphabet_size = len(self.alphabet)
self.dict = {} # Maps each character to an integer
self.no_of_classes = num_of_classes
self.dict['UNK'] = 0
for idx, char in enumerate(self.alphabet):
self.dict[char] = idx + 1
self.length = input_size
self.data_source = data_source
def load_data(self):
"""
Load raw data from the source file into data variable.
Returns: None
"""
data = []
with open(self.data_source, 'r', encoding='utf-8') as f:
rdr = csv.reader(f, delimiter=',', quotechar='"')
for row in rdr:
txt = ""
for s in row[1:]:
txt = txt + " " + re.sub("^\s*(.-)\s*$", "%1", s).replace("\\n", "\n")
data.append((int(row[0]), txt)) # format: (label, text)
self.data = np.array(data)[:1000]
print("Data loaded from " + self.data_source)
def get_all_data(self):
"""
Return all loaded data from data variable.
Returns:
(np.ndarray) Data transformed from raw to indexed form with associated one-hot label.
"""
data_size = len(self.data)
start_index = 0
end_index = data_size
batch_texts = self.data[start_index:end_index]
batch_indices = []
one_hot = np.eye(self.no_of_classes, dtype='int64')
classes = []
for c, s in batch_texts:
batch_indices.append(self.str_to_indexes(s))
c = int(c) - 1
classes.append(one_hot[c])
return np.asarray(batch_indices, dtype='int64'), np.asarray(classes)
def str_to_indexes(self, s):
"""
Convert a string to character indexes based on character dictionary.
Args:
s (str): String to be converted to indexes
Returns:
str2idx (np.ndarray): Indexes of characters in s
"""
s = s.lower()
max_length = min(len(s), self.length)
str2idx = np.zeros(self.length, dtype='int64')
for i in range(1, max_length + 1):
c = s[-i]
if c in self.dict:
str2idx[i - 1] = self.dict[c]
# else, 'UNK' return str2idx all elements are zero
return str2idx
def main():
alphabet = "abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:'\"/\\|_@#$%^&*~`+-=<>()[]{}"
training_data = Data("train.csv", alphabet, 256, 2)
training_data.load_data()
training_inputs, training_labels = training_data.get_all_data()
validation_data = Data("validate.csv", alphabet, 256, 2)
validation_data.load_data()
validation_inputs, validation_labels = validation_data.get_all_data()
test_data = Data("", alphabet, 256, 2)
test_data.data = np.array([[0, "nortel.com"]])
test_inputs, _ = test_data.get_all_data()
model = CharCNNKim(input_size=256,
alphabet_size=len(alphabet),
embedding_size=128,
conv_layers=[[256, 10], [256, 7], [256, 5], [256, 3]],
fully_connected_layers=[1024, 1024],
num_of_classes=2,
dropout_p=0.1,
optimizer="adam",
loss="categorical_crossentropy")
model.train(training_inputs=training_inputs,
training_labels=training_labels,
validation_inputs=validation_inputs,
validation_labels=validation_labels,
epochs=1,
batch_size=128,
checkpoint_every=5)
model.model.save(".dnscnn.h5")
dnscnn = tfkeras.models.load_model(".dnscnn.h5")
dnscnn.save("dnscnn_saved_model/0001", save_format="tf")
if __name__ == "__main__":
main()