Skip to main content

"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)

Use viterbi decode

import torch
from pytorch_partial_crf import CRF
from pytorch_partial_crf import PartialCRF


num_tags = 6
model = CRF(num_tags) # or FuzzyCRF

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

model.viterbi_decode(emissions)

# restricted viterbi decode
possible_tags = torch.randn(batch_size, sequence_length, num_tags)
possible_tags[possible_tags<=0] = 0
possible_tags[possible_tags>0] = 1
possible_tags = possible_tags.byte()  

model.restricted_viterbi_decode(emissions, possible_tags)

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.10.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

pytorch_partial_crf-0.0.10-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytorch-partial-crf-0.0.10.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for pytorch-partial-crf-0.0.10.tar.gz
Algorithm Hash digest
SHA256 baa7c9289504fbc24a8800f0c7ad400076076cdacaacef4d1c949ff8dded3069
MD5 c83270ca7cd0658f86198245eb37b258
BLAKE2b-256 015970c91671c8a4a612255b34e29345d5a63568da281f43cb2737432664501b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytorch_partial_crf-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for pytorch_partial_crf-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 db31861092e3435abe0d5cb1829454bab849f0da0fbda59d86bd92c0696759a6
MD5 6b08aa2563677b2f515f64f97d9d7a72
BLAKE2b-256 e26080397c73be1acf3438cbabbbbcefea2790d831648c518be188cce82508cc

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