Skip to main content
https://badge.fury.io/py/pytorch-crf.svg https://travis-ci.org/kmkurn/pytorch-crf.svg?branch=master https://coveralls.io/repos/github/kmkurn/pytorch-crf/badge.svg?branch=master https://cdn.rawgit.com/syl20bnr/spacemacs/442d025779da2f62fc86c2082703697714db6514/assets/spacemacs-badge.svg

Conditional random field in PyTorch.

Description

This package provides an implementation of conditional random field (CRF) in PyTorch. This implementation borrows mostly from AllenNLP CRF module with some modifications.

Requirements

  • Python 3.6

  • PyTorch 0.4.1

Installation

You can install with pip

pip install pytorch-crf

Or, you can install from Github directly

pip install git+https://github.com/kmkurn/pytorch-crf#egg=pytorch_crf

Examples

In the examples below, we will assume that these lines have been executed

>>> import torch
>>> from torchcrf import CRF
>>> seq_length, batch_size, num_tags = 3, 2, 5
>>> emissions = torch.randn(seq_length, batch_size, num_tags)
>>> tags = torch.tensor([[0, 1], [2, 4], [3, 1]], dtype=torch.long)  # (seq_length, batch_size)
>>> model = CRF(num_tags)

Computing log likelihood

>>> model(emissions, tags)
tensor(-12.7431, grad_fn=<SumBackward0>)

Computing log likelihood with mask

>>> mask = torch.tensor([[1, 1], [1, 1], [1, 0]], dtype=torch.uint8)  # (seq_length, batch_size)
>>> model(emissions, tags, mask=mask)
tensor(-10.8390, grad_fn=<SumBackward0>)

Decoding

>>> model.decode(emissions)
[[3, 1, 3], [0, 1, 0]]

Decoding with mask

>>> model.decode(emissions, mask=mask)
[[3, 1, 3], [0, 1]]

See tests/test_crf.py for more examples.

License

MIT. See LICENSE for details.

Contributing

Contributions are welcome! Please follow these instructions to setup dependencies and running the tests and linter. Make a pull request once your contribution is ready.

Installing dependencies

Make sure you setup a virtual environment with Python 3.6 and PyTorch installed. Then, install all the dependencies in requirements.txt file and install this package in development mode.

pip install -r requirements.txt
pip install -e .

Running tests

Run pytest in the project root directory.

Running linter

Run flake8 in the project root directory. This will also run mypy, thanks to flake8-mypy package.

Metadata

Release files for pytorch-crf 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for pytorch-crf 0.6.0
File Interpreter ABI Platform
pytorch_crf-0.6.0-py3-none-any.whl Python 3 none any Details

Release files / pytorch_crf-0.6.0-py3-none-any.whl

Download URL pytorch_crf-0.6.0-py3-none-any.whl
Size 9.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f1b53664a4b4af278c107d5d9b4a069aadb25536d6cbb2f257ede8dbb7ffa8f
BLAKE2b-256 checksum
How to use checksums
37b01166f2373a5d7bf825a1c616d4d1feaef1f7407d771d1c3e14bf51fadbd3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.3

Release history Release notifications | RSS feed

0.7.2

2 release files

0.7.1

2 release files

0.7.0

1 release file

This release

0.6.0 This release

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page