模型:
allenai/longformer-scico
该模型是论文 SciCo: Hierarchical Cross-Document Coreference for Scientific Concepts (AKBC 2021) 中讨论的统一模型,将层次交叉文档共指消解(H-CDCR)任务作为一个多类问题进行了建模。该模型以两个提及m1和m2及其相应的上下文作为输入,并输出4个分数:
我们提供以下代码作为将全局注意力集中在特殊标记,和上的示例:
from transformers import AutoTokenizer, AutoModelForSequenceClassification import torch tokenizer = AutoTokenizer.from_pretrained('allenai/longformer-scico') model = AutoModelForSequenceClassification.from_pretrained('allenai/longformer-scico') start_token = tokenizer.convert_tokens_to_ids("<m>") end_token = tokenizer.convert_tokens_to_ids("</m>") def get_global_attention(input_ids): global_attention_mask = torch.zeros(input_ids.shape) global_attention_mask[:, 0] = 1 # global attention to the CLS token start = torch.nonzero(input_ids == start_token) # global attention to the <m> token end = torch.nonzero(input_ids == end_token) # global attention to the </m> token globs = torch.cat((start, end)) value = torch.ones(globs.shape[0]) global_attention_mask.index_put_(tuple(globs.t()), value) return global_attention_mask m1 = "In this paper we present the results of an experiment in <m> automatic concept and definition extraction </m> from written sources of law using relatively simple natural methods." m2 = "This task is important since many natural language processing (NLP) problems, such as <m> information extraction </m>, summarization and dialogue." inputs = m1 + " </s></s> " + m2 tokens = tokenizer(inputs, return_tensors='pt') global_attention_mask = get_global_attention(tokens['input_ids']) with torch.no_grad(): output = model(tokens['input_ids'], tokens['attention_mask'], global_attention_mask) scores = torch.softmax(output.logits, dim=-1) # tensor([[0.0818, 0.0023, 0.0019, 0.9139]]) -- m1 is a child of m2
注意:这个模型和 paper 中提到的原始模型之间有细微差别。原始模型在标记(相当于[CLS])之上包括一个单一线性层,而这个模型包括一个两层的多层感知机(MLP),以便与LongformerForSequenceClassification保持一致。原始代码库可以在 here 中找到。
@inproceedings{ cattan2021scico, title={SciCo: Hierarchical Cross-Document Coreference for Scientific Concepts}, author={Arie Cattan and Sophie Johnson and Daniel S Weld and Ido Dagan and Iz Beltagy and Doug Downey and Tom Hope}, booktitle={3rd Conference on Automated Knowledge Base Construction}, year={2021}, url={https://openreview.net/forum?id=OFLbgUP04nC} }