PyTorch CRF with N-best decoding
Project description
PyTorch CRF with N-best Decoding
Implementation of Conditional Random Fields (CRF) in PyTorch 1.0. It supports top-N most probable paths decoding.
The package is based on pytorch-crf with only the following differences
- Method
_viterbi_decode
that decodes the most probable path get optimized. Running time gets reduced to 50% or less with batch size 15+ and sequence length 20+ - The class now supports decoding top-N most probable paths through the implementation of the method
_viterbi_decode_nbest
Requirements
- Python 3 (>= 3.6)
- PyTorch (>= 1.0)
Installation
pip install pytorchcrf
Examples
>>> import torch
>>> from pytorchcrf import CRF
>>> num_tags = 5 # number of tags is 5
>>> model = CRF(num_tags)
>>> seq_length = 3 # maximum sequence length in a batch
>>> batch_size = 2 # number of samples in the batch
>>> emissions = torch.randn(seq_length, batch_size, num_tags)
# Computing log likelihood
>>> tags = torch.tensor([[2, 3], [1, 0], [3, 4]], dtype=torch.long) # (seq_length, batch_size)
>>> model(emissions, tags)
# Decoding
>>> model.decode(emissions) # decoding the best path
>>> model.decode(emissions, nbest=3) # decoding the top 3 paths
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
pytorchcrf-1.1.0.tar.gz
(5.6 kB
view hashes)
Built Distribution
Close
Hashes for pytorchcrf-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b29fc8dcf198f488273c9a0ddc29ea783dd6ae0d653e4785f51269b6b1c17b49 |
|
MD5 | 7bed19346494537db71a02098ff13681 |
|
BLAKE2b-256 | f3e7e2d2ed8674167535e0130077ef87284144f5dab6699aef4e3e507bb543e2 |