"Partial/Fuzzy Conditional random field in PyTorch."
Project description
pytorch-partial-crf
Partial/Fuzzy conditional random field in PyTorch.
How to use
Install
pip install pytorch-partial-crf
Use CRF
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
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
model.viterbi_decode(emissions)
Restricted viterbi decode
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
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
Close
Hashes for pytorch-partial-crf-0.1.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b694c3b3376fd16ba3ce8a621fddbcfdc840b7619beefbe43d15d4ec2db86350 |
|
MD5 | a774f9d43ea6b1c376c4e7abe83dc76a |
|
BLAKE2b-256 | ff2e78bd1d425d403d1d0e733b5202c53b5933a657754995239d214981f79ee0 |