-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add NPTag #1246
Merged
Merged
Add NPTag #1246
Changes from 3 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
579e37e
add nptag
linjieccc 5210145
support static mode
linjieccc f0b1517
Update modeling
linjieccc 40969ff
Update comments for nptag
linjieccc f537251
add nptag finetune example
linjieccc 06b7ef9
Update NPTag finetune example
linjieccc 641ae59
Update nptag taskflow
linjieccc b7426da
Update task name
linjieccc 49d464c
Update finetune example readme
linjieccc c8f7392
Update NPTag
linjieccc 082748d
Update NPTag example
linjieccc a43289a
Update NPTag example
linjieccc d48edd2
Update NPTag example
linjieccc daa842d
Update README.md
linjieccc 8b0d567
Update README.md
linjieccc 258fa8f
Update taskflow static mode
linjieccc e0592bc
Update README.md
linjieccc 2b83321
Update README.md
linjieccc 5f7c5db
Update static mode inference
linjieccc fdb4864
Update static mode
linjieccc e770180
Update README.md
linjieccc dbead53
Update README.md
linjieccc 9376990
Update README.md
linjieccc da12bd0
Merge branch 'develop' into add_nptag
ZeyuChen 08e01b6
Update NPTag code format
linjieccc 1c891cd
Update README.md
linjieccc c99a031
Update README.md
linjieccc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,279 @@ | ||
# coding:utf-8 | ||
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# 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. | ||
|
||
import json | ||
from collections import OrderedDict | ||
from typing import Optional, List | ||
|
||
import numpy as np | ||
import paddle | ||
|
||
from ..datasets import MapDataset, load_dataset | ||
from ..data import Stack, Pad, Tuple | ||
from ..transformers import ErnieCtmNptagModel, ErnieCtmTokenizer | ||
from .utils import download_file, add_docstrings, static_mode_guard, dygraph_mode_guard | ||
from .utils import BurkhardKellerTree | ||
from .task import Task | ||
|
||
|
||
URLS = { | ||
"name_category_map.json":[ | ||
"https://paddlenlp.bj.bcebos.com/models/transformers/ernie_ctm/name_category_map.json", | ||
"40e8adaaadb567e90978c5389d595b48", | ||
], | ||
} | ||
|
||
|
||
usage = r""" | ||
from paddlenlp import Taskflow | ||
|
||
nptag = Taskflow("noun_phrase_tagging") | ||
nptag("糖醋排骨") | ||
''' | ||
[{'text': '糖醋排骨', 'cls_label': '菜品'}] | ||
''' | ||
nptag(["糖醋排骨", "红曲霉菌"]) | ||
''' | ||
[{'text': '糖醋排骨', 'cls_label': '菜品'}, {'text': '红曲霉菌', 'cls_label': '微生物'}] | ||
''' | ||
|
||
nptag = Taskflow("noun_phrase_tagging", linking=True) | ||
nptag(["糖醋排骨", "红曲霉菌"]) | ||
''' | ||
[{'text': '糖醋排骨', 'cls_label': '菜品', 'category': '饮食类_菜品'}, {'text': '红曲霉菌', 'cls_label': '微生物', 'category': '生物类_微生物'}] | ||
''' | ||
""" | ||
|
||
|
||
@add_docstrings(usage) | ||
class NPTagTask(Task): | ||
""" | ||
Noun phrase tagging task that convert the noun phrase to POS tag. | ||
Args: | ||
task(string): The name of task. | ||
model(string): The model name in the task. | ||
batch_size(int): Numbers of examples a batch. | ||
linking(bool): Returns the categories. The fine-grained labels (cls_label) will link with the coarse-grained labels (category). | ||
""" | ||
def __init__(self, | ||
task, | ||
model, | ||
batch_size=1, | ||
linking=False, | ||
**kwargs): | ||
super().__init__(task=task, model=model, **kwargs) | ||
self._usage = usage | ||
self._static_mode = True | ||
self._batch_size = batch_size | ||
self._linking = linking | ||
self._construct_tokenizer(model) | ||
self._name_dict = None | ||
name_dict_path = download_file(self._task_path, | ||
"name_category_map.json", | ||
URLS["name_category_map.json"][0], | ||
URLS["name_category_map.json"][1]) | ||
self._construct_dict_map(name_dict_path) | ||
if self._static_mode: | ||
self._get_inference_model() | ||
else: | ||
self._construct_model(model) | ||
|
||
def _construct_dict_map(self, name_dict_path): | ||
""" | ||
Construct dict map for the predictor. | ||
""" | ||
with open(name_dict_path, encoding="utf-8") as fp: | ||
self._name_dict = json.load(fp) | ||
self._tree = BurkhardKellerTree() | ||
for k in self._name_dict: | ||
self._tree.add(k) | ||
self._cls_vocabs = OrderedDict() | ||
for k in self._name_dict: | ||
for c in k: | ||
if c not in self._cls_vocabs: | ||
self._cls_vocabs[c] = len(self._cls_vocabs) | ||
self._cls_vocabs["[PAD]"] = len(self._cls_vocabs) | ||
self._id_vocabs = dict(zip(self._cls_vocabs.values(), self._cls_vocabs.keys())) | ||
self._vocab_ids = self._tokenizer.vocab.to_indices(list(self._cls_vocabs.keys())) | ||
|
||
def _decode(self, pred_ids): | ||
tokens = [self._id_vocabs[i] for i in pred_ids] | ||
valid_token = [] | ||
for token in tokens: | ||
if token == "[PAD]": | ||
break | ||
valid_token.append(token) | ||
return "".join(valid_token) | ||
|
||
def _search(self, scores_can, pred_ids_can, depth, path, score): | ||
if depth >= 5: | ||
return [(path, score)] | ||
res = [] | ||
for i in range(len(pred_ids_can[0])): | ||
tmp_res = self._search( | ||
scores_can, pred_ids_can, depth + 1, path + [pred_ids_can[depth][i]], | ||
score + scores_can[depth][i] | ||
) | ||
res.extend(tmp_res) | ||
return res | ||
|
||
def _find_topk(self, a, k, axis=-1, largest=True, sorted=True): | ||
if axis is None: | ||
axis_size = a.size | ||
else: | ||
axis_size = a.shape[axis] | ||
assert 1 <= k <= axis_size | ||
|
||
a = np.asanyarray(a) | ||
if largest: | ||
index_array = np.argpartition(a, axis_size-k, axis=axis) | ||
topk_indices = np.take(index_array, -np.arange(k)-1, axis=axis) | ||
else: | ||
index_array = np.argpartition(a, k-1, axis=axis) | ||
topk_indices = np.take(index_array, np.arange(k), axis=axis) | ||
topk_values = np.take_along_axis(a, topk_indices, axis=axis) | ||
if sorted: | ||
sorted_indices_in_topk = np.argsort(topk_values, axis=axis) | ||
if largest: | ||
sorted_indices_in_topk = np.flip(sorted_indices_in_topk, axis=axis) | ||
sorted_topk_values = np.take_along_axis( | ||
topk_values, sorted_indices_in_topk, axis=axis) | ||
sorted_topk_indices = np.take_along_axis( | ||
topk_indices, sorted_indices_in_topk, axis=axis) | ||
return sorted_topk_values, sorted_topk_indices | ||
return topk_values, topk_indices | ||
|
||
def _construct_input_spec(self): | ||
""" | ||
Construct the input spec for the convert dygraph model to static model. | ||
""" | ||
self._input_spec = [ | ||
paddle.static.InputSpec( | ||
shape=[None, None], dtype="int64", | ||
name="input_ids"), # input_ids | ||
paddle.static.InputSpec( | ||
shape=[None, None], dtype="int64", | ||
name="token_type_ids"), # segment_ids | ||
] | ||
|
||
def _construct_model(self, model): | ||
""" | ||
Construct the inference model for the predictor. | ||
""" | ||
model_instance = ErnieCtmNptagModel.from_pretrained(model) | ||
model_instance.eval() | ||
self._model = model_instance | ||
|
||
def _construct_tokenizer(self, model): | ||
""" | ||
Construct the tokenizer for the predictor. | ||
""" | ||
tokenizer_instance = ErnieCtmTokenizer.from_pretrained(model) | ||
self._tokenizer = tokenizer_instance | ||
|
||
def _preprocess(self, inputs): | ||
""" | ||
Create the dataset and dataloader for the predict. | ||
""" | ||
inputs = self._check_input_text(inputs) | ||
self._cls_seq_length = 5 | ||
num_workers = self.kwargs[ | ||
'num_workers'] if 'num_workers' in self.kwargs else 0 | ||
lazy_load = self.kwargs[ | ||
'lazy_load'] if 'lazy_load' in self.kwargs else False | ||
|
||
# Prompt template: input_text + "是" + "[MASK]" * cls_seq_length | ||
prompt_template = ["是"] + ["[MASK]"] * self._cls_seq_length | ||
prompt_template_ids = self._tokenizer.vocab.to_indices(prompt_template) | ||
|
||
def read(inputs): | ||
for text in inputs: | ||
input_id = self._tokenizer.vocab.to_indices(list(text)) | ||
input_id += prompt_template_ids | ||
input_id = self._tokenizer.build_inputs_with_special_tokens(input_id) | ||
token_type_id = [self._tokenizer.pad_token_type_id] * len(input_id) | ||
yield input_id, token_type_id | ||
|
||
infer_ds = load_dataset(read, inputs=inputs, lazy=lazy_load) | ||
batchify_fn = lambda samples, fn=Tuple( | ||
Pad(axis=0, pad_val=self._tokenizer.pad_token_id), # input_ids | ||
Pad(axis=0, pad_val=self._tokenizer.pad_token_type_id) # token_type_ids | ||
): fn(samples) | ||
|
||
infer_data_loader = paddle.io.DataLoader( | ||
infer_ds, | ||
collate_fn=batchify_fn, | ||
num_workers=num_workers, | ||
batch_size=self._batch_size, | ||
shuffle=False, | ||
return_list=True) | ||
|
||
outputs = {} | ||
outputs['data_loader'] = infer_data_loader | ||
outputs['texts'] = inputs | ||
return outputs | ||
|
||
def _run_model(self, inputs): | ||
all_scores_can = [] | ||
all_preds_can = [] | ||
pred_ids = [] | ||
|
||
for batch in inputs['data_loader']: | ||
input_ids, token_type_ids = batch | ||
self.input_handles[0].copy_from_cpu(input_ids.numpy()) | ||
self.input_handles[1].copy_from_cpu(token_type_ids.numpy()) | ||
self.predictor.run() | ||
logits = self.output_handle[0].copy_to_cpu() | ||
logits = logits.squeeze()[-(self._cls_seq_length + 1): -1, self._vocab_ids] | ||
# Find topk candidates of scores and predicted indices. | ||
scores_can, pred_ids_can = self._find_topk(logits, k=4, axis=-1) | ||
all_scores_can.extend([scores_can.tolist()]) | ||
all_preds_can.extend([pred_ids_can.tolist()]) | ||
pred_ids.extend([pred_ids_can[:, 0].tolist()]) | ||
|
||
inputs['all_scores_can'] = all_scores_can | ||
inputs['all_preds_can'] = all_preds_can | ||
inputs['pred_ids'] = pred_ids | ||
return inputs | ||
|
||
def _postprocess(self, inputs): | ||
results = [] | ||
|
||
for i in range(len(inputs['texts'])): | ||
cls_label = self._decode(inputs['pred_ids'][i]) | ||
|
||
result = { | ||
'text': inputs['texts'][i], | ||
'cls_label': cls_label, | ||
} | ||
|
||
if cls_label not in self._name_dict: | ||
scores_can = inputs['all_scores_can'][i] | ||
pred_ids_can = inputs['all_preds_can'][i] | ||
labels_can = self._search(scores_can, pred_ids_can, 0, [], 0) | ||
labels_can.sort(key=lambda d: -d[1]) | ||
for labels in labels_can: | ||
cls_label_can = self._decode(labels[0]) | ||
if cls_label_can in self._name_dict: | ||
result['cls_label'] = cls_label_can | ||
break | ||
else: | ||
labels_can = self._tree.search_similar_word(cls_label) | ||
result['cls_label'] = labels_can[0][0] | ||
|
||
if self._linking: | ||
result['category'] = self._name_dict[result['cls_label']] | ||
results.append(result) | ||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
需要注明If true xxxx
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done