An Implementation of Conditional Random Fields in pytorch
Project description
Torch CRF
Implementation of CRF (Conditional Random Fields) in PyTorch 1.0
Requirements
- python3 (>=3.6)
- PyTorch 1.0
Installation
$ pip install TorchCRF
Usage
>>> import torch
>>> from TorchCRF import CRF
>>> batch_size = 2
>>> sequence_size = 3
>>> num_labels = 5
>>> mask = torch.FloatTensor([[1, 1, 1], [1, 1, 0]]) # (batch_size. sequence_size)
>>> labels = torch.LongTensor([[0, 2, 3], [1, 4, 1]]) # (batch_size, sequence_size)
>>> hidden = torch.randn((batch_size, sequence_size, num_labels), requires_grad=True)
>>> crf = CRF(num_labels)
Computing log-likelihood (used where forward)
>>> crf.forward(hidden, labels, mask)
tensor([-7.6204, -3.6124], 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.2.tar.gz
(5.6 kB
view details)
Built Distribution
File details
Details for the file TorchCRF-1.0.2.tar.gz
.
File metadata
- Download URL: TorchCRF-1.0.2.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 692fc9e79a5a76e6db5e012e14dd439a7a119e231231744d9f256424f9674235 |
|
MD5 | e1fd558bffd20e0bdb1196be2d3560a1 |
|
BLAKE2b-256 | ab27a9eb63e22ccd4a8176723230a93c7ff9f57750e0d43f4530fabbaf9d28de |
File details
Details for the file TorchCRF-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: TorchCRF-1.0.2-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9550b84cf97aecde9185102746c50d9b234cd932c02608624b524ec87833cd1a |
|
MD5 | acb5a485a3ae9b8441e8fb36d901dfbd |
|
BLAKE2b-256 | b92bd072ca804258eaf8bb0fbf3a4bb3909821422f3d247f889116c81444dc98 |