"Partial/Fuzzy Conditional random field in PyTorch."
Project description
pytorch-partial-crf
Partial/Fuzzy conditional random field in PyTorch.
How to use
Install
.. code-block:: shell
pip install pytorch-partial-crf
Use CRF
.. code-block:: python
import torch
from pytorch_partial_crf import CRF
# Create
num_tags = 6
model = CRF(num_tags)
batch_size, sequence_length = 3, 5
emissions = torch.randn(batch_size, sequence_length, num_tags)
tags = torch.LongTensor([
[1, 2, 3, 3, 5],
[1, 3, 4, 2, 1],
[1, 0, 2, 4, 4],
])
# Computing log likelihood
model(emissions, tags)
Use partial CRF
.. code-block:: python
import torch
from pytorch_partial_crf import PartialCRF
# Create
num_tags = 6
model = PartialCRF(num_tags)
batch_size, sequence_length = 3, 5
emissions = torch.randn(batch_size, sequence_length, num_tags)
# Set unknown tag to -1
tags = torch.LongTensor([
[1, 2, 3, 3, 5],
[-1, 3, 3, 2, -1],
[-1, 0, -1, -1, 4],
])
# Computing log likelihood
model(emissions, tags)
Decoding
Viterbi decode
.. code-block:: python
model.viterbi_decode(emissions)
Restricted viterbi decode
.. code-block:: python
possible_tags = torch.randn(batch_size, sequence_length, num_tags)
possible_tags[possible_tags <= 0] = 0 # `0` express that can not pass.
possible_tags[possible_tags > 0] = 1 # `1` express that can pass.
possible_tags = possible_tags.byte()
model.restricted_viterbi_decode(emissions, possible_tags)
Marginal probabilities
.. code-block:: python
model.marginal_probabilities(emissions)
License
MIT
References
The implementation is based on AllenNLP CRF module and pytorch-crf.
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
File details
Details for the file pytorch-partial-crf-0.1.1.tar.gz
.
File metadata
- Download URL: pytorch-partial-crf-0.1.1.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12bed6a0affe7e4a7b4870becd32cc8b09c5711a72aeaa7765d1332b72d68ea0 |
|
MD5 | 126ec93437911592784b8bfa5d2bbb25 |
|
BLAKE2b-256 | 069f446d3de0a44c59ba36baf00135882d3383b95fab4a4e8bb946848579d6d7 |