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
```python
>>> 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)
```python
>>> crf.forward(hidden, labels, mask)
tensor([-7.6204, -3.6124], device='cuda:0', grad_fn=<ThSubBackward>)
```
Decoding (predict labels of sequences)
```python
>>> 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.5.tar.gz
(5.9 kB
view details)
Built Distribution
File details
Details for the file TorchCRF-1.0.5.tar.gz
.
File metadata
- Download URL: TorchCRF-1.0.5.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 | b6fb9f03e5cbf20095bdb329cf2ee2c5921c3bdd8daf105ca02a129dcca5d3ca |
|
MD5 | 33e05d5ca5915b2333a0db115ed085de |
|
BLAKE2b-256 | af91c3946a49770368f8d9afff90dcc530728a582309a745c1dc0cc65f8c60bb |
File details
Details for the file TorchCRF-1.0.5-py3-none-any.whl
.
File metadata
- Download URL: TorchCRF-1.0.5-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 | 83c05c2d2fd2820620ed567ce1542aa58c1afc5b3957025f1cb2d7f1054d6112 |
|
MD5 | cd0f6d9f14a0017ac49afad67be2b64e |
|
BLAKE2b-256 | a3e820a7ed08b46786a9b50ab209a7a5cd5d69597008cc9365fb38d64cf95263 |