It is a TensorFlow-based framwork for easily building relation extraction models. We divide the pipeline of relation extraction into four parts, which are embedding, encoder, selector and classifier. For each part we have implemented several methods.
- Word embedding
- Position embedding
- Concatenation method
- Softmax loss function
All those methods could be combined freely.
We also provide fast training and testing codes. You could change hyper-parameters or appoint model architectures by using Python arguments. A plotting method is also in the package.
Advesarial training method is implemented following Wu et al. (2017). Since it's a general training method, you could adapt it to any models with simply adding a few lines of code.
This project is under MIT license.
- Python (>=2.7)
- TensorFlow (>=1.4.1)
- CUDA (>=8.0) if you are using gpu
- Matplotlib (>=2.0.0)
- scikit-learn (>=0.18)
- Install TensorFlow
- Clone the OpenNRE repository:
git clone [email protected]:thunlp/OpenNRE.git
tar xvf origin_data.tar
F1 Score Results
- (Adv) means using adversarial training
The processed data will be stored in
HINT: If you are using python3, execute
python train.py --model_name pcnn_att
model_name appoints model architecture, and
pcnn_att is the name of one of our models. All available models are in
./model. About other arguments please refer to
./train.py. Once you start training, all checkpoints are stored in
python test.py --model_name pcnn_att
Same usage as training. When finishing testing, the best checkpoint's corresponding pr-curve data will be stored in
python draw_plot.py pcnn_att
The plot will be saved as
./test_result/pr_curve.png. You could appoint several models in the arguments, like
python draw_plot.py pcnn_att pcnn_max pcnn_ave, as long as there are these models' results in
Build Your Own Model
Not only could you train and test existing models in our package, you could also build your own model or add methods to the four basic modules. When adding a new model, you could create a python file in
./model having the same name as the model and implement it like following:
from framework import Framework import tensorflow as tf def your_new_model(is_training): if is_training: framework = Framework(is_training=True) else: framework = Framework(is_training=False) # Word Embedding word_embedding = framework.embedding.word_embedding() # Position Embedding. Set simple_pos=True to use simple pos embedding pos_embedding = framework.embedding.pos_embedding() # Concat two embeddings embedding = framework.embedding.concat_embedding(word_embedding, pos_embedding) # PCNN. Appoint activation to whatever activation function you want to use. # There are three more encoders: # framework.encoder.cnn # framework.encoder.rnn # framework.encoder.birnn x = framework.encoder.pcnn(embedding, FLAGS.hidden_size, framework.mask, activation=tf.nn.relu) # Selective attention. Setting parameter dropout_before=True means using dropout before attention. # There are three more selecting method # framework.selector.maximum # framework.selector.average # framework.selector.no_bag logit, repre = framework.selector.attention(x, framework.scope, framework.label_for_select) if is_training: loss = framework.classifier.softmax_cross_entropy(logit) output = framework.classifier.output(logit) # Set optimizer to whatever optimizer you want to use framework.init_train_model(loss, output, optimizer=tf.train.GradientDescentOptimizer) framework.load_train_data() framework.train() else: framework.init_test_model(tf.nn.softmax(logit)) framework.load_test_data() framework.test()
After creating model's python file, you need to add the model to
./test.py as following:
# other code ... def main(): from model.your_new_model import your_new_model # other code ...
Then you can train, test and plot!
As for using adversarial training, please refer to
./model/pcnn_att_adv.py for more details.