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.0.6.tar.gz
(5.9 kB
view details)
Built Distribution
File details
Details for the file TorchCRF-1.0.6.tar.gz
.
File metadata
- Download URL: TorchCRF-1.0.6.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12e66f9e961ab633f2a65f5ed87b04796afd7534ab40d4451f531f7c99e610aa |
|
MD5 | 03473e9264d38ffec90b86403baf05d0 |
|
BLAKE2b-256 | ca3069e515117a1840daf2a29a6e64c51de6c74151d46651f2fd988e0e275603 |
File details
Details for the file TorchCRF-1.0.6-py3-none-any.whl
.
File metadata
- Download URL: TorchCRF-1.0.6-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2be49566a571abdb82c14cb2177c6cd6db0e56a80668b4f2081e55294f17844 |
|
MD5 | 6a9234858aa8d4887ddd02b49b66e815 |
|
BLAKE2b-256 | c255fc9e720d2d150818582b664627590f61138c076958f3868b6dbcd119c573 |