Skip to main content

pytorch-crf

https://travis-ci.org/kmkurn/pytorch-crf.svg?branch=master https://coveralls.io/repos/github/kmkurn/pytorch-crf/badge.svg?branch=enable-travis

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.3.0

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.autograd.Variable(torch.randn(seq_length, batch_size, num_tags), requires_grad=True)
>>> tags = torch.autograd.Variable(torch.LongTensor([[0, 1], [2, 4], [3, 1]]))  # (seq_length, batch_size)
>>> model = CRF(num_tags)

Computing log likelihood

>>> model(emissions, tags)
Variable containing:
-10.0635
[torch.FloatTensor of size 1]

Computing log likelihood with mask

>>> mask = torch.autograd.Variable(torch.ByteTensor([[1, 1], [1, 1], [1, 0]]))  # (seq_length, batch_size)
>>> model(emissions, tags, mask=mask)
Variable containing:
-8.4981
[torch.FloatTensor of size 1]

Decoding

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

Decoding with mask

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

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.5.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.5.0
File Interpreter ABI Platform Actions
pytorch_crf-0.5.0-py3-none-any.whl Python 3 none any Details for pytorch_crf-0.5.0-py3-none-any.whl

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

Download URL pytorch_crf-0.5.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4d7549bc4ac2893def2c94340ada0f23e52a400078122da1bf49f81c102020b8
BLAKE2b-256 checksum
How to use checksums
c73eabc5666df256f23508a026ea451c6dfce36816e5d9751e41dd6942f2f239
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for pytorch-crf 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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