# Torch CRF
[![CircleCI](https://circleci.com/gh/s14t284/TorchCRF.svg?style=svg)](https://circleci.com/gh/s14t284/TorchCRF)
[![Coverage Status](https://coveralls.io/repos/github/s14t284/TorchCRF/badge.svg)](https://coveralls.io/github/s14t284/TorchCRF)
[![MIT License](https://img.shields.io/github/license/s14t284/TorchCRF)](LICENSE)
[![Python Versions](https://img.shields.io/pypi/pyversions/TorchCRF.svg)](https://pypi.org/project/TorchCRF/)
[![PyPI version](https://badge.fury.io/py/TorchCRF.svg)](https://badge.fury.io/py/TorchCRF)
Implementation of CRF (Conditional Random Fields) in PyTorch
## Requirements
- python3 (>=3.6)
- PyTorch (>=1.0)
## Installation
$ pip install TorchCRF
## Usage
```python
>>> import torch
>>> from TorchCRF import CRF
>>> device = "cuda" if torch.cuda.is_available() else "cpu"
>>> batch_size = 2
>>> sequence_size = 3
>>> num_labels = 5
>>> mask = torch.ByteTensor([[1, 1, 1], [1, 1, 0]]).to(device) # (batch_size. sequence_size)
>>> labels = torch.LongTensor([[0, 2, 3], [1, 4, 1]]).to(device) # (batch_size, sequence_size)
>>> hidden = torch.randn((batch_size, sequence_size, num_labels), requires_grad=True).to(device)
>>> crf = CRF(num_labels)
```
### Computing log-likelihood (used where forward)
```python
>>> crf.forward(hidden, labels, mask)
tensor([-7.6204, -3.6124], device='cuda:0', grad_fn=<ThSubBackward>)
```
### Decoding (predict labels of sequences)
```python
>>> crf.viterbi_decode(hidden, mask)
[[0, 2, 2], [4, 0]]
```
## License
MIT
## References
- [threelittlemonkeys/lstm-crf-pytorch](https://github.com/threelittlemonkeys/lstm-crf-pytorch)
- [kmkurn/pytorch-crf](https://github.com/kmkurn/pytorch-crf)
Raw data
{
"_id": null,
"home_page": "https://github.com/s14t284/TorchCRF",
"name": "TorchCRF",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "crf,conditional random fields,nlp,natural language processing",
"author": "Ryuya Ikeda",
"author_email": "rikeda71@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/a4/b8/1b7568b7e84d0439723f7a972cd4b3007e606a66e3d81ada5f61c131d29f/TorchCRF-1.1.0.tar.gz",
"platform": "",
"description": "# Torch CRF\n\n[![CircleCI](https://circleci.com/gh/s14t284/TorchCRF.svg?style=svg)](https://circleci.com/gh/s14t284/TorchCRF)\n[![Coverage Status](https://coveralls.io/repos/github/s14t284/TorchCRF/badge.svg)](https://coveralls.io/github/s14t284/TorchCRF)\n[![MIT License](https://img.shields.io/github/license/s14t284/TorchCRF)](LICENSE)\n\n[![Python Versions](https://img.shields.io/pypi/pyversions/TorchCRF.svg)](https://pypi.org/project/TorchCRF/)\n[![PyPI version](https://badge.fury.io/py/TorchCRF.svg)](https://badge.fury.io/py/TorchCRF)\n\nImplementation of CRF (Conditional Random Fields) in PyTorch\n\n## Requirements\n\n- python3 (>=3.6)\n- PyTorch (>=1.0)\n\n## Installation\n\n $ pip install TorchCRF\n\n## Usage\n\n```python\n>>> import torch\n>>> from TorchCRF import CRF\n>>> device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n>>> batch_size = 2\n>>> sequence_size = 3\n>>> num_labels = 5\n>>> mask = torch.ByteTensor([[1, 1, 1], [1, 1, 0]]).to(device) # (batch_size. sequence_size)\n>>> labels = torch.LongTensor([[0, 2, 3], [1, 4, 1]]).to(device) # (batch_size, sequence_size)\n>>> hidden = torch.randn((batch_size, sequence_size, num_labels), requires_grad=True).to(device)\n>>> crf = CRF(num_labels)\n```\n\n### Computing log-likelihood (used where forward)\n\n```python\n>>> crf.forward(hidden, labels, mask)\ntensor([-7.6204, -3.6124], device='cuda:0', grad_fn=<ThSubBackward>)\n```\n\n### Decoding (predict labels of sequences)\n\n```python\n>>> crf.viterbi_decode(hidden, mask)\n[[0, 2, 2], [4, 0]]\n```\n\n## License\n\nMIT\n\n## References\n\n- [threelittlemonkeys/lstm-crf-pytorch](https://github.com/threelittlemonkeys/lstm-crf-pytorch)\n- [kmkurn/pytorch-crf](https://github.com/kmkurn/pytorch-crf)\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "An Implementation of Conditional Random Fields in pytorch",
"version": "1.1.0",
"project_urls": {
"Homepage": "https://github.com/s14t284/TorchCRF"
},
"split_keywords": [
"crf",
"conditional random fields",
"nlp",
"natural language processing"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0e953490a6c67d543aca9af21a7a13197750e18e975a215fa4b6f5411902908d",
"md5": "b65f005509040701d9943b40d8f48e34",
"sha256": "540f1a61ce94fa3c247719e2754a55e1cdd0e75b42f838ec025cfe158afd9151"
},
"downloads": -1,
"filename": "TorchCRF-1.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b65f005509040701d9943b40d8f48e34",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 5205,
"upload_time": "2020-08-01T09:27:46",
"upload_time_iso_8601": "2020-08-01T09:27:46.778780Z",
"url": "https://files.pythonhosted.org/packages/0e/95/3490a6c67d543aca9af21a7a13197750e18e975a215fa4b6f5411902908d/TorchCRF-1.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a4b81b7568b7e84d0439723f7a972cd4b3007e606a66e3d81ada5f61c131d29f",
"md5": "76a0eae67c1c0f4c04820cccdddb573f",
"sha256": "c916064e9b498a9f238a5f733e510ec3f271fd482e2970563a675cbf390d34fb"
},
"downloads": -1,
"filename": "TorchCRF-1.1.0.tar.gz",
"has_sig": false,
"md5_digest": "76a0eae67c1c0f4c04820cccdddb573f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 5352,
"upload_time": "2020-08-01T09:27:48",
"upload_time_iso_8601": "2020-08-01T09:27:48.674929Z",
"url": "https://files.pythonhosted.org/packages/a4/b8/1b7568b7e84d0439723f7a972cd4b3007e606a66e3d81ada5f61c131d29f/TorchCRF-1.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2020-08-01 09:27:48",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "s14t284",
"github_project": "TorchCRF",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"circle": true,
"requirements": [],
"lcname": "torchcrf"
}