prepare train data to embedded word vector sequence given sequence limit for testing return: questions_encoded: np ndarray, shape (number samples, seq length, vector size) poss_encoded: same layout, sequence for positive answer negs_encode
(train_raw,
label2answer,
idx2word,
q_seq_limit,
ans_seq_limit,
idx2vec,
top_k_candi_limit=6)
| 210 | |
| 211 | |
| 212 | def limit_encode_eval(train_raw, |
| 213 | label2answer, |
| 214 | idx2word, |
| 215 | q_seq_limit, |
| 216 | ans_seq_limit, |
| 217 | idx2vec, |
| 218 | top_k_candi_limit=6): |
| 219 | ''' prepare train data to embedded word vector sequence given sequence limit for testing |
| 220 | return: |
| 221 | questions_encoded: np ndarray, shape |
| 222 | (number samples, seq length, vector size) |
| 223 | poss_encoded: same layout, sequence for positive answer |
| 224 | negs_encoded: same layout, sequence for negative answer |
| 225 | ''' |
| 226 | questions = [question for question, answers, candis in train_raw] |
| 227 | |
| 228 | # combine truth and candidate answers label, |
| 229 | candi_pools = [ |
| 230 | list(answers + candis)[:top_k_candi_limit] |
| 231 | for question, answers, candis in train_raw |
| 232 | ] |
| 233 | assert all([len(pool) == top_k_candi_limit for pool in candi_pools]) |
| 234 | |
| 235 | ans_count = [len(answers) for question, answers, candis in train_raw] |
| 236 | assert all([c > 0 for c in ans_count]) |
| 237 | |
| 238 | # encode ans |
| 239 | candi_pools_encoded = [[label2answer[candi_label] |
| 240 | for candi_label in pool] |
| 241 | for pool in candi_pools] |
| 242 | |
| 243 | # filtered word not in idx2vec |
| 244 | questions_filtered = [ |
| 245 | [idx for idx in q if idx in idx2vec] for q in questions |
| 246 | ] |
| 247 | candi_pools_filtered = [[[idx |
| 248 | for idx in candi_encoded |
| 249 | if idx in idx2vec] |
| 250 | for candi_encoded in pool] |
| 251 | for pool in candi_pools_encoded] |
| 252 | |
| 253 | # crop to seq limit |
| 254 | questions_crop = [ |
| 255 | q[:q_seq_limit] + [0] * max(0, q_seq_limit - len(q)) |
| 256 | for q in questions_filtered |
| 257 | ] |
| 258 | candi_pools_crop = [[ |
| 259 | candi[:ans_seq_limit] + [0] * max(0, ans_seq_limit - len(candi)) |
| 260 | for candi in pool |
| 261 | ] |
| 262 | for pool in candi_pools_filtered] |
| 263 | |
| 264 | # encoded, word idx to word vector |
| 265 | questions_encoded = [[idx2vec[idx] for idx in q] for q in questions_crop] |
| 266 | candi_pools_encoded = [[[idx2vec[idx] |
| 267 | for idx in candi] |
| 268 | for candi in pool] |
| 269 | for pool in candi_pools_crop] |