-
Notifications
You must be signed in to change notification settings - Fork 0
/
kproblog_qc.py
268 lines (224 loc) · 8.93 KB
/
kproblog_qc.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
from collections import defaultdict
from joblib import Parallel, delayed
from kproblog import Term, KProbLog, symbols
from kproblog.core import term_to_string
from kproblog.experiments.common import feature_extraction, model_selection, split_in_blocks, VLABEL, ELABEL
from kproblog.experiments.common import model_selection, extract_subset_parallel, SparseFeatures
from kproblog.experiments.qc_io import one_pkl2dataset, Word2WL_Type
from kproblog.semirings import VoidMonoid, PolynomialSemiring, SetMonoid, PolynomialSemiring, MultisetMonoidOfMonoids, Meta, ShortestPathSemiring
from kproblog.semirings.utils import dict2symbol, sv_add, Obj2Id, nhash_m19, nhash_m13
from kproblog.utils import TimeContext
from sklearn.metrics import accuracy_score
import functools, itertools, glob
import numpy as np
import scipy.sparse as sp
import signal
N_JOBS = 1
word2wl_type = Word2WL_Type()
token_labels, dep_rel = symbols('token_labels, dep_rel')
bow_features, feature_blocks, final_features = symbols('bow_features, feature_blocks, final_features')
dep_rel_edge, spath = symbols('dep_rel_edge, spath')
v2labels, = symbols('v2labels')
config, = symbols('config')
# VARIABLES
V, U, W = symbols('V, U, W')
# CONSTANTS
word, pos, lemma = symbols('word, pos, lemma')
meta, = symbols('meta')
kproblog = KProbLog()
kproblog.declare_destructive_many({
PolynomialSemiring(): [
final_features/0,
dep_rel/2
],
MultisetMonoidOfMonoids(PolynomialSemiring()):[
token_labels/1,
bow_features/0,
feature_blocks/0,
],
MultisetMonoidOfMonoids(MultisetMonoidOfMonoids(PolynomialSemiring())):[
v2labels/0
],
Meta(): [
meta/0
],
SetMonoid():[
config/0
]
})
kproblog.declare_additive_many({
ShortestPathSemiring(): [
spath/2,
dep_rel_edge/2,
]
})
# VERTEX TO LABELS
@kproblog
def vertex_to_labels_proc(label_dict:token_labels(V), info:meta) -> v2labels:
v, = info['label_dict'].args
return {v:label_dict}
# BOW FEATURES
@kproblog
def bow_feat_block_proc(label_dict:token_labels(V), config_set:config) -> bow_features:
block2feats = {}
for (feat_type, params), in config_set:
if feat_type != 'bow': continue
bow_label_type, = params
block2feats['bow', bow_label_type] = label_dict[bow_label_type]
return block2feats
# SHORTEST PATHS ON DEPENDENCY RELATIONS
@kproblog
def cast_to_shortest_path(edge_value:dep_rel(V, W), info:meta) -> dep_rel_edge(V, W):
v, w = info['edge_value'].args
edge_label = dict2symbol(edge_value)
return ShortestPathSemiring.create_from_edge_value(v, w, edge_label=edge_label) # XXX EDGE LABEL HERE
@kproblog
def sp_proc0(edge_value:dep_rel_edge(V, W), info:meta) -> spath(V, W):
return edge_value
@kproblog
def sp_proc1(edge_value:dep_rel_edge(V, U), sp_value:spath(U, W), _:V != W) -> spath(V, W):
return edge_value * sp_value
# FEATURE BLOCKS
@kproblog # BAG OF WORD BLOCKS
def bow_feature_block_proc(feat_block:bow_features) -> feature_blocks:
return feat_block
@kproblog # SHORTEST PATH BLOCKS
def sp_features_block_proc(spath_value:spath(V, W), v2labels_dict:v2labels, config_set:config) -> feature_blocks:
block2phi = defaultdict(lambda:defaultdict(float))
for (block_type, params), in config_set:
if block_type != 'sp': continue
use_dep_labels, label_type = params
for path in spath_value.paths:
path_len, key = label_path_helper(path, v2labels_dict, use_dep_labels, label_type)
block2phi['sp', use_dep_labels, label_type, path_len][path_len, key] += 1.
return block2phi
def label_path_helper(path, v2labels_dict, use_dep_labels, label_type):
if label_type != '_': # decorate the path with vertex labels
v_labels = tuple(dict2symbol(v2labels_dict[v].get(label_type, '_')) for v in path[::2])
else: # label_type == '_': # no vertex label info
v_labels = ()
if use_dep_labels: # decorate the path with edge labels
e_labels = path[1::2]
else:
e_labels = () # no edge label info
path_len = len(path[1::2]); assert path_len > 0
key = v_labels + e_labels
return path_len, key
@kproblog # FINAL FEATURES
def final_features_proc(feat_dict:feature_blocks) -> final_features:
acc = {}
for _, feats in feat_dict.items():
acc = sv_add(acc, feats)
return PolynomialSemiring.rehash(acc, nhash_m19)
def qc2facts(sentence_pkl_file_name):
coarse_label, _fine_label, graph_list = one_pkl2dataset(sentence_pkl_file_name)
y_i = coarse_label
data = defaultdict(float)
for graph in graph_list:
for v, attr in graph.nodes(data=True):
v = int(v)
data[token_labels(v)] = {
label_name: {label:1.}
for label_name, label in attr.items()
if label_name != 'vector' and label_name != 'wl'
}
word = attr['word']
wl_key = word2wl_type.get(word, '_')
data[token_labels(v)]['wl'] = {wl_key:1.}
for v, w, attr in graph.edges(data=True):
v, w = map(int, [v, w])
dep_rel_label, = symbols(attr['label'])
data[dep_rel(v, w)] = {dep_rel_label:1.}
return y_i, data
# GENERATE CONFIGURATIONS
def bow_config_gen(label_types):
for i in range(0, len(label_types)+1):
for label_types_subset in itertools.combinations(label_types, i):
yield [('bow', (lt,)) for lt in label_types_subset]
def sp_config_gen(label_types):
for i in range(0, len(label_types)+1):
for label_types_subset in itertools.combinations(label_types, i):
yield [('sp', (lt != 'lemma' and lt != 'word', lt)) for lt in label_types_subset]
def config_gen():
label_types = ['word', 'pos', 'lemma']
for bow, sp in itertools.product(bow_config_gen(label_types), sp_config_gen(label_types + ['_'])):
t = bow + sp
if t:
yield t
def timeout_handler(signum, frame):
raise Exception("end of time")
def parametric_extraction(configuration_set):
with TimeContext("EXTRACTION"):
train_pkl_file_name_list = glob.glob('data/qc/pkl_graphs/train/*.pkl')
test_pkl_file_name_list = glob.glob('data/qc/pkl_graphs/test/*.pkl')
train_y_list, train_feat_list = extract_subset_parallel(
kproblog,
query=final_features,
dataset2facts_hook=qc2facts,
configuration_set=configuration_set,
examples_list=train_pkl_file_name_list,
subset_size=350,
n_jobs=N_JOBS
)
test_y_list, test_feat_list = extract_subset_parallel(
kproblog,
query=final_features,
dataset2facts_hook=qc2facts,
configuration_set=configuration_set,
examples_list=test_pkl_file_name_list,
subset_size=35,
n_jobs=N_JOBS
)
obj2id = Obj2Id()
train_feat_list = [PolynomialSemiring.rehash(feat, obj2id) for feat in train_feat_list]
test_feat_list = [PolynomialSemiring.rehash(feat, obj2id) for feat in test_feat_list]
train_sparse_features = SparseFeatures.from_y_and_feats(train_y_list, train_feat_list, max_feature=np.inf)
y_train, X_train = train_sparse_features.get_yX()
max_feature = X_train.shape[1]
test_sparse_features = SparseFeatures.from_y_and_feats(test_y_list, test_feat_list, max_feature=max_feature)
y_test, X_test = test_sparse_features.get_yX(max_feature)
CLASSIFIER_SEED = 123
KFOLD_SEED = 124
# C_list = np.logspace(0, 3, 4)
# C_list = np.logspace(2, 5, 4)
C_list = [10000.]
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(60) # 1 minute
try:
clf = model_selection(
C_list, X_train, y_train,
params = dict(
dual=False,
multi_class='ovr',
random_state=CLASSIFIER_SEED,
),
kfold_seed=KFOLD_SEED,
verbose_flag=True
)
clf.fit(X_train, y_train)
y_pred_train = clf.predict(X_train)
y_pred_test = clf.predict(X_test)
acc_train = accuracy_score(y_train, y_pred_train)
acc_test = accuracy_score(y_test, y_pred_test)
print("FINAL LEARNING train acc: {:.1f}% acc test: {:.1f}%".format(acc_train*100., acc_test*100.))
except Exception as msg:
print("FINAL TIMEOUT", msg)
def main():
PLOT_FLAG = True
if PLOT_FLAG:
for configuration_set in config_gen():
print('CONFIGURATION_SET', configuration_set)
parametric_extraction(configuration_set)
else:
configuration_set = {
('bow', ('word',)),
('bow', ('lemma',)),
('bow', ('pos',)),
('sp', (True, 'wl')),
('sp', (True, 'pos')),
('sp', (False, 'lemma')),
('sp', (True, '_'))
}
parametric_extraction(configuration_set)
if __name__ == '__main__':
main()