stanleylsx / entity_extractor_by_pointer Goto Github PK
View Code? Open in Web Editor NEW使用torch整合两种经典的指针NER抽取范式,分别是SpanBert和苏神的GlobalPointer,简单加了些tricks,配置后一键运行
使用torch整合两种经典的指针NER抽取范式,分别是SpanBert和苏神的GlobalPointer,简单加了些tricks,配置后一键运行
def extract_entities(configs, tokenizer, text, model, device):
"""
从验证集中预测到相关实体
"""
predict_results = {}
encode_results = tokenizer(text, padding='max_length')
input_ids = encode_results.get('input_ids')
token = tokenizer.convert_ids_to_tokens(input_ids)
mapping = rematch(text, token)
token_ids = torch.unsqueeze(torch.LongTensor(input_ids), 0) .to(device)
attention_mask = torch.unsqueeze(torch.LongTensor(encode_results.get('attention_mask')), 0).to(device)
model_outputs = model(token_ids, attention_mask).detach().to('cpu')
decision_threshold = float(configs.decision_threshold)
for model_output in model_outputs:
start = np.where(model_output[:, :, 0] > decision_threshold)
end = np.where(model_output[:, :, 1] > decision_threshold)
for _start, predicate1 in zip(*start):
for _end, predicate2 in zip(*end):
if _start <= _end and predicate1 == predicate2:
if len(mapping[_start]) > 0 and len(mapping[_end]) > 0:
start_in_text = mapping[_start][0]
end_in_text = mapping[_end][-1]
entity_text = text[start_in_text: end_in_text + 1]
predict_results.setdefault(predicate1, set()).add(entity_text)
break
return predict_results
to('cpu')
验证时 RuntimeError: CUDA error: device-side assert triggered
你好,请问一下start/end这种标注方式,相对于传统的CRF有什么优点呢?
token_results = self.tokenizer(text)
in engines/data.py
while your "mode=train" in your system.config
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.