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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: TorchCRF-1.0.4.tar.gz
  • Upload date:
  • Size: 5.8 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.4.tar.gz
Algorithm Hash digest
SHA256 1f18362a068d9b38abc740af5bf97437373f9947de41b5fe3412c18e1335f89e
MD5 769292b4fcdfb072e7f6c6d4f7eadec9
BLAKE2b-256 0eb53d5b32fba84513afc5d2fef1d936313fd46a9ba2a5153529fa169b0582a5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: TorchCRF-1.0.4-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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 07d537789211de1ba5a47a7b0504fcf7eccafe0d4f4882ef9cd403c2d6522e97
MD5 619d38c27307012d67a6fa70329ddf59
BLAKE2b-256 15b73df56591dd533805a1be5607abb7c1e3255ac2a6d241518246f7a573d32b

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