forked from Mleader2/text_scalpel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict_utils.py
98 lines (88 loc) · 3.63 KB
/
predict_utils.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
# coding=utf-8
# Copyright 2019 The Google Research Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Lint as: python3
"""Utility functions for running inference with a LaserTagger model."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from collections import defaultdict
# from typing import Mapping, Sequence, Text
#
# import bert_example
import tagging
class LaserTaggerPredictor(object):
"""Class for computing and realizing predictions with LaserTagger."""
def __init__(self, tf_predictor,
example_builder,
label_map):
"""Initializes an instance of LaserTaggerPredictor.
Args:
tf_predictor: Loaded Tensorflow model.
example_builder: BERT example builder.
label_map: Mapping from tags to tag IDs.
"""
self._predictor = tf_predictor
self._example_builder = example_builder
self._id_2_tag = {
tag_id: tagging.Tag(tag) for tag, tag_id in label_map.items()
}
def predict_batch(self, sources_batch, location_batch=None): # 由predict改成
"""Returns realized prediction for given sources."""
# Predict tag IDs.
keys = ['input_ids', 'input_mask', 'segment_ids']
input_info = defaultdict(list)
example_list = []
location = None
for id, sources in enumerate(sources_batch):
if location_batch is not None:
location = location_batch[id] # 表示是否能修改
example = self._example_builder.build_bert_example(sources, location=location)
if example is None:
raise ValueError("Example couldn't be built.")
for key in keys:
input_info[key].append(example.features[key])
example_list.append(example)
out = self._predictor(input_info)
prediction_list= []
for output,example in zip(out['pred'], example_list):
predicted_ids = output.tolist()
# Realize output.
example.features['labels'] = predicted_ids
# Mask out the begin and the end token.
example.features['labels_mask'] = [0] + [1] * (len(predicted_ids) - 2) + [0]
labels = [
self._id_2_tag[label_id] for label_id in example.get_token_labels()
]
prediction = example.editing_task.realize_output(labels)
prediction_list.append(prediction)
return prediction_list
# def predict(self, sources): # TODO 这是逐个样本预测,没有batch 应该好改
# """Returns realized prediction for given sources."""
# example = self._example_builder.build_bert_example(sources)
# if example is None:
# raise ValueError("Example couldn't be built.")
#
# # Predict tag IDs.
# keys = ['input_ids', 'input_mask', 'segment_ids']
# out = self._predictor({key: [example.features[key]] for key in keys})
# predicted_ids = out['pred'][0].tolist()
# # Realize output.
# example.features['labels'] = predicted_ids
# # Mask out the begin and the end token.
# example.features['labels_mask'] = [0] + [1] * (len(predicted_ids) - 2) + [0]
# labels = [
# self._id_2_tag[label_id] for label_id in example.get_token_labels()
# ]
# return example.editing_task.realize_output(labels)