Skip to main content

Torch CRF

CircleCI Coverage Status MIT License

Python Versions PyPI version

Implementation of CRF (Conditional Random Fields) in PyTorch

Requirements

  • python3 (>=3.6)
  • PyTorch (>=1.0)

Installation

$ pip install TorchCRF

Usage

>>> import torch
>>> from TorchCRF import CRF
>>> device = "cuda" if torch.cuda.is_available() else "cpu"
>>> batch_size = 2
>>> sequence_size = 3
>>> num_labels = 5
>>> mask = torch.ByteTensor([[1, 1, 1], [1, 1, 0]]).to(device) # (batch_size. sequence_size)
>>> labels = torch.LongTensor([[0, 2, 3], [1, 4, 1]]).to(device)  # (batch_size, sequence_size)
>>> hidden = torch.randn((batch_size, sequence_size, num_labels), requires_grad=True).to(device)
>>> crf = CRF(num_labels)

Computing log-likelihood (used where forward)

>>> crf.forward(hidden, labels, mask)
tensor([-7.6204, -3.6124], device='cuda:0', grad_fn=<ThSubBackward>)

Decoding (predict labels of sequences)

>>> crf.viterbi_decode(hidden, mask)
[[0, 2, 2], [4, 0]]

License

MIT

References

Metadata

Release files for TorchCRF 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for TorchCRF 1.1.0
File Size Uploaded Provenance Actions
TorchCRF-1.1.0.tar.gz 5.4 kB Without provenance Details for TorchCRF-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for TorchCRF 1.1.0
File Interpreter ABI Platform Actions
TorchCRF-1.1.0-py3-none-any.whl Python 3 none any Details for TorchCRF-1.1.0-py3-none-any.whl

Total release size: 10.6 kB

Release files / TorchCRF-1.1.0.tar.gz

Download URL TorchCRF-1.1.0.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
c916064e9b498a9f238a5f733e510ec3f271fd482e2970563a675cbf390d34fb
BLAKE2b-256 checksum
How to use checksums
a4b81b7568b7e84d0439723f7a972cd4b3007e606a66e3d81ada5f61c131d29f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.0

Release files / TorchCRF-1.1.0-py3-none-any.whl

Download URL TorchCRF-1.1.0-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
540f1a61ce94fa3c247719e2754a55e1cdd0e75b42f838ec025cfe158afd9151
BLAKE2b-256 checksum
How to use checksums
0e953490a6c67d543aca9af21a7a13197750e18e975a215fa4b6f5411902908d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.0

Security for TorchCRF 1.1.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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page