An Implementation of Conditional Random Fields in pytorch
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
TorchCRF-1.1.0.tar.gz
(5.4 kB
view details)
Built Distribution
File details
Details for the file TorchCRF-1.1.0.tar.gz
.
File metadata
- Download URL: TorchCRF-1.1.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c916064e9b498a9f238a5f733e510ec3f271fd482e2970563a675cbf390d34fb |
|
MD5 | 76a0eae67c1c0f4c04820cccdddb573f |
|
BLAKE2b-256 | a4b81b7568b7e84d0439723f7a972cd4b3007e606a66e3d81ada5f61c131d29f |
File details
Details for the file TorchCRF-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: TorchCRF-1.1.0-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 540f1a61ce94fa3c247719e2754a55e1cdd0e75b42f838ec025cfe158afd9151 |
|
MD5 | b65f005509040701d9943b40d8f48e34 |
|
BLAKE2b-256 | 0e953490a6c67d543aca9af21a7a13197750e18e975a215fa4b6f5411902908d |