A PyTorch implementation of the BI-LSTM-CRF model.
Features:
- Compared with PyTorch BI-LSTM-CRF tutorial, following improvements are performed:
- Full support for mini-batch computation
- Full vectorized implementation. Specially, removing all loops in "score sentence" algorithm, which dramatically improve training performance
- CUDA supported
- Very simple APIs for CRF module
- START/STOP tags are automatically added in CRF
- A inner Linear Layer is included which transform from feature space to tag space
- Specialized for NLP sequence tagging tasks
- Easy to train your own sequence tagging models
- MIT License
Installation
- dependencies
- Python 3
- PyTorch
- install
$ pip install bi-lstm-crf
Training
corpus
- prepare your corpus in the specified structure and format
- there is also a sample corpus in
bi_lstm_crf/app/sample_corpus
training
$ python -m bi_lstm_crf corpus_dir --model_dir "model_xxx"
training curve
import pandas as pd
import matplotlib.pyplot as plt
# the training losses are saved in the model_dir
df = pd.read_csv(".../model_dir/loss.csv")
df[["train_loss", "val_loss"]].ffill().plot(grid=True)
plt.show()
Prediction
from bi_lstm_crf.app import WordsTagger
model = WordsTagger(model_dir="xxx")
tags, sequences = model(["市领导到成都..."]) # CHAR-based model
print(tags)
# [["B", "B", "I", "B", "B-LOC", "I-LOC", "I-LOC", "I-LOC", "I-LOC", "B", "I", "B", "I"]]
print(sequences)
# [['市', '领导', '到', ('成都', 'LOC'), ...]]
# model([["市", "领导", "到", "成都", ...]]) # WORD-based model
CRF Module
The CRF module can be easily embeded into other models:
from bi_lstm_crf import CRF
# a BERT-CRF model for sequence tagging
class BertCrf(nn.Module):
def __init__(self, ...):
...
self.bert = BERT(...)
self.crf = CRF(in_features, num_tags)
def loss(self, xs, tags):
features, = self.bert(xs)
masks = xs.gt(0)
loss = self.crf.loss(features, tags, masks)
return loss
def forward(self, xs):
features, = self.bert(xs)
masks = xs.gt(0)
scores, tag_seq = self.crf(features, masks)
return scores, tag_seq
References
- Zhiheng Huang, Wei Xu, and Kai Yu. 2015. Bidirectional LSTM-CRF Models for Sequence Tagging. arXiv:1508.01991.
- PyTorch tutorial ADVANCED: MAKING DYNAMIC DECISIONS AND THE BI-LSTM CRF
Metadata
Release files for bi-lstm-crf 0.2.0
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 |
|---|---|---|---|---|
| bi-lstm-crf-0.2.0.tar.gz | 11.1 kB | Without provenance | Details for bi-lstm-crf-0.2.0.tar.gz |
Release files / bi-lstm-crf-0.2.0.tar.gz
| Download URL | bi-lstm-crf-0.2.0.tar.gz |
|---|---|
| Size | 11.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cbe90e68066cc56e38f0b16ccafa292d6dc7f18d41d5363339aaa605cd877167
|
|
BLAKE2b-256 checksum How to use checksums |
13f4cf0aabd5d3af7c8815bbce11038f9a9ec5b5c2e5d2f1a311e51b138e2313
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0.post20200309 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6
|
Security for bi-lstm-crf 0.2.0
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.