Skip to main content

An Implementation of Conditional Random Fields in pytorch

Project description

Torch CRF

CircleCI Coverage Status

Implementation of CRF (Conditional Random Fields) in PyTorch 1.0

Requirements

  • python3 (>=3.6)
  • PyTorch 1.0

Installation

$ pip install TorchCRF

Usage

>>> import torch
>>> from TorchCRF import CRF
>>> batch_size = 2
>>> sequence_size = 3
>>> num_labels = 5
>>> mask = torch.FloatTensor([[1, 1, 1], [1, 1, 0]]) # (batch_size. sequence_size)
>>> labels = torch.LongTensor([[0, 2, 3], [1, 4, 1]])  # (batch_size, sequence_size)
>>> hidden = torch.randn((batch_size, sequence_size, num_labels), requires_grad=True)
>>> crf = CRF(num_labels)

Computing log-likelihood (used where forward)

>>> crf.forward(hidden, labels, mask)
tensor([-7.6204, -3.6124], 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.3.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

TorchCRF-1.0.3-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: TorchCRF-1.0.3.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for TorchCRF-1.0.3.tar.gz
Algorithm Hash digest
SHA256 23c5401c58a36d4b816f06c73fabd839f6a75ecb2d2b5b8661b9aeac320d7e95
MD5 1050914891f85c88d05c4d299362e1ad
BLAKE2b-256 6c11aae32d57d2d60ab8dca0031c784173b7f1811c340504cc89c764fbf5f3d8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: TorchCRF-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for TorchCRF-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1784cb7cf39f7435baebbba9a12945676352b6ce8a8796cde619bb3109fb0b53
MD5 2775a8b60b0a757f1fa4dfb65f092e7c
BLAKE2b-256 58f9b9e7673f3ec810890703a4f595873dd802c53b447a36c81b520750bbebfa

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