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

Uploaded Source

Built Distribution

TorchCRF-1.0.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: TorchCRF-1.0.1.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.1

File hashes

Hashes for TorchCRF-1.0.1.tar.gz
Algorithm Hash digest
SHA256 4c110d15ed1cc33bf714f3a75dc4c21f597a01f657b32b8c96aef190b5ab1575
MD5 de5c48973fef3f8691f3629dfed0092b
BLAKE2b-256 d4917f87f3ddbf4665067eb166f1a606e74a99aad1d6fa9127efce79a1dbfa9a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: TorchCRF-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 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.1

File hashes

Hashes for TorchCRF-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a0b79c723677e58b1c3586ed003ecf7c59840b5210cf606f3f92eda54c8bd627
MD5 374f251949121d3ea53d31c6742e3bc0
BLAKE2b-256 b826e42ee0b2106b85cf7842947915555062bd95c3584a23269650bf12c3a556

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