Skip to main content

Axial Positional Embedding

PyPI version

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

$ pip install axial-positional-embedding

Usage

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)

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

@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}
}
@misc{ho2019axial,
    title   = {Axial Attention in Multidimensional Transformers},
    author  = {Jonathan Ho and Nal Kalchbrenner and Dirk Weissenborn and Tim Salimans},
    year    = {2019},
    archivePrefix = {arXiv}
}

Metadata

Release files for axial-positional-embedding 0.3.12

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

Source distribution (sdist)

Source distribution for axial-positional-embedding 0.3.12
File Size Uploaded
axial_positional_embedding-0.3.12.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for axial-positional-embedding 0.3.12
File Interpreter ABI Platform
axial_positional_embedding-0.3.12-py3-none-any.whl Python 3 none any Details

Total release size: 13.0 kB

Release files / axial_positional_embedding-0.3.12.tar.gz

Download URL axial_positional_embedding-0.3.12.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
10d98f342f8fa0d4d3d20b36ed27fb0fc63f1d398202b9165e07bc9d6130bc17
BLAKE2b-256 checksum
How to use checksums
12e6294c09aa82ccfb8ea18276cbce61964f35758f5de28f2677acf4fd71fcce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release files / axial_positional_embedding-0.3.12-py3-none-any.whl

Download URL axial_positional_embedding-0.3.12-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f5708106b18903c3b99aced0bfa79d1ae6f708dae34f56e641e8c24dbc06b02f
BLAKE2b-256 checksum
How to use checksums
e7f925753b9de3029d3eb2487755520b98eb72b0cb562d8974329c6e19831063
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.21

Release history Release notifications | RSS feed

This release

0.3.12 This release

2 release files

0.3.10

2 release files

0.3.9

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

1 release file

0.2.0

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.2

1 release file

0.0.1

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