Linear CRF
Description
This repository hosts my Pytorch implementation of the Linear Conditional Random Field model, which is available via PyPi. There are a number of similar packages, however at this point I can say this one is faster (or equivalent), as I benchmarked the alternatives.
Origin story
About a year ago I needed this for a project I was working on, and stumbled upon the de facto official implementation (at least by star count), which you can check here. Now although the code was already quite good I decided to optimize it for my needs, and ended up with significant gains. I decided to open a PR in order to share, but quickly realized two thjings. The first was that I had commited way too much, and should have consulted with the author first. The second was that some changes actually went against the wants of the author, as they didn't meet his readability standard. Anyway, long story short I got demotivated, then forgot about it, and the PR of shame is still open >_<"
However the story does not stop here ! As I needed to use a CRF for another project recently, I decided to clean up my code, and ended up optimizing it even further. As I like how it looks, but learned my lesson, I decided to release it, and voila !
Installation
With Python 3.6 or higher:
pip install linear-crf-torch
The model is not compatible with Pytorch versions older then 1.3, as I use features added from that version. The required changes are minimal, si I don't plan to include them.
Usage
The example below shows the basic usage:
import torch
from linear_crf import LinearCRF
seq_length = 3
batch_size = 4
num_tags = 5
model = LinearCRF(num_tags)
emissions = torch.randn(seq_length, batch_size, num_tags)
labels = torch.randint(num_tags, (seq_length, batch_size))
# Compute the average negative log-likelihood
loss = model(emissions, labels)
print(f"loss: {loss:.4f}")
# Viterbi decoding
tags = model.decode(emissions)
for i, x in enumerate(tags):
print(f"tags for sequence {i}: {x}")
A couple caveats:
- I followed the Pytorch convention of setting the batch dimension after the sequence one, but you can set
batch_first=Truein the constructor if you wish to pass data the other way around. - Unlike similar packages, no input validation is performed - I think the documentation should be enough to avoid any bugs.
- Using the
impossible_starts,impossible_transitionsandimpossible_endsparameters in the constructor, you can make it impossible for certain tags to appear at the start or end of the sequences, and make transitions from one tag to another impossible. - In the forward pass, the loss is normalized by the number of non-masked elements. It doesn't make sense to normalize in an other way, neither does directly using the sum.
- Gradients are disabled during decoding.
- Masking is only supported from the right, meaning if you mask the left part of a sentence (e.g.
[0, 0, 1, 1]) the computations will be incorrect.
License
Benchmarks
Over here.
Contributing
All help is welcome, as long as you open an issue beforehand to talk about it :)
Reference
Conditional Random Fields: Probabilistic Modelsfor Segmenting and Labeling Sequence Data by John Lafferty, Andrew McCallum and Fernando C.N. Pereira
Metadata
Release files for linear-crf-torch 0.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 | |
|---|---|---|---|---|
| linear-crf-torch-0.0.1.tar.gz | 5.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| linear_crf_torch-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.0 kB
Release files / linear-crf-torch-0.0.1.tar.gz
| Download URL | linear-crf-torch-0.0.1.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6f68e85cd626650f0bbf3a01a85eafe37b5fd0511ec186da821966895203c3ba
|
|
BLAKE2b-256 checksum How to use checksums |
3e3533797ae74ecb76ec70d202b6df99ab17f382f6d759e8c23131e9c15ca4f6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.9
|
Release files / linear_crf_torch-0.0.1-py3-none-any.whl
| Download URL | linear_crf_torch-0.0.1-py3-none-any.whl |
|---|---|
| Size | 5.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
62d11aa72a68b84965ebd2baeeced01dd1b106c667d3d614edc43092bd9927d8
|
|
BLAKE2b-256 checksum How to use checksums |
ab17509626d4fb6c14386476a1fd901802414b39a3fb987312de7dc4184b9d2a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.9
|
Security for linear-crf-torch 0.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.