-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathselection_tools.py
More file actions
215 lines (177 loc) · 7.77 KB
/
selection_tools.py
File metadata and controls
215 lines (177 loc) · 7.77 KB
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
import pandas as pd
from gensim.models import Word2Vec
import re
from nltk.tokenize import word_tokenize
from keras.preprocessing.sequence import pad_sequences
import keras
import numpy as np
from statics import *
import sys
sys.path.append('..')
intent_dic = {"PlayMusic": 0, "AddToPlaylist": 1, "RateBook": 2, "SearchScreeningEvent": 3,
"BookRestaurant": 4, "GetWeather": 5, "SearchCreativeWork": 6}
def process_snips_data(data_path, w2v_path):
data = pd.read_csv(data_path)
w2v_model = Word2Vec.load(w2v_path)
sentences_ = list(data["text"])
intent_ = list(data["intent"])
intent = [intent_dic[i] for i in intent_]
sentences = []
for s in sentences_:
clean = re.sub(r'[^ a-z A-Z 0-9]', " ", s)
w = word_tokenize(clean)
# stemming
sentences.append([i.lower() for i in w])
# 取得所有单词
vocab_list = list(w2v_model.wv.vocab.keys())
# 每个词语对应的索引
word_index = {word: index for index, word in enumerate(vocab_list)}
# 序列化
def get_index(sentence):
sequence = []
for word in sentence:
try:
sequence.append(word_index[word])
except KeyError:
pass
return sequence
X_data = list(map(get_index, sentences))
maxlen = 16 # 截长补短
X_pad = pad_sequences(X_data, maxlen=maxlen)
Y = keras.utils.to_categorical(intent, num_classes=7)
return X_pad, Y
def process_agnews_data(data_path, w2v_path):
data = pd.read_csv(data_path)
w2v_model = Word2Vec.load(w2v_path)
sentences_ = list(data["news"])
intent_ = list(data["label"])
intent = [i-1 for i in intent_]
sentences = []
for s in sentences_:
clean = re.sub(r'[^ a-z A-Z 0-9]', " ", s)
w = word_tokenize(clean)
# stemming
sentences.append([i.lower() for i in w])
# 取得所有单词
vocab_list = list(w2v_model.wv.vocab.keys())
# 每个词语对应的索引
word_index = {word: index for index, word in enumerate(vocab_list)}
# 序列化
def get_index(sentence):
sequence = []
for word in sentence:
try:
sequence.append(word_index[word])
except KeyError:
pass
return sequence
X_data = list(map(get_index, sentences))
maxlen = 35 # 截长补短
X_pad = pad_sequences(X_data, maxlen=maxlen)
Y = keras.utils.to_categorical(intent, num_classes=4)
return X_pad, Y
def get_selection_information(file_path, model, lstm_classifier, dense_model, wrapper_path, w2v_path, time_steps):
if file_path.split(".")[-1] == "npz":
with np.load(file_path, allow_pickle=True) as f:
X, Y = f['X'], f['Y']
if file_path.split(".")[-1] == "csv" and "snips" in file_path.split(".")[-2]:
X, Y = process_snips_data(file_path, w2v_path)
elif file_path.split(".")[-1] == "csv" and "agnews" in file_path.split(".")[-2]:
X, Y = process_agnews_data(file_path, w2v_path)
weight_state, stellar_bscov, stellar_btcov, rnntest_sc, rnntest_sc_cam, nc_cov, nc_cam = [], [], [], [], [], [], []
right, hscov_max_index, trend_set = [], [], []
act_set = set()
act_time_set = set()
for idx, (x, y) in enumerate(zip(X, Y)):
if file_path.split(".")[-1] == "npz":
x_test = mnist_input_preprocess(np.array([x]))
y_test = keras.utils.to_categorical(np.array([y]), num_classes=10)
else:
x_test = np.array([x])
y_test = y
classify_out_list, plus_sum, minus_sum = [], [], []
lstm_out = model.predict(x_test)[1]
# hs_cov
hscov_max_index.append(np.argmax(lstm_out[0])) # The index of the lstm_out matrix activated by this use case
# nc_cov
act = get_nc_activate(lstm_out)
nc = len(act) / lstm_out[0].size if len(act) != 0 else 0
nc_cov.append(nc)
diff = act - act_set
if not diff: # True represents an empty set, i.e., there is no new act
nc_cam.append(0)
else:
act_set = act_set.union(act)
nc_cam.append(1)
for i in range(time_steps):
lstm_t = lstm_out[0][i]
plus_sum.append(sum([lstm_ti for lstm_ti in lstm_t if lstm_ti > 0]))
minus_sum.append(sum([lstm_ti for lstm_ti in lstm_t if lstm_ti < 0]))
dense_array = lambda x: x[:, i, :]
tmp = dense_model.predict(np.array(dense_array(lstm_out)))
confident = np.max(tmp)
if confident >= 0.5 and i != (time_steps - 1):
classify_out_list.append(np.argmax(tmp))
elif i == (time_steps - 1):
classify_out_list.append(np.argmax(tmp))
trend_set.append(get_change_set(classify_out_list))
weight_state.append(cacl_change_rate_with_weights(classify_out_list))
# check the predict result is right or wrong
check_predict_result(int(classify_out_list[-1]), int(np.argmax(y_test)), right)
# Stellar Coverage
BSCov, BTCov = get_stellar_cov(lstm_classifier, model, x, wrapper_path)
stellar_bscov.append(BSCov)
stellar_btcov.append(BTCov)
# rnnTest Coverage
SC, acted_time = get_testrnn_sc(plus_sum, minus_sum)
rnntest_sc.append(SC)
diff_acted_time = acted_time - act_time_set
if not diff_acted_time: # True represents an empty set, i.e., there is no new act
rnntest_sc_cam.append(0)
else:
act_time_set = act_time_set.union(acted_time)
rnntest_sc_cam.append(1)
# hs_cov
unique_index_arr, unique_index_arr_id = np.unique(hscov_max_index, return_index=True)
return weight_state, unique_index_arr_id, np.array(stellar_bscov), np.array(stellar_btcov), np.array(rnntest_sc), \
np.array(nc_cov), np.array(nc_cam), np.array(rnntest_sc_cam), trend_set, right
def get_selected_data(file_path, selected_li, w2v_path):
selected_id = np.where(selected_li == 1)[0]
X_selected, Y_selected = [], []
if file_path.split(".")[-1] == "npz":
with np.load(file_path, allow_pickle=True) as f:
X, Y = f['X'], f['Y']
for idx in selected_id:
X_selected.append(X[idx][0])
Y_selected.append(Y[idx])
elif file_path.split(".")[-1] == "csv" and "snips" in file_path.split(".")[-2]:
X, Y = process_snips_data(file_path, w2v_path)
for idx in selected_id:
X_selected.append(np.array(X[idx]))
Y_selected.append(Y[idx])
elif file_path.split(".")[-1] == "csv" and "agnews" in file_path.split(".")[-2]:
X, Y = process_agnews_data(file_path, w2v_path)
for idx in selected_id:
X_selected.append(np.array(X[idx]))
Y_selected.append(Y[idx])
X_selected_array = np.array(X_selected)
Y_selected_array = np.array(Y_selected)
return X_selected_array, Y_selected_array
def get_val_data(file_path, w2v_path):
X_val, Y_val = [], []
if file_path.split(".")[-1] == "npz":
with np.load(file_path, allow_pickle=True) as f:
X, Y = f['X'], f['Y']
for x in X:
X_val.append(x[0])
return np.array(X_val), Y
elif file_path.split(".")[-1] == "csv" and "snips" in file_path.split(".")[-2]:
X, Y = process_snips_data(file_path, w2v_path)
for x in X:
X_val.append(x)
return np.array(X_val), Y
elif file_path.split(".")[-1] == "csv" and "agnews" in file_path.split(".")[-2]:
X, Y = process_agnews_data(file_path, w2v_path)
for x in X:
X_val.append(x)
return np.array(X_val), Y