Skip to main content

一行代码训练 BERT

Project description

pybert

安装

>pip install pybert

预训练模型

下载地址:

所需文件:

  • pytorch_model.bin
  • bert_config.json
  • vocab.txt

放到 bert_pretrain 文件夹中

训练数据下载

THUCNews

  • 可以任意指定文件夹名称,训练数据的格式要和上面一致

训练和预测

训练

from pybert.models import bert
from pybert.train_eval import load_and_train

dataset = 'THUCNews'  # 数据集
logfile = 'log.txt'  # 日志文件
config = bert.Config(dataset, logfile=logfile)
load_and_train(config)

预测

# coding: UTF-8
import pybert.models.bert as bert
from pybert.train_eval import Prediction

config = bert.Config(dataset='THUCNews')
prediction = Prediction(config)

sentences = ['野兽用纪录打爆第二中锋 掘金版三巨头已巍然成型', '56所高校预估2009年湖北录取分数线出炉']

predict_label, score = prediction.predict(sentences)
print("predict label:")
print(predict_label)

对应论文

[1] BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pybert-0.0.2.tar.gz (28.6 kB view hashes)

Uploaded Source

Built Distribution

pybert-0.0.2-py3-none-any.whl (29.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page