OpenNRE Documentation

repository·master·Indexed 26 days ago

https://github.com/thunlp/opennre

An open-source, extensible toolkit for Neural Relation Extraction (NRE). It provides a unified framework for implementing relation extraction models, supporting supervised and distant supervised settings using CNNs and BERT-based architectures. The toolkit includes utilities for training models, performing inference with pretrained models like wiki80 and TACRED, and managing datasets.

Tokens
736
Snippets
3
Records
5
Agent score
39%

What's inside OpenNRE

  1. Install OpenNRE from Git Repository

    master

    To use OpenNRE, clone the repository and install the required dependencies and the package itself. Ensure you have an appropriate PyTorch version installed for your machine's CUDA version before proceeding.

    1. Clone the repository:
      git clone https://github.com/thunlp/OpenNRE.git
      (Use --depth 1 for a faster clone if needed).
    2. Install requirements:
      pip install -r requirements.txt
    3. Install the package:
      python setup.py install
      If you intend to modify the source code, use python setup.py develop instead.
    git clone https://github.com/thunlp/OpenNRE.git
    pip install -r requirements.txt
    python setup.py install
  2. Download Datasets and Pretrained Files

    master

    OpenNRE does not include data or pretrained files in the repository to keep the deployment fast. You must download them manually using the provided scripts in the benchmark and pretrain folders.

    Example: To download the FewRel dataset, run:

    bash benchmark/download_fewrel.sh
  3. Train Relation Extraction Models

    master

    OpenNRE provides example scripts in the example folder for training both supervised and bag-level relation extraction models. You can use provided datasets or your own.

    Train a Bag-Level CNN-ATT Model

    Use example/train_bag_cnn.py to train on datasets like nyt10m:

    python example/train_bag_cnn.py \
        --metric auc \
        --dataset nyt10m \
        --batch_size 160 \
        --lr 0.1 \
        --weight_decay 1e-5 \
        --max_epoch 100 \
        --max_length 128 \
        --seed 42 \
        --encoder pcnn \
        --aggr att

    Train a Supervised BERT Model

    Use example/train_supervised_bert.py to train on the wiki80 dataset:

    python example/train_supervised_bert.py \
        --pretrain_path bert-base-uncased \
        --dataset wiki80
  4. Perform Relation Extraction with Pretrained Models

    master

    You can perform sentence-level relation extraction by loading a pretrained model using opennre.get_model() and calling the .infer() method. The .infer() method requires a dictionary containing the text and the character positions of the head (h) and tail (t) entities.

    To use a GPU, call .cuda() on the model instance before inference.

  5. Available Pretrained Models

    master

    The following models are currently available for immediate use via opennre.get_model():

    • wiki80_cnn_softmax: Trained on wiki80 dataset with a CNN encoder.
    • wiki80_bert_softmax: Trained on wiki80 dataset with a BERT encoder.
    • wiki80_bertentity_softmax: Trained on wiki80 dataset with a BERT encoder (using entity representation concatenation).
    • tacred_bert_softmax: Trained on TACRED dataset with a BERT encoder.
    • tacred_bertentity_softmax: Trained on TACRED dataset with a BERT encoder (using entity representation concatenation).