Coder Social home page Coder Social logo

bert_ner's Introduction

PyTorch Implementation of NER with pretrained Bert

I know that you know BERT. In the great paper, the authors claim that the pretrained models do great in NER. It's even impressive, allowing for the fact that they don't use any prediction-conditioned algorithms like CRFs. We try to reproduce the result in a simple manner.

Requirements

  • python>=3.6 (Let's move on to python 3 if you still use python 2)
  • pytorch==1.0
  • pytorch_pretrained_bert==0.6.1
  • numpy>=1.15.4

Training & Evaluating

  • STEP 1. Run the command below to download conll 2003 NER dataset.
bash download.sh

It should be extracted to conll2003/ folder automatically.

  • STEP 2a. Run the command if you want to do the feature-based approach.
python train.py --logdir checkpoints/feature --batch_size 128 --top_rnns --lr 1e-4 --n_epochs 30
  • STEP 2b. Run the command if you want to do the fine-tuning approach.
python train.py --logdir checkpoints/finetuning --finetuning --batch_size 32 --lr 5e-5 --n_epochs 3

Results in the paper

  • Feature-based approach

  • Fine-tuning

Results

  • F1 scores on conll2003 valid dataset are reported.
  • You can check the classification outputs in checkpoints.
epoch feature-based fine-tuning
1 0.2 0.95
2 0.75 0.95
3 0.84 0.96
4 0.88
5 0.89
6 0.90
7 0.90
8 0.91
9 0.91
10 0.92
11 0.92
12 0.93
13 0.93
14 0.93
15 0.93
16 0.92
17 0.93
18 0.93
19 0.93
20 0.93
21 0.94
22 0.94
23 0.93
24 0.93
25 0.93
26 0.93
27 0.93
28 0.93
29 0.94
30 0.93

bert_ner's People

Contributors

kyubyong 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bert_ner's Issues

F1, recall and precision calculation

Hi,
I was wondering how you are actually calculating your scores.

y_true = np.array([hp.tag2idx[line.split()[1]] for line in open(f, 'r').read().splitlines() if len(line) > 0])
y_pred = np.array([hp.tag2idx[line.split()[2]] for line in open(f, 'r').read().splitlines() if len(line) > 0])

num_proposed = len(y_pred[y_pred>1])
num_correct = (np.logical_and(y_true==y_pred, y_true>1)).astype(np.int).sum()
num_gold = len(y_true[y_true>1])

precision = num_correct / num_proposed
recall = num_correct / num_gold

Can you explain what the above code means?
How does this translate to say recall = TP / TP + FN? Don't you have to use some multi-class method?

Also, why are you only taking the index where y_true>1? Is it because you do not want the Other tag to skew your results? Thanks!

The ##word should not be predicted

In bert paper, it seems that the words start with '##' should not be predicted. And you did compute is_head variable, but why this variable is not used when computing loss ?

datasets

Can I use other datasets on this model?

Cuda out of memory error for finetuning

I'm getting the following error when i try to run the finetuning example:

RuntimeError: CUDA out of memory. Tried to allocate 85.00 MiB (GPU 0; 4.00 GiB total capacity; 3.04 GiB already allocated; 9.21 MiB free; 15.31 MiB cached)

Reducing the batch_size didn't help.

can not find the file pytorch_pretrained_bert.py

hi,
Thank you for sharing the code. But I can not find the file 'pytorch_pretrained_bert.py', hence I could not run the code.
Please tell how can I find the file 'pytorch_pretrained_bert.py'. Thanks

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.