torchKbert
- Our customized version of bert for pytorch
说明
这是笔者基于 Meelfy 的 pytorch_pretrained_BERT 库进行部分定制化修改的模型库。
本项目的初衷是为了满足个人实验的方便,因此不会经常更新。
功能
- 原始的模型库 pytorch_pretrained_BERT 中的功能仍然支持;
- 支持层次分解位置编码。
- 支持基于词颗粒度的woBERT。
Pytorch 权重(这里提供的是WoBERT Plus模型):
- chinese_wobert_plus.zip(提取码: fg6j)
使用
-
安装:
pip install torchKbert
-
典型的使用例子请参考官方 examples 目录。
-
若想使用层次分解位置编码,使 BERT 可以处理长文本,在
model中传入参数is_hierarchical=True即可。示例如下:model = BertModel(config) encoder_outputs, _ = model(input_ids, token_ids, input_mask, is_hierarchical=True) -
若想使用基于词颗粒度的中文WoBERT,只需在构建
BertTokenizer对象时传入新参数:from torchKbert.tokenization import BertTokenizer tokenizer = BertTokenizer( vocab_file=vocab_path, pre_tokenizer=lambda s: jieba.cut(s, HMM=False))不传入时,默认为
None。分词时默认以词为单位,若想恢复使用以字为单位,只需在tokenize时传入新参数pre_tokenize=False:tokenzier.tokenize(text, pre_tokenize=False)
背景
之前一直在用 Meelfy 编写的 pytorch_pretrained_BERT,调用预训练模型或进行微调已经十分方便。后来因个人的需求,所以就想改写一个支持层次分解位置编码的版本。
苏神的 bert4keras 已经实现了这样的功能。但因个人惯于使用 pytorch,已经很久不用 keras 了,所以才打算自己改写一个。
更新
- 2021.03.07 : 添加层次分解位置编码。
- 2021.05.27 : 添加基于词颗粒度的中文WoBERT。
- 2022.03.27 : 参照 pytorch_transformers 对 BertPretrainedModel 代码实现进行了重构。
参考
- 感谢 Meelfy 实现的 pytorch_pretrained_BERT,本实现完全基于 pytorch_pretrained_BERT 的源码。
- 感谢苏神的 insight 和无私分享:层次分解位置编码,让BERT可以处理超长文本。
- WoBERT: Word-based Chinese BERT model - ZhuiyiAI。
Metadata
Release files for torchKbert 1.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torchKbert-1.1.3.tar.gz | 71.3 kB | Without provenance | Details for torchKbert-1.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torchKbert-1.1.3-py3-none-any.whl | Python 3 | none | any | Details for torchKbert-1.1.3-py3-none-any.whl |
Total release size: 159.7 kB
Release files / torchKbert-1.1.3.tar.gz
| Download URL | torchKbert-1.1.3.tar.gz |
|---|---|
| Size | 71.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
454e0221d3277d8116e2f70f3bdc18b5780562386663460c2b4509bf2f6e0d77
|
|
BLAKE2b-256 checksum How to use checksums |
b525ea8bd2e973f893e5946a672b676120222d42adf88540475b87505d389a06
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/58.1.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3
|
Release files / torchKbert-1.1.3-py3-none-any.whl
| Download URL | torchKbert-1.1.3-py3-none-any.whl |
|---|---|
| Size | 88.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a26610eea4e9a906a56023d2bc928ec7da62ed488240bf9a3db30bd4d69db2fd
|
|
BLAKE2b-256 checksum How to use checksums |
8b13fb89e679a1ec720e1852aa246ff398bc5e08579b72ea36a3eb7a06e09887
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/58.1.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3
|
Security for torchKbert 1.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.