Torch CRF
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)
| File | Size | Uploaded | |
|---|---|---|---|
| TorchCRF-1.1.0.tar.gz | 5.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| TorchCRF-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
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
|