Docstring for Rank Model
| 303 | return batch_out |
| 304 | |
| 305 | class RankModel(Model): |
| 306 | """Docstring for Rank Model""" |
| 307 | def __init__(self, model_path, mode, use_cuda): |
| 308 | # init rank model |
| 309 | super(RankModel, self).__init__(model_path, mode, use_cuda) |
| 310 | |
| 311 | # parsing the lac model address |
| 312 | parent_path = os.path.split(model_path)[0] |
| 313 | lac_path = os.path.join(parent_path, 'lac_model') |
| 314 | |
| 315 | # init lac model |
| 316 | self.lac = LacModel(model_path=lac_path, mode='lac', use_cuda=use_cuda) |
| 317 | |
| 318 | def run(self, texts): |
| 319 | if self.custom is not None: |
| 320 | self.lac.custom = self.custom |
| 321 | |
| 322 | lac_result = self.lac.call_run(texts) |
| 323 | self.batch = self.lac.batch |
| 324 | |
| 325 | if len(lac_result) != 4: |
| 326 | return lac_result["crf_result"] |
| 327 | |
| 328 | crf_decode = lac_result["crf_decode"] |
| 329 | crf_result = lac_result["crf_result"] |
| 330 | tensor_words = lac_result["tensor_words"] |
| 331 | words_length = lac_result["words_length"] |
| 332 | |
| 333 | result = [[word, tag] for word, tag, tag_for_rank in crf_result] |
| 334 | tags_for_rank = [tag_for_rank for word, tag, tag_for_rank in crf_result if len(tag_for_rank) != 0] |
| 335 | |
| 336 | rank_decode = self.predictor.run([tensor_words, crf_decode[0]]) |
| 337 | weight = self.parse_result(tags_for_rank, rank_decode[0], words_length) |
| 338 | |
| 339 | for _ in range(len(result)): |
| 340 | if len(result[_][0]) == 0: |
| 341 | result[_].append([]) |
| 342 | else: |
| 343 | result[_].append(weight.pop(0)) |
| 344 | |
| 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): |