Skip to main content

Conditional random field in PyTorch

Project description

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

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytorch_crf-0.5.1-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file pytorch_crf-0.5.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pytorch_crf-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 007008abc5c6c337f22f84d4f03d0cd5e5797cdd4f08ca51c0dc0934dbd11520
MD5 82d14ca272985a0e8074f2c92e3b8801
BLAKE2b-256 3c3e7b6d9c997e56d4201177161b5da6aa60b133ae5b524913e2d4459baf32d7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page