axial-positional-embedding


Nameaxial-positional-embedding JSON
Version 0.3.10 PyPI version JSON
download
home_pageNone
SummaryAxial Positional Embedding
upload_time2025-01-23 20:05:25
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT License Copyright (c) 2020 Phil Wang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords artificial intelligence deep learning positional embedding
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ## Axial Positional Embedding

[![PyPI version](https://badge.fury.io/py/axial-positional-embedding.svg)](https://badge.fury.io/py/axial-positional-embedding)

A type of positional embedding that is very effective when working with attention networks on multi-dimensional data, or for language models in general.

## Install

```bash
$ pip install axial-positional-embedding
```

## Usage

```python
import torch
from axial_positional_embedding import AxialPositionalEmbedding

pos_emb = AxialPositionalEmbedding(
    dim = 512,
    axial_shape = (64, 64),          # axial shape will multiply up to the maximum sequence length allowed (64 * 64 = 4096)
    axial_dims = (256, 256)          # if not specified, dimensions will default to 'dim' for all axials and summed at the end. if specified, each axial will have the specified dimension and be concatted together. the concatted dimensions needs to sum up to the `dim` (256 + 256 = 512)
)

tokens = torch.randn(1, 1024, 512)  # assume are tokens
tokens = pos_emb(tokens) + tokens   # add positional embedding to token embeddings
```

A continuous version with better extrapolation ability (each axis parameterized by a 2 layer MLP)

```python
import torch
from axial_positional_embedding import ContinuousAxialPositionalEmbedding

pos_emb = ContinuousAxialPositionalEmbedding(
    dim = 512,
    num_axial_dims = 3
)

tokens = torch.randn(1, 8, 16, 32, 512) # say a video with 8 frames, 16 x 32 image dimension

axial_pos_emb = pos_emb((8, 16, 32)) # pass in the size from above

tokens = axial_pos_emb + tokens   # add positional embedding to token embeddings
```

## Citations

```bibtex
@inproceedings{kitaev2020reformer,
    title       = {Reformer: The Efficient Transformer},
    author      = {Nikita Kitaev and Lukasz Kaiser and Anselm Levskaya},
    booktitle   = {International Conference on Learning Representations},
    year        = {2020},
    url         = {https://openreview.net/forum?id=rkgNKkHtvB}
}
```

```bibtex
@misc{ho2019axial,
    title   = {Axial Attention in Multidimensional Transformers},
    author  = {Jonathan Ho and Nal Kalchbrenner and Dirk Weissenborn and Tim Salimans},
    year    = {2019},
    archivePrefix = {arXiv}
}
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "axial-positional-embedding",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "artificial intelligence, deep learning, positional embedding",
    "author": null,
    "author_email": "Phil Wang <lucidrains@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/4a/98/a904b0955f2d7fb81f8b8c08f3fc98c8834b8b97118c912b15a4aabe3814/axial_positional_embedding-0.3.10.tar.gz",
    "platform": null,
    "description": "## Axial Positional Embedding\n\n[![PyPI version](https://badge.fury.io/py/axial-positional-embedding.svg)](https://badge.fury.io/py/axial-positional-embedding)\n\nA type of positional embedding that is very effective when working with attention networks on multi-dimensional data, or for language models in general.\n\n## Install\n\n```bash\n$ pip install axial-positional-embedding\n```\n\n## Usage\n\n```python\nimport torch\nfrom axial_positional_embedding import AxialPositionalEmbedding\n\npos_emb = AxialPositionalEmbedding(\n    dim = 512,\n    axial_shape = (64, 64),          # axial shape will multiply up to the maximum sequence length allowed (64 * 64 = 4096)\n    axial_dims = (256, 256)          # if not specified, dimensions will default to 'dim' for all axials and summed at the end. if specified, each axial will have the specified dimension and be concatted together. the concatted dimensions needs to sum up to the `dim` (256 + 256 = 512)\n)\n\ntokens = torch.randn(1, 1024, 512)  # assume are tokens\ntokens = pos_emb(tokens) + tokens   # add positional embedding to token embeddings\n```\n\nA continuous version with better extrapolation ability (each axis parameterized by a 2 layer MLP)\n\n```python\nimport torch\nfrom axial_positional_embedding import ContinuousAxialPositionalEmbedding\n\npos_emb = ContinuousAxialPositionalEmbedding(\n    dim = 512,\n    num_axial_dims = 3\n)\n\ntokens = torch.randn(1, 8, 16, 32, 512) # say a video with 8 frames, 16 x 32 image dimension\n\naxial_pos_emb = pos_emb((8, 16, 32)) # pass in the size from above\n\ntokens = axial_pos_emb + tokens   # add positional embedding to token embeddings\n```\n\n## Citations\n\n```bibtex\n@inproceedings{kitaev2020reformer,\n    title       = {Reformer: The Efficient Transformer},\n    author      = {Nikita Kitaev and Lukasz Kaiser and Anselm Levskaya},\n    booktitle   = {International Conference on Learning Representations},\n    year        = {2020},\n    url         = {https://openreview.net/forum?id=rkgNKkHtvB}\n}\n```\n\n```bibtex\n@misc{ho2019axial,\n    title   = {Axial Attention in Multidimensional Transformers},\n    author  = {Jonathan Ho and Nal Kalchbrenner and Dirk Weissenborn and Tim Salimans},\n    year    = {2019},\n    archivePrefix = {arXiv}\n}\n```\n",
    "bugtrack_url": null,
    "license": "MIT License\n        \n        Copyright (c) 2020 Phil Wang\n        \n        Permission is hereby granted, free of charge, to any person obtaining a copy\n        of this software and associated documentation files (the \"Software\"), to deal\n        in the Software without restriction, including without limitation the rights\n        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n        copies of the Software, and to permit persons to whom the Software is\n        furnished to do so, subject to the following conditions:\n        \n        The above copyright notice and this permission notice shall be included in all\n        copies or substantial portions of the Software.\n        \n        THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n        SOFTWARE.",
    "summary": "Axial Positional Embedding",
    "version": "0.3.10",
    "project_urls": {
        "Homepage": "https://pypi.org/project/axial_positional_embedding/",
        "Repository": "https://github.com/lucidrains/axial_positional_embedding"
    },
    "split_keywords": [
        "artificial intelligence",
        " deep learning",
        " positional embedding"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "f71c63dab07e168bca7bcf701da7901164da5eb2db257c4a9474f521cf5ad60c",
                "md5": "fc35aa7a2a6bb15838bd9ffedbcfb99a",
                "sha256": "26c0cdf0e01bd61fea8a0f7beff685b88ee317089d727ef12a68635f1faf353d"
            },
            "downloads": -1,
            "filename": "axial_positional_embedding-0.3.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "fc35aa7a2a6bb15838bd9ffedbcfb99a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6729,
            "upload_time": "2025-01-23T20:05:19",
            "upload_time_iso_8601": "2025-01-23T20:05:19.391798Z",
            "url": "https://files.pythonhosted.org/packages/f7/1c/63dab07e168bca7bcf701da7901164da5eb2db257c4a9474f521cf5ad60c/axial_positional_embedding-0.3.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "4a98a904b0955f2d7fb81f8b8c08f3fc98c8834b8b97118c912b15a4aabe3814",
                "md5": "6353d31d4b8e4da849e69607c590e1e2",
                "sha256": "92df9d38352af26ee93071dd72aff8d351d5abee60f605a12c01a83fe9d98620"
            },
            "downloads": -1,
            "filename": "axial_positional_embedding-0.3.10.tar.gz",
            "has_sig": false,
            "md5_digest": "6353d31d4b8e4da849e69607c590e1e2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 6311,
            "upload_time": "2025-01-23T20:05:25",
            "upload_time_iso_8601": "2025-01-23T20:05:25.735373Z",
            "url": "https://files.pythonhosted.org/packages/4a/98/a904b0955f2d7fb81f8b8c08f3fc98c8834b8b97118c912b15a4aabe3814/axial_positional_embedding-0.3.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-23 20:05:25",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "lucidrains",
    "github_project": "axial_positional_embedding",
    "github_not_found": true,
    "lcname": "axial-positional-embedding"
}
        
Elapsed time: 0.85009s