A PyTorch implementation of the BI-LSTM-CRF model.
Python
263
6 commits
updated Oct 30, 2024
A PyTorch implementation of the BI-LSTM-CRF model.
$ pip install bi-lstm-crf
bi_lstm_crf/app/sample_corpus$ python -m bi_lstm_crf corpus_dir --model_dir "model_xxx"
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()
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
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
Overall
Maintained
Code review
PyPI
Latest release
Releases this year
A PyTorch implementation of the BI-LSTM-CRF model.
Python
263
6 commits
updated Oct 30, 2024
A PyTorch implementation of the BI-LSTM-CRF model.
$ pip install bi-lstm-crf
bi_lstm_crf/app/sample_corpus$ python -m bi_lstm_crf corpus_dir --model_dir "model_xxx"
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()
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
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
Overall
Maintained
Code review
PyPI
Latest release
Releases this year