• Stars
    star
    465
  • Rank 93,649 (Top 2 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This is the template code to use BERT for sequence lableing and text classification, in order to facilitate BERT for more tasks. Currently, the template code has included conll-2003 named entity identification, Snips Slot Filling and Intent Prediction.

Template Code: BERT-for-Sequence-Labeling-and-Text-Classification

BERT is used for sequence annotation and text categorization template code to facilitate BERT for more tasks. The code has been tested on snips (intention recognition and slot filling task), ATIS (intention recognition and slot filling task) and conll-2003 (named entity recognition task) datasets. Welcome to use this BERT template to solve more NLP tasks, and then share your results and code here.

这是使用BERT进行序列标注和文本分类的模板代码,方便大家将BERT用于更多任务。该代码已经在SNIPS(意图识别和槽填充任务)、ATIS(意图识别和槽填充任务)和conll-2003(命名实体识别任务)数据集上进行了实验。欢迎使用这个BERT模板解决更多NLP任务,然后在这里分享你的结果和代码。

Task and Dataset

I have downloaded the data for you. Welcome to add new data set.

task name dataset name data source
CoNLL-2003 named entity recognition conll2003ner https://www.clips.uantwerpen.be/conll2003/ner/
Atis Joint Slot Filling and Intent Prediction atis https://github.com/MiuLab/SlotGated-SLU/tree/master/data/atis
Snips Joint Slot Filling and Intent Prediction snips https://github.com/MiuLab/SlotGated-SLU/tree/master/data/snips

Environment Requirements

Use pip install -r requirements.txt to install dependencies quickly.

  • python 3.6+
  • Tensorflow 1.12.0+
  • sklearn

Template Code Usage Method

Using pre training and fine-tuning model directly

For example: Atis Joint Slot Filling and Intent Prediction

  1. Download model weight atis_join_task_LSTM_epoch30_simple.zip and unzip then to file store_fine_tuned_model, https://pan.baidu.com/s/1SZkQXP8NrOtZKVEMfDE4bw;
  2. Run Code! You can change task_name and output_dir.
python run_slot_intent_join_task_LSTM.py \
  --task_name=Atis \
  --do_predict=true \
  --data_dir=data/atis_Intent_Detection_and_Slot_Filling \
  --vocab_file=pretrained_model/uncased_L-12_H-768_A-12/vocab.txt \
  --bert_config_file=pretrained_model/uncased_L-12_H-768_A-12/bert_config.json \
  --init_checkpoint=store_fine_tuned_model/atis_join_task_LSTM_epoch30_simple/model.ckpt-4198 \
  --max_seq_length=128 \
  --output_dir=./output_model_predict/atis_join_task_LSTM_epoch30_simple_ckpt4198

You can find the file of model prediction and the score of model prediction in output_dir (You can find the content of model socres later).

Quick start(model train and predict)

See predefined_task_usage.md for more predefined task usage codes.

  1. Move google's BERT code to file bert (I've prepared a copy for you.);
  2. Download google's BERT pretrained model and unzip then to file pretrained_model, https://github.com/google-research/bert;
  3. Run Code! You can change task_name and output_dir.

model training

python run_sequence_labeling_and_text_classification.py \
  --task_name=snips \
  --do_train=true \
  --do_eval=true \
  --data_dir=data/snips_Intent_Detection_and_Slot_Filling \
  --vocab_file=pretrained_model/uncased_L-12_H-768_A-12/vocab.txt \
  --bert_config_file=pretrained_model/uncased_L-12_H-768_A-12/bert_config.json \
  --init_checkpoint=pretrained_model/uncased_L-12_H-768_A-12/bert_model.ckpt \
  --num_train_epochs=3.0 \
  --output_dir=./store_fine_tuned_model/snips_join_task_epoch3/

Then you can find the fine tuned model in the output_dir=./store_fine_tuned_model/snips_join_task_epoch3/ folder.

model prediction

python run_sequence_labeling_and_text_classification.py \
  --task_name=Snips \
  --do_predict=true \
  --data_dir=data/snips_Intent_Detection_and_Slot_Filling \
  --vocab_file=pretrained_model/uncased_L-12_H-768_A-12/vocab.txt \
  --bert_config_file=pretrained_model/uncased_L-12_H-768_A-12/bert_config.json \
  --init_checkpoint=output_model/snips_join_task_epoch3/model.ckpt-1000 \
  --max_seq_length=128 \
  --output_dir=./output_model_prediction/snips_join_task_epoch3_ckpt1000

Then you can find the predicted output of the model and the output test results (accuracy, recall, F1 value, etc.) in the output_dir=./output_model_prediction/snips_join_task_epoch3_ckpt1000 folder.

File Structure

name function
bert store google's BERT code
data store task raw data set
output_model_prediction store model predict
store_fine_tuned_model store finet tuned model
calculating_model_score
pretrained_model store BERT pretrained model
run_sequence_labeling.py for Sequence Labeling Task
run_text_classification.py for Text Classification Task
run_sequence_labeling_and_text_classification.py for join task
calculate_model_score.py for evaluation model

Model Socres

The following model scores are model scores without careful adjustment of model parameters, that is to say, the scores can continue to improve!

CoNLL-2003 named entity recognition

eval_f = 0.926 eval_precision = 0.925 eval_recall = 0.928

Atis Joint Slot Filling and Intent Prediction

Intent Prediction Correct rate: 0.976 Accuracy: 0.976 Recall rate: 0.976 F1-score: 0.976

Slot Filling19 Correct rate: 0.955 Accuracy: 0.955 Recall rate: 0.955 F1-score: 0.955

How to add a new task

Just write a small piece of code according to the existing template!

Data

For example, If you have a new classification task QQP.

Before running this example you must download the GLUE data by running this script.

Code

Now, write code!

class QqpProcessor(DataProcessor):
    """Processor for the QQP data set."""

    def get_train_examples(self, data_dir):
        """See base class."""
        return self._create_examples(
            self._read_tsv(os.path.join(data_dir, "train.tsv")), "train")

    def get_dev_examples(self, data_dir):
        """See base class."""
        return self._create_examples(
            self._read_tsv(os.path.join(data_dir, "dev.tsv")), "dev")

    def get_test_examples(self, data_dir):
        """See base class."""
        return self._create_examples(
            self._read_tsv(os.path.join(data_dir, "test.tsv")), "test")

    def get_labels(self):
        """See base class."""
        return ["0", "1"]

    def _create_examples(self, lines, set_type):
        """Creates examples for the training and dev sets."""
        examples = []
        for (i, line) in enumerate(lines):
            if i == 0 or len(line)!=6:
                continue
            guid = "%s-%s" % (set_type, i)
            text_a = tokenization.convert_to_unicode(line[3])
            text_b = tokenization.convert_to_unicode(line[4])
            if set_type == "test":
                label = "1"
            else:
                label = tokenization.convert_to_unicode(line[5])
            examples.append(
                InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
        return examples

Registration task

def main(_):
   tf.logging.set_verbosity(tf.logging.INFO)
   processors = {
       "qqp": QqpProcessor,
   }

Run

python run_text_classification.py \
--task_name=qqp \
--do_train=true \
--do_eval=true \
--data_dir=data/snips_Intent_Detection_and_Slot_Filling \
--vocab_file=pretrained_model/uncased_L-12_H-768_A-12/vocab.txt \
--bert_config_file=pretrained_model/uncased_L-12_H-768_A-12/bert_config.json \
--init_checkpoint=pretrained_model/uncased_L-12_H-768_A-12/bert_model.ckpt \
--max_seq_length=128 \
--train_batch_size=32 \
--learning_rate=2e-5 \
--num_train_epochs=3.0 \
--output_dir=./output/qqp_Intent_Detection/

More Repositories

1

DeepImage-an-Image-to-Image-technology

DeepNude's algorithm and general image generation theory and practice research, including pix2pix, CycleGAN, UGATIT, DCGAN, SinGAN, ALAE, mGANprior, StarGAN-v2 and VAE models (TensorFlow2 implementation). DeepNude的算法以及通用生成对抗网络(GAN,Generative Adversarial Network)图像生成的理论与实践研究。
Python
5,168
star
2

Entity-Relation-Extraction

Entity and Relation Extraction Based on TensorFlow and BERT. 基于TensorFlow和BERT的管道式实体及关系抽取,2019语言与智能技术竞赛信息抽取任务解决方案。Schema based Knowledge Extraction, SKE 2019
Python
1,214
star
3

Machine-Learning-Book

《机器学习宝典》包含:谷歌机器学习速成课程(招式)+机器学习术语表(口诀)+机器学习规则(心得)+机器学习中的常识性问题 (内功)。该资源适用于机器学习、深度学习研究人员和爱好者参考!
Jupyter Notebook
1,031
star
4

BERT_Paper_Chinese_Translation

BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding 论文的中文翻译 Chinese Translation!
677
star
5

Multiple-Relations-Extraction-Only-Look-Once

Multiple-Relations-Extraction-Only-Look-Once. Just look at the sentence once and extract the multiple pairs of entities and their corresponding relations. 端到端联合多关系抽取模型,可用于 http://lic2019.ccf.org.cn/kg 信息抽取。
Python
345
star
6

Schema-based-Knowledge-Extraction

Code for http://lic2019.ccf.org.cn/kg 信息抽取。使用基于 BERT 的实体抽取和关系抽取的端到端的联合模型。
Python
283
star
7

Machine_Learning_bookshelf

机器学习深度学习相关书籍、课件、代码的仓库。 Machine learning is the warehouse of books, courseware and codes.
Jupyter Notebook
189
star
8

Multimodal-short-video-dataset-and-baseline-classification-model

500,000 multimodal short video data and baseline models. 50万条多模态短视频数据集和基线模型(TensorFlow2.0)。
Jupyter Notebook
125
star
9

Theoretical-Proof-of-Neural-Network-Model-and-Implementation-Based-on-Numpy

This resource implements a deep neural network through Numpy, and is equipped with easy-to-understand theoretical derivation, mainly for the in-depth understanding of neural networks. 神经网络模型的理论证明与基于Numpy的实现。
Python
77
star
10

Find-a-Machine-Learning-Job

找一份机器学习工作(算法工程师),需要提纲(算法能力)挈领(编程能力),充分准备。 本人学习和在找工作期间受到了很多前辈们的帮助,目前已经找到心仪的工作,撰写此文献给那些在求职路上有梦有汗水的人们!2020秋招算法,难度剧增!没有选择,只能迎难而上。
65
star
11

XLNet_Paper_Chinese_Translation

XLNet: Generalized Autoregressive Pretraining for Language Understanding 论文的中文翻译 Paper Chinese Translation!
50
star
12

Slot-Filling-and-Intention-Prediction-in-Paper-Translation

槽填充、意图预测(口语理解)论文整理和中文翻译。Slot filling and intent prediction paper collation and Chinese translation.
49
star
13

SMP2018

SMP2018中文人机对话技术评测(ECDT)
Jupyter Notebook
47
star
14

fan-ren-xiu-xian-zhuan

凡人修仙传(fanrenxiuxianzhuan)的资源汇总,谨献给“凡友”们。
Python
45
star
15

Image-Captioning

CNN-Encoder and RNN-Decoder (Bahdanau Attention) for image caption or image to text on MS-COCO dataset. 图片描述
Jupyter Notebook
35
star
16

ELMo

ELMo: Embeddings from Language Models. Using, visualizing and understanding EMLo by examples!
Jupyter Notebook
32
star
17

Text-generation-task-and-language-model-GPT2

solve text generation tasks by the language model GPT2, including papers, code, demo demos, and hands-on tutorials. 使用语言模型GPT2来解决文本生成任务的资源,包括论文、代码、展示demo和动手教程。
29
star
18

Transformer_implementation_and_application

The 300 lines of code (Tensorflow 2) completely replicates the Transformer model and is used in neural machine translation tasks and chat bots. 300行代码(Tensorflow 2)完整复现了Transformer模型,并且应用在神经机器翻译任务和聊天机器人上。
Jupyter Notebook
26
star
19

yuanxiaosc.github.io

个人博客;论文;机器学习;深度学习;Python学习;C++学习;
HTML
21
star
20

CPlusPlus-Programming-Language-Foundation

《CPlusPlus编程语言基础》又称为“C加加知识树”,用树状思维导图的形式展现C++从业人员必备的所有C++基础知识。
21
star
21

Keras_Attention_Seq2Seq

A sequence-to-sequence framework of Keras-based generative attention mechanisms that humans can read.一个人类可以阅读的基于Keras的代注意力机制的序列到序列的框架/模型,或许你不用写复杂的代码,直接使用吧。
Python
18
star
22

Deep_dynamic_contextualized_word_representation

TensorFlow code and pre-trained models for A Dynamic Word Representation Model Based on Deep Context. It combines the idea of BERT model and ELMo's deep context word representation.
Python
16
star
23

Path-Classification-Experiment

Introduction to Data Analysis: Path Classification Experiment. 本资源以选择最优路径为例详细介绍了如何解决一般的分类问题,包括原始数据的探索、模型的构建、模型调优和模型预测分析。包含前馈神经网络(Keras)、机器学习模型(sklearn)和绘制数据图表(matplotlib)的基础使用。
Jupyter Notebook
13
star
24

Deep-Convolutional-Generative-Adversarial-Network

Tensorflow 2. This repository demonstrates how to generate images of handwritten digits (MINIST) using a Deep Convolutional Generative Adversarial Network (DCGAN). 深度卷积生成对抗网络
Jupyter Notebook
9
star
25

NLPCC2019-Conference-Materials

NLPCC2019会议资料分享:论文投稿信息总结、NLP当前研究内容和趋势、学者演讲、海报、公司介绍和招聘信息。
7
star
26

Slot-Gated-Modeling-for-Joint-Slot-Filling-and-Intent-Prediction

Code parsing and paper parsing for "Slot-Gated Modeling for Joint Slot Filling and Intent Prediction"
Python
5
star
27

Image_to_Text

Taking the image description task on the MS-COCO data set as an example, the template code of Image_to_Text is shown.
Jupyter Notebook
5
star
28

Seq2Seq-English-French-Machine-Translation-Model

Seq2Seq English-French Machine Translation Model
Python
5
star
29

Hands-on-chat-robots

There are a variety of out-of-the-box chat bot codes here.
Jupyter Notebook
3
star