-
Notifications
You must be signed in to change notification settings - Fork 0
/
mc1.py
206 lines (161 loc) · 5.29 KB
/
mc1.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
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 18 12:46:27 2020
@author: damian
TODO:
Classes:
1. "Completeness"
2. "Consistency"
3. "Conformity"
4. "Accuracy"
5. "Integrity"
6. "Timeliness"
"""
import numpy as np
import pandas as pd
import os
import tensorflow as tf
import utils
CLASSES = [
"completeness",
"consistency",
"conformity",
"accuracy",
"integrity",
"timeliness",
]
DATA_FOLDER = 'data'
def load_train_data(embeds, dct_n2i, seq_size=500):
data = []
labels = []
for label, class_name in enumerate(CLASSES):
data_folder = os.path.join(DATA_FOLDER, class_name)
files = os.listdir(data_folder)
for file in files:
fn = os.path.join(data_folder, file)
with open(fn, 'rt', encoding="utf-8") as fh:
text = fh.read()
obs = utils.tokenize_and_embeds(text, embeds, dct_n2i, max_size=seq_size)
data.append(obs)
labels.append(label)
return np.array(data), np.array(labels)
def decode(obs, embeds, dct_i2n):
zeros = np.zeros(obs.shape[1])
valid = [x for x in obs if np.all(x != zeros)]
idxs = []
for embed in valid:
diff = np.abs((embeds - embed)).sum(axis=1)
idx = np.argmin(diff)
idxs.append(idx)
texts = [dct_i2n[i] for i in idxs]
txt = " ".join(texts)
return txt
def get_test_data(df, embeds, dct_n2i, seq_size=500):
texts = [x for x in df.Description]
data = [utils.tokenize_and_embeds(doc, embeds=embeds, dct_n2i=dct_n2i, max_size=seq_size)
for doc in texts]
return np.array(data)
def get_model(input_shape):
tf_input = tf.keras.layers.Input(input_shape)
tf_x = tf_input
tf_x1 = tf.keras.layers.Conv1D(256, 1, activation='relu')(tf_x)
tf_x2 = tf.keras.layers.Conv1D(256, 3, activation='relu')(tf_x)
tf_x3 = tf.keras.layers.Conv1D(256, 5, activation='relu')(tf_x)
tf_x1 = tf.keras.layers.LSTM(256)(tf_x1)
tf_x2 = tf.keras.layers.LSTM(256)(tf_x2)
tf_x3 = tf.keras.layers.LSTM(256)(tf_x3)
tf_x = tf.keras.layers.concatenate([tf_x1, tf_x2, tf_x3])
tf_x = tf.keras.layers.Dropout(0.7)(tf_x)
tf_x = tf.keras.layers.Dense(384, activation='relu')(tf_x)
tf_x = tf.keras.layers.Dropout(0.7)(tf_x)
tf_out = tf.keras.layers.Dense(len(CLASSES),
activation='softmax')(tf_x)
model = tf.keras.models.Model(tf_input, tf_out, name='MC')
model.compile(
loss='sparse_categorical_crossentropy',
optimizer='nadam',
metrics=['acc']
)
return model
def save_data(df, log):
df_hl = df[~df.DQType.isna()]
log.P("Found {} labels".format(df_hl.shape[0]))
for i in range(df_hl.shape[0]):
subfolder = df_hl.iloc[i].DQType
file = df_hl.iloc[i].Key
fn = os.path.join(DATA_FOLDER, subfolder, file.lower() + '.txt')
txt = df_hl[df_hl.Key == file][['Description']].iloc[0,0]
with open(fn, 'wt', encoding="utf-8") as fh:
fh.write(txt)
return
if __name__ == '__main__':
SEQ_SIZE = 100
FULL_TRAIN = True
GLV_FILE = os.path.join(DATA_FOLDER, 'glove.6B.50d.txt')
EMBS_FILE = os.path.join(DATA_FOLDER, 'embs_voc.npz')
DATA_FILE = os.path.join(DATA_FOLDER, 'data3.xlsx')
log = utils.Log()
for c in CLASSES:
_dir = os.path.join(DATA_FOLDER, c)
if not os.path.isdir(_dir):
os.mkdir(_dir)
df_inp = pd.read_excel(DATA_FILE)
df = df_inp[~df_inp.Description.isna()]
save_data(df, log=log)
if 'np_embeds' not in globals():
if os.path.isfile(EMBS_FILE):
log.P("Loading GloVe word embeddings")
glove_words = os.path.join(EMBS_FILE)
data = np.load(glove_words)
np_vocab = data['arr_0']
np_embeds = data['arr_1']
dct_i2n = {x:np_vocab[x] for x in range(np_vocab.shape[0])}
dct_n2i = {np_vocab[x]:x for x in range(np_vocab.shape[0])}
else:
log.P("Generating word vectors...")
_d = utils.glove2dict(GLV_FILE)
word_list = list(_d.keys())
np_vocab = np.array(word_list)
np_embeds = np.array(
[_d[np_vocab[x]]
for x in range(np_vocab.shape[0])]
).astype(np.float32)
log.P("Word vectors generated.")
log.P("Saving word vectors...")
np.savez(EMBS_FILE, np_vocab, np_embeds)
log.P("Saved embeds.")
dct_i2n = {x:np_vocab[x] for x in range(np_vocab.shape[0])}
dct_n2i = {np_vocab[x]:x for x in range(np_vocab.shape[0])}
else:
log.P("np_embeds already loaded.")
def show_word(word):
return utils.show_neighbors(
idx=word,
embeds=np_embeds,
dct_i2n=dct_i2n,
log=log,
dct_n2i=dct_n2i)
for c in CLASSES:
show_word(c)
if FULL_TRAIN:
log.P("Prepare data...")
X, y = load_train_data(
embeds=np_embeds,
dct_n2i=dct_n2i,
seq_size=SEQ_SIZE)
log.P("Done prepare data.")
if False:
log.P(decode(X[0], np_embeds, dct_i2n))
model = get_model(X.shape[1:])
model.fit(X, y, epochs=100)
x_test = get_test_data(df, np_embeds, dct_n2i, seq_size=SEQ_SIZE)
yh = model.predict(x_test)
yp = yh.argmax(axis=1)
labels = [CLASSES[y] for y in yp]
procs = [round(p[x] * 100,2) for p, x in zip(yh, yp)]
df_res = pd.DataFrame({
'KEY' : df.Key,
'LABEL' : labels,
'PROBA' : procs
})
df_res.to_csv('results.csv', index=False)