Skip to main content

No project description provided

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytorch-partial-crf-0.0.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

pytorch_partial_crf-0.0.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file pytorch-partial-crf-0.0.0.tar.gz.

File metadata

  • Download URL: pytorch-partial-crf-0.0.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0a2 CPython/3.9.8 Linux/5.11.0-1020-azure

File hashes

Hashes for pytorch-partial-crf-0.0.0.tar.gz
Algorithm Hash digest
SHA256 39734a258d4093dac12f857bd0fe080e5ab9d04367a739795451dc1bafa466d4
MD5 8de683be4cd909c3654a69855eef07b3
BLAKE2b-256 6f44caa2384b86d773992d5a6edf36ed4077e40cdb3665b9d9611a144f7194cb

See more details on using hashes here.

File details

Details for the file pytorch_partial_crf-0.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytorch_partial_crf-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b2c21959c655bb1a988f7c2368e9e9f18d4b7b031cd5c771d4787afbfdcadc50
MD5 b01de40f71a54be7db1cd183a9345f27
BLAKE2b-256 be2b124950ed669c689b99e837b58c3bb4f3eaefa0989fd83e383ed7c614f4bd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page