BERT for Attribute Extraction in Knowledge Graph

Feb 5, 2022 | Educational

Attribute extraction is crucial for building robust knowledge graphs. By leveraging BERT (Bidirectional Encoder Representations from Transformers), we can enhance our extraction techniques through two primary methods: fine-tuning and feature extraction. This article will guide you on how to implement BERT for attribute extraction effectively.

Prerequisites

  • TensorFlow version 1.10
  • scikit-learn

Pre-trained Models

For this project, you can use the following model:

  • BERT-Base, Chinese: This model supports both Simplified and Traditional Chinese, consists of 12 layers, 768 hidden units, 12 attention heads, and has 110M parameters.

Dataset

The dataset is constructed from character entries in Baidu Encyclopedia. To create an efficient dataset, follow these steps:

  • Filter out any corpus that does not contain entities and attributes.
  • Extract entities and attributes using name entity recognition.
  • Obtain labels from the Baidu Encyclopedia infobox, with most labeled manually, which may introduce some inaccuracies.

Getting Started

To prepare your data for processing, follow these commands:

  • run strip.py to get cleaned data.
  • run data_process.py to process the data into a numpy input file.
  • Make sure to refer to the parameters file required to run the model.

Running the Tests

You can evaluate your model using either of the two methods: fine-tuning or feature extraction.

Fine-tuning Method

To fine-tune the model on the birthplace dataset, execute the following command:

python run_classifier.py             --task_name=my             --do_train=true             --do_predict=true             --data_dir=a             --vocab_file=hometinyzhaomengbertmodel/vocab.txt             --bert_config_file=hometinyzhaomengbertmodel/bert_config.json             --init_checkpoint=hometinyzhaomengbertmodel/bert_model.ckpt             --max_seq_length=80             --train_batch_size=32             --learning_rate=2e-5             --num_train_epochs=1.0             --output_dir=.output

After running the above command, utilize proba2metrics.py to evaluate the final results and identify misclassified items.

Feature Extraction Method

For the feature-extraction approach, run the following command:

python extract_features.py             --input_file=../data/birth_place_train.txt             --output_file=../data/birth_place_train.jsonl             --vocab_file=hometinyzhaomengbertmodel/vocab.txt             --bert_config_file=hometinyzhaomengbertmodel/bert_config.json             --init_checkpoint=hometinyzhaomengbertmodel/bert_model.ckpt             --layers=-1             --max_seq_length=80             --batch_size=16

Next, convert the JSON files to a vector representation by executing json2vector.py. Finally, run run_classifier.py to apply machine learning techniques, where MLP (Multi-Layer Perceptron) typically yields the best results.

Results

The predicted results along with any misclassified entries are saved in the result directory. An example of precision, recall, and F1-score for the birthplace dataset using the fine-tuning method is as follows:

precision    recall  f1-score   support
           0      0.963     0.967     0.965       573
           1      0.951     0.946     0.948       389

Troubleshooting

If you encounter issues, here are a few things to consider:

  • Ensure compatibility of TensorFlow version with your model. Downgrading to version 1.10 may be necessary.
  • Check the correctness of the file paths specified in your commands.
  • If you have data inconsistency issues, revisit the data cleaning steps.
  • Optimize hyperparameters like learning rate and batch size for better model performance.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox