Skip to main content

An Implementation of Conditional Random Fields in pytorch

Project description

Torch CRF

CircleCI Coverage Status Github Star PyPI version Python Versions MIT License

Implementation of CRF (Conditional Random Fields) in PyTorch

Requirements

  • python3 (>=3.6)
  • PyTorch (>=1.0)

Installation

$ pip install TorchCRF

Usage

>>> import torch
>>> from TorchCRF import CRF
>>> device = "cuda" if torch.cuda.is_available() else "cpu"
>>> batch_size = 2
>>> sequence_size = 3
>>> num_labels = 5
>>> mask = torch.ByteTensor([[1, 1, 1], [1, 1, 0]]).to(device) # (batch_size. sequence_size)
>>> labels = torch.LongTensor([[0, 2, 3], [1, 4, 1]]).to(device)  # (batch_size, sequence_size)
>>> hidden = torch.randn((batch_size, sequence_size, num_labels), requires_grad=True).to(device)
>>> crf = CRF(num_labels)

Computing log-likelihood (used where forward)

>>> crf.forward(hidden, labels, mask)
tensor([-7.6204, -3.6124], device='cuda:0', grad_fn=<ThSubBackward>)

Decoding (predict labels of sequences)

>>> crf.viterbi_decode(hidden, mask)
[[0, 2, 2], [4, 0]]

License

MIT

References

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

TorchCRF-1.0.7.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

TorchCRF-1.0.7-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file TorchCRF-1.0.7.tar.gz.

File metadata

  • Download URL: TorchCRF-1.0.7.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for TorchCRF-1.0.7.tar.gz
Algorithm Hash digest
SHA256 bcea092a0ebe92c6c57f91dc9fd05028e5a2307c9da417df50262b61d66e4a6a
MD5 ff65ec66fec2e24cca18f8cdc8bb1ce9
BLAKE2b-256 8f904825f7c3dd9588c0c28a523311486c7e4f447370971dca992ba8b27218f1

See more details on using hashes here.

File details

Details for the file TorchCRF-1.0.7-py3-none-any.whl.

File metadata

  • Download URL: TorchCRF-1.0.7-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for TorchCRF-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 c9f472d505ee4e06b2731a53f1c7d2547afa87e6376a5d9e28179edeb8499196
MD5 341a923f68979ff65edd8d4c07140b2c
BLAKE2b-256 072a7bccc71cd80fd712a976a2dc1a954f1677b5c966513a1f4d5c934054127f

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