PyTorch Text CRF
This package contains a simple wrapper for using conditional random fields(CRF). This code is based on the excellent Allen NLP implementation of CRF.
Installation
pip install pytorch-text-crf
Usage
from crf.crf import ConditionalRandomField
# Initilization
crf = ConditionalRandomField(n_tags,
label_encoding="BIO",
idx2tag={0:"B-GEO", 1:"I-GEO", 2:"0"} # Index to tag mapping
)
# Likelihood estimation
log_likelihood = crf(logits, tags, mask)
# Decoding
best_tag_sequence = crf.best_viterbi_tag(logits, mask)
top_5_viterbi_tags = crf.viterbi_tags(logits, mask, top_k=5)
LSTM CRF Implementation
Refer to https://github.com/iamsimha/pytorch-text-crf/blob/master/examples/pos_tagging/train.ipynb for a complete working implementation.
from crf.crf import ConditionalRandomField
class LSTMCRF:
"""
An Example implementation for using a CRF model on top of LSTM.
"""
def __init__(self):
...
...
# Initilize the conditional CRF model
self.crf = ConditionalRandomField(
n_class, # Number of tags
label_encoding="BIO", # Label encoding format
idx2tag=idx2tag # Dict mapping index to a tag
)
def forward(self, inputs, tags):
logits = self.lstm(inputs) # logits dim:(batch_size, seq_length, num_tags)
mask = inputs != "<pad token>" # mask for ignoring pad tokens. mask dim: (batch_size, seq_length)
log_likelihood = self.crf(logits, tags, mask)
loss = -log_likelihood # Log likelihood is not normalized (It is not divided by the batch size).
# To obtain the best sequence using viterbi decoding
best_tag_sequence = self.crf.best_viterbi_tag(logits, mask)
# To obtain output similar to the lstm prediction we can use the below code
class_probabilities = out * 0.0
for i, instance_tags in enumerate(best_tag_sequence):
for j, tag_id in enumerate(instance_tags[0][0]):
class_probabilities[i, j, int(tag_id)] = 1
return {"loss": loss, "class_probabilities": class_probabilities}
# Training
lstm_crf = LSTMCRF()
output = lstm_crf(sentences, tags)
loss = output["loss"]
loss.backward()
optimizer.step()
Metadata
Release files for pytorch-text-crf 0.1
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 |
|---|---|---|---|---|
| pytorch-text-crf-0.1.tar.gz | 10.0 kB | Without provenance | Details for pytorch-text-crf-0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytorch_text_crf-0.1-py3-none-any.whl | Python 3 | none | any | Details for pytorch_text_crf-0.1-py3-none-any.whl |
Total release size: 24.0 kB
Release files / pytorch-text-crf-0.1.tar.gz
| Download URL | pytorch-text-crf-0.1.tar.gz |
|---|---|
| Size | 10.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bab37a78d9d2c0f62c1ae82f3a8466af9c64df0d01bea0eb4f357c5b3e0ebdc7
|
|
BLAKE2b-256 checksum How to use checksums |
df651e6b577211ad2ffeee49352bf0b6b16ede29bae10c54f5a7f45aa423e958
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
|
Release files / pytorch_text_crf-0.1-py3-none-any.whl
| Download URL | pytorch_text_crf-0.1-py3-none-any.whl |
|---|---|
| Size | 14.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5000a5b68ed82fc8551362b6c0a6e25582553bccef4fe687e188de1b72ec7398
|
|
BLAKE2b-256 checksum How to use checksums |
17aea8f42b712dc3d16953d088be32b5e8a5c1515acb5006a4509fb909f0344f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
|
Security for pytorch-text-crf 0.1
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.