Git Product home page Git Product logo

qaner's Introduction

tests linter

python 3.7 release (latest by date) license

pre-commit code style: black

pypi version pypi downloads

My public talk about this project at Sberloga:
QaNER - NER via Exractive QA

QaNER

Unofficial implementation of QaNER: Prompting Question Answering Models for Few-shot Named Entity Recognition.

You can adopt this pipeline for arbitrary BIO-markup data.

Installation

pip install qaner

CoNLL-2003

Pipeline results on CoNLL-2003 dataset:

How to use

Training

Script for training QaNER model:

qaner-train \
--bert_model_name 'bert-base-uncased' \
--path_to_prompt_mapper 'data/conll2003/prompt_mapper.json' \
--path_to_train_data 'data/conll2003/train.bio' \
--path_to_test_data 'data/conll2003/test.bio' \
--path_to_save_model 'dayyass/qaner-conll-bert-base-uncased' \
--n_epochs 2 \
--batch_size 128 \
--learning_rate 1e-5 \
--seed 42 \
--log_dir 'runs/qaner'

Required arguments:

  • --bert_model_name - base bert model for QaNER fine-tuning
  • --path_to_prompt_mapper - path to prompt mapper json file
  • --path_to_train_data - path to train data (BIO-markup)
  • --path_to_test_data - path to test data (BIO-markup)
  • --path_to_save_model - path to save trained QaNER model
  • --n_epochs - number of epochs to fine-tune
  • --batch_size - batch size
  • --learning_rate - learning rate

Optional arguments:

  • --seed - random seed for reproducibility (default: 42)
  • --log_dir - tensorboard log_dir (default: 'runs/qaner')

Infrerence

Script for inference trained QaNER model:

qaner-inference \
--context 'EU rejects German call to boycott British lamb .' \
--question 'What is the organization?' \
--path_to_prompt_mapper 'data/conll2003/prompt_mapper.json' \
--path_to_trained_model 'dayyass/qaner-conll-bert-base-uncased' \
--n_best_size 1 \
--max_answer_length 100 \
--seed 42

Result:

question: What is the organization?

context: EU rejects German call to boycott British lamb .

answer: [Span(token='EU', label='ORG', start_context_char_pos=0, end_context_char_pos=2)]

Required arguments:

  • --context - sentence to extract entities from
  • --question - question prompt with entity name to extract (examples below)
  • --path_to_prompt_mapper - path to prompt mapper json file
  • --path_to_trained_model - path to trained QaNER model
  • --n_best_size - number of best QA answers to consider

Optional arguments:

  • --max_answer_length - entity max length to eliminate very long entities (default: 100)
  • --seed - random seed for reproducibility (default: 42)

Possible inference questions for CoNLL-2003:

  • What is the location? (LOC)
  • What is the person? (PER)
  • What is the organization? (ORG)
  • What is the miscellaneous entity? (MISC)

Requirements

Python >= 3.7

Citation

@misc{liu2022qaner,
    title         = {QaNER: Prompting Question Answering Models for Few-shot Named Entity Recognition},
    author        = {Andy T. Liu and Wei Xiao and Henghui Zhu and Dejiao Zhang and Shang-Wen Li and Andrew Arnold},
    year          = {2022},
    eprint        = {2203.01543},
    archivePrefix = {arXiv},
    primaryClass  = {cs.LG}
}

qaner's People

Contributors

dayyass avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

qaner's Issues

About the parameter 'n_best_size'

Hello, I have some questions regarding the parameter 'n_best_size'. In the program, 'n_best_size' is set to a fixed value, but in reality, each sentence may contain a different number of entities. Therefore, I would like to inquire about your opinion on this matter. I am looking forward to your response.

F1 score may be obly concern about the entity

Thank you for your project. I noticed that the confusion matrix is used to calculate the metrics, and when calculating the final macro F1 score, the "O" type is not excluded. I think this is incorrect. Since there are far fewer entities compared to "O", and the focus is mainly on extracting entities, it is generally necessary to exclude predictions for entities. I made the following changes in metric.py:
` for span_true, span_pred in zip(spans_true_batch, spans_pred_batch_top_1):
span_pred = span_pred[0] # type: ignore

    i = entity_mapper[span_true.label]
    j = entity_mapper[span_pred.label]  # type: ignore
    
    confusion_matrix_true_denominator[i] += 1
    confusion_matrix_pred_denominator[j] += 1
    if span_true == span_pred:
        ner_confusion_matrix[i, j] += 1
    if i!=0:
        all_true+=1
        if span_true == span_pred:
            all_pred_correct+=1
        if j!=0:
            all_pred+=1

p1=all_pred_correct/all_pred if all_pred_correct!=0 else 0
r1=all_pred_correct/all_true if all_pred_correct!=0 else 0
f1=2*p1*r1/(p1+r1) if p1+r1!=0 else 0
metrics["ner_p"] = p1
metrics["ner_r"] = r1
metrics["ner_f1"]= f1`

ner_f1 indicates "micro_f1" about entity type

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.