Code Monkey home page Code Monkey logo

bert_for_text_matching's Introduction

bert_for_text_matching

该仓库以文本匹配任务为例展示了如何使用Bert预训练模型在特定领域语料上进行微调,并使用tensorflow serving部署在生产环境中用做推理。

What I did:

  • 支持文本匹配任务
  • Bert官方代码库使用了大量TPU相关逻辑,这里做了相应简化
  • 使用标准的tf.data和tf.estimator api构建模型
  • 使用tensorflow serving将模型部署到生产环境

Data

采用LCQMC中文文本匹配数据集作为模型微调对象。支持的训练方式为pointwise,句子对0/1二分类。

Requirements

python 3

tensorflow 1.12.0

docker (for tensorflow serving)

Usage

step 1 领域数据微调

export BERT_BASE_DIR=/path/to/bert/chinese_L-12_H-768_A-12

python train.py \
  --do_train=true \
  --do_eval=true \
  --do_predict=true \
  --data_dir=/path/to/your/data \
  --vocab_file=$BERT_BASE_DIR/vocab.txt \
  --bert_config_file=$BERT_BASE_DIR/bert_config.json \
  --init_checkpoint=$BERT_BASE_DIR/bert_model.ckpt \
  --max_seq_length=64 \
  --train_batch_size=32 \
  --learning_rate=2e-5 \
  --num_train_epochs=3 \
  --output_dir=/path/for/output/

step 2 导出模型

使用tensorflow serving部署之前需要将模型从checkpoint导出为saved_model格式。

python export.py \
  -c path/for/bert_config\
  -m path/to/checkpoints \
  -o path/for/saved_model 

step 3 部署模型

  • 安装docker,并拉取tensorflow serving镜像(若使用GPU加速,还需安装nvidia-docker)

    docker pull tensorflow/serving:1.12.0-gpu
    
  • 启动容器服务,对外提供rest接口

    docker run -p 8501:8501 \
      --mount type=bind,source=path/to/your/local/saved_models,target=/models \
      -e MODEL_NAME=serving_model -t tensorflow/serving:1.12.0-gpu

Reporting issues

Please let me know, if you encounter any problems.

bert_for_text_matching's People

Contributors

zhongbin1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

bert_for_text_matching's Issues

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.