Skip to main content

pytorch-partial-crf

Partial/Fuzzy conditional random field in PyTorch.

Document: https://pytorch-partial-crf.readthedocs.io/

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 negative 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 negative log likelihood
model(emissions, tags)

Use Marginal CRF

import torch
from pytorch_partial_crf import MarginalCRF

# Create 
num_tags = 6
model = MarginalCRF(num_tags)

batch_size, sequence_length = 3, 5
emissions = torch.randn(batch_size, sequence_length, num_tags)

# Set probability tags
marginal_tags = torch.Tensor([
        [
            [0.2, 0.2, 0.2, 0.1, 0.1, 0.2],
            [0.8, 0.0, 0.0, 0.1, 0.1, 0.0],
            [0.0, 0.0, 0.0, 0.0, 1.0, 0.0],
            [0.3, 0.0, 0.0, 0.1, 0.6, 0.0],
        ],
        [
            [0.2, 0.2, 0.2, 0.1, 0.1, 0.2],
            [0.8, 0.0, 0.0, 0.1, 0.1, 0.0],
            [0.0, 0.0, 0.0, 0.0, 1.0, 0.0],
            [0.3, 0.0, 0.0, 0.1, 0.6, 0.0],
        ],
        [
            [0.2, 0.2, 0.2, 0.1, 0.1, 0.2],
            [0.8, 0.0, 0.0, 0.1, 0.1, 0.0],
            [0.0, 0.0, 0.0, 0.0, 1.0, 0.0],
            [0.3, 0.0, 0.0, 0.1, 0.6, 0.0],
        ],
])
# Computing negative log likelihood
model(emissions, marginal_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)

Contributing

We welcome contributions! Please post your requests and comments on Issue.

License

MIT

References

The implementation is based on AllenNLP CRF module and pytorch-crf.

Metadata

Release files for pytorch-partial-crf 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytorch-partial-crf 0.2.1
File Size Uploaded
pytorch-partial-crf-0.2.1.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytorch-partial-crf 0.2.1
File Interpreter ABI Platform
pytorch_partial_crf-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.4 kB

Release files / pytorch-partial-crf-0.2.1.tar.gz

Download URL pytorch-partial-crf-0.2.1.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5184dd339f39119112cf66215f055c22eff7d742b85ebc6cdd3d67d707221422
BLAKE2b-256 checksum
How to use checksums
4a73cad3ac906542222e58108287d786461cacf6953f1b41428ae0d9a54e4416
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0a2 CPython/3.9.8 Linux/5.11.0-1020-azure

Release files / pytorch_partial_crf-0.2.1-py3-none-any.whl

Download URL pytorch_partial_crf-0.2.1-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
670aefbfac7d2cd5762b15bab23ed57eff972a682f056834a4b871981cfb2caf
BLAKE2b-256 checksum
How to use checksums
03b5281cdacbc5729e1a355f65da973b6f2b7852a5d68f93954135525bcaa8e9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0a2 CPython/3.9.8 Linux/5.11.0-1020-azure

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page