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.8.tar.gz
(5.1 kB
view details)
Built Distribution
File details
Details for the file TorchCRF-1.0.8.tar.gz
.
File metadata
- Download URL: TorchCRF-1.0.8.tar.gz
- Upload date:
- Size: 5.1 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 | 0e1881ffd74bdbe858343c864015515ec254a9873e4da4edeab2a8bc963533dd |
|
MD5 | 6d26bedda7d0a714cf4b0df707f416d8 |
|
BLAKE2b-256 | 1a31d02b857f7e124b1fec100127758a5129254312f286ac29e89a7ce5296ca6 |
File details
Details for the file TorchCRF-1.0.8-py3-none-any.whl
.
File metadata
- Download URL: TorchCRF-1.0.8-py3-none-any.whl
- Upload date:
- Size: 5.2 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 | bf8e85dcd23c8d4ce81acc5e39e7df91651eddc9d76a8adacc9d21a038c1c2f2 |
|
MD5 | 2166b708ba980842ad2c1d3f72f292f8 |
|
BLAKE2b-256 | 756085c09369f4684b556b034aa20e8906c0f20712701999b8d57e3c0c02f315 |