将RANK模型输出的Tensor转为明文
(self, tags_for_rank, result, words_length)
| 345 | return result if self.batch else result[0] |
| 346 | |
| 347 | def parse_result(self, tags_for_rank, result, words_length): |
| 348 | """将RANK模型输出的Tensor转为明文""" |
| 349 | offset_list = result.lod[0] |
| 350 | rank_weight = result.data.int64_data() |
| 351 | batch_size = len(offset_list) - 1 |
| 352 | |
| 353 | batch_out = [] |
| 354 | for sent_index in range(batch_size): |
| 355 | begin, end = offset_list[sent_index], offset_list[sent_index + 1] |
| 356 | |
| 357 | tags = tags_for_rank[sent_index] |
| 358 | word_length = words_length[sent_index] |
| 359 | weight = rank_weight[begin:end] |
| 360 | |
| 361 | # 重新填充被省略的单词的char部分 |
| 362 | for current in range(len(word_length)-1, -1, -1): |
| 363 | for offset in range(1, word_length[current]): |
| 364 | weight.insert(current + offset, weight[current]) |
| 365 | |
| 366 | weight_out = [] |
| 367 | for ind, tag in enumerate(tags): |
| 368 | if tag.endswith("B") or tag.endswith("S"): |
| 369 | weight_out.append(weight[ind]) |
| 370 | continue |
| 371 | weight_out[-1] = max(weight_out[-1], weight[ind]) |
| 372 | |
| 373 | batch_out.append(weight_out) |
| 374 | return batch_out |
| 375 | |
| 376 | def train(self, model_save_dir, train_data, test_data, iter_num, thread_num): |
| 377 | logging.info("To be continued...") |