← 开源
tatp22

multidim-positional-encoding

An implementation of 1D, 2D, and 3D positional encoding in Pytorch and TensorFlow

Model DevelopmentArchitecturePython
在 GitHub 打开
增长势头
+024 小时新增 Star0.0%
616
Star
35
Fork
+0
本周
5
贡献者
创建于 2020-07-26 · 更新于 2026-09-24 · 今日第 16034 名
主要开发者
tatp22IRiViIpetar-iv

tatp22/multidim-positional-encoding 排名第 16,034,有 616 个星标。

同类别比较

tatp22/multidim-positional-encoding 排名第 16,034,此处有 8 个同类条目;LynnHo/AttGAN-Tensorflow 排名第 16,037。

同类别比较
名称排名指标
hpcaitech/FastFold16,019
语言:
Python
星标数:
617
分叉数:
90
简介:
Optimizing AlphaFold Training and Inference on GPU Clusters
Audio-WestlakeU/FullSubNet16,026
语言:
Python
星标数:
616
分叉数:
158
简介:
PyTorch implementation of "FullSubNet: A Full-Band and Sub-Band Fusion Model for Real-Time Single-Channel Speech Enhancement."
DiffSharp/DiffSharp16,027
语言:
F#
星标数:
616
分叉数:
72
简介:
DiffSharp: Differentiable Functional Programming
RElbers/info-nce-pytorch16,030
语言:
Python
星标数:
616
分叉数:
43
简介:
PyTorch implementation of the InfoNCE loss for self-supervised learning.
LynnHo/AttGAN-Tensorflow16,037
语言:
Python
星标数:
615
分叉数:
137
简介:
[TIP'19] AttGAN: Facial Attribute Editing by Only Changing What You Want
mondejar/ecg-classification16,046
语言:
Python
星标数:
615
分叉数:
200
简介:
Code for training and test machine learning classifiers on MIT-BIH Arrhyhtmia database
pmixer/SASRec.pytorch16,048
语言:
TeX
星标数:
615
分叉数:
133
简介:
PyTorch(1.6+) implementation of https://github.com/kang205/SASRec
richardaecn/class-balanced-loss16,049
语言:
Python
星标数:
615
分叉数:
66
简介:
Class-Balanced Loss Based on Effective Number of Samples. CVPR 2019
关键参数与规格
关键参数与规格
参数值
语言Python
类别Model Development
子类别Architecture
星标数616
分叉数35
贡献者5
创建日期2020-07-26
更新日期2026-09-24
1 天星标数0
7 天星标数0
排名16,034
上次排名15,832
排名变化-202
7 天星标排名15,308
上次 7 天星标排名15,321
7 天星标排名变化13
总星标排名16,450
上次总星标排名16,420
总星标排名变化-30
国家Switzerland
README 摘录

1D, 2D, and 3D Sinusoidal Postional Encoding (Pytorch and Tensorflow) This is a practical, easy to download implemenation of 1D, 2D, and 3D sinusodial positional encodings for PyTorch and Tensorflow. It is able to encode on tensors of the form , , and , where the positional encodings will be calculated along the dimension. The Attention is All You Need allowed for positional encoding in only one dimension, however, this works to extend this to 2 and 3 dimensions. This also works on

tensors of the form , etc. **NOTE**: The import syntax has changed as of version . To install, simply run: You can also install the pytorch and tf encodings individually with the following commands. For a PyTorch only installation, run For a TensorFlow only installation, run Usage: Pytorch The repo comes with the three main positional encoding models, . In addition, there are a class that adds the input tensor to the positional encodings. And for tensors of the form or their 2D

and 3D counterparts, include the word before the number in the class; e. for a 1D input of size , do instead of . Note to override the output dtype you can specify it when creating the encoding: This is particularly useful when the input tensor is of an type since the output will always be zero (see issue #39). Tensorflow Keras This also supports Tensorflow. Changes as of version Before , users had to install both the and the packages, both of

which are quite large. Now, one can install the packages individually, but now the code has to be changed: If using PyTorch: If using TensorFlow: Formulas The formula for inserting the positional encoding are as follows: 1D: 2D: 3D: The 3D formula is just a natural extension of the 2D positional encoding used in this paper. Don't worry if the input is not divisible by 2 (1D), 4 (2D), or 6 (3D); all the necessary padding will be taken care

of. Thank you Thank you for this repo for inspriration of this method.

常见问题

记录了多少星标和分叉?

tatp22/multidim-positional-encoding 有 616 个星标和 35 个分叉。

仓库排名是多少?

tatp22/multidim-positional-encoding 排名第 16,034。

记录的语言和类别是什么?

tatp22/multidim-positional-encoding 有 616 个星标,语言为 Python,类别为 Model Development。

哪个仓库排名最接近?

tatp22/multidim-positional-encoding 与 LynnHo/AttGAN-Tensorflow 比较,后者排名第 16,037,有 615 个星标。

README

1D, 2D, and 3D Sinusoidal Postional Encoding (Pytorch and Tensorflow)

This is a practical, easy to download implemenation of 1D, 2D, and 3D sinusodial positional encodings for PyTorch and Tensorflow.

It is able to encode on tensors of the form (batchsize, x, ch), (batchsize, x, y, ch), and (batchsize, x, y, z, ch), where the positional encodings will be calculated along the ch dimension. The Attention is All You Need allowed for positional encoding in only one dimension, however, this works to extend this to 2 and 3 dimensions.

This also works on tensors of the form (batchsize, ch, x), etc. See the usage for more information.

NOTE: The import syntax has changed as of version 6.0.1. See the section for details.

To install, simply run:

pip install positional-encodings[pytorch,tensorflow]

You can also install the pytorch and tf encodings individually with the following commands.

  • For a PyTorch only installation, run pip install positional-encodings[pytorch]
  • For a TensorFlow only installation, run pip install positional-encodings[tensorflow]

Usage:

Pytorch

The repo comes with the three main positional encoding models, PositionalEncoding{1,2,3}D. In addition, there are a Summer class that adds the input tensor to the positional encodings.

import torch
from positional_encodings.torch_encodings import PositionalEncoding1D, PositionalEncoding2D, PositionalEncoding3D, Summer

# Returns the position encoding only
p_enc_1d_model = PositionalEncoding1D(10)

# Return the inputs with the position encoding added
p_enc_1d_model_sum = Summer(PositionalEncoding1D(10))

x = torch.rand(1,6,10)
penc_no_sum = p_enc_1d_model(x) # penc_no_sum.shape == (1, 6, 10)
penc_sum = p_enc_1d_model_sum(x)
print(penc_no_sum + x == penc_sum) # True
p_enc_2d = PositionalEncoding2D(8)
y = torch.zeros((1,6,2,8))
print(p_enc_2d(y).shape) # (1, 6, 2, 8)

p_enc_3d = PositionalEncoding3D(11)
z = torch.zeros((1,5,6,4,11))
print(p_enc_3d(z).shape) # (1, 5, 6, 4, 11)

And for tensors of the form (batchsize, ch, x) or their 2D and 3D counterparts, include the word Permute before the number in the class; e.g. for a 1D input of size (batchsize, ch, x), do PositionalEncodingPermute1D instead of PositionalEncoding1D.

import torch
from positional_encodings.torch_encodings import PositionalEncodingPermute3D

p_enc_3d = PositionalEncodingPermute3D(11)
z = torch.zeros((1,11,5,6,4))
print(p_enc_3d(z).shape) # (1, 11, 5, 6, 4)

Note to override the output dtype you can specify it when creating the encoding:

p_enc_3d = PositionalEncodingPermute3D(11, dtype_override=torch.float64)

This is particularly useful when the input tensor is of an int type since the output will always be zero (see issue #39).

Tensorflow Keras

This also supports Tensorflow. Simply prepend all class names with TF.

import tensorflow as tf
from positional_encodings.tf_encodings import TFPositionalEncoding2D, TFSummer

# Returns the position encoding only
p_enc_2d = TFPositionalEncoding2D(170)
y = tf.zeros((1,8,6,2))
print(p_enc_2d(y).shape) # (1, 8, 6, 2)

# Return the inputs with the position encoding added
add_p_enc_2d = TFSummer(TFPositionalEncoding2D(170))
y = tf.ones((1,8,6,2))
print(add_p_enc_2d(y) - p_enc_2d(y)) # tf.ones((1,8,6,2))

Changes as of version 6.0.1

Before 6.0.1, users had to install both the tensorflow and the torch packages, both of which are quite large. Now, one can install the packages individually, but now the code has to be changed:

If using PyTorch:

from positional_encodings import * -> from positional_encodings.torch_encodings import *

If using TensorFlow:

from positional_encodings import * -> from positional_encodings.tf_encodings import *

Formulas

The formula for inserting the positional encoding are as follows:

1D:

PE(x,2i) = sin(x/10000^(2i/D))
PE(x,2i+1) = cos(x/10000^(2i/D))

Where:
x is a point in 2d space
i is an integer in [0, D/2), where D is the size of the ch dimension

2D:

PE(x,y,2i) = sin(x/10000^(4i/D))
PE(x,y,2i+1) = cos(x/10000^(4i/D))
PE(x,y,2j+D/2) = sin(y/10000^(4j/D))
PE(x,y,2j+1+D/2) = cos(y/10000^(4j/D))

Where:
(x,y) is a point in 2d space
i,j is an integer in [0, D/4), where D is the size of the ch dimension

3D:

PE(x,y,z,2i) = sin(x/10000^(6i/D))
PE(x,y,z,2i+1) = cos(x/10000^(6i/D))
PE(x,y,z,2j+D/3) = sin(y/10000^(6j/D))
PE(x,y,z,2j+1+D/3) = cos(y/10000^(6j/D))
PE(x,y,z,2k+2D/3) = sin(z/10000^(6k/D))
PE(x,y,z,2k+1+2D/3) = cos(z/10000^(6k/D))

Where:
(x,y,z) is a point in 3d space
i,j,k is an integer in [0, D/6), where D is the size of the ch dimension

The 3D formula is just a natural extension of the 2D positional encoding used in this paper.

Don't worry if the input is not divisible by 2 (1D), 4 (2D), or 6 (3D); all the necessary padding will be taken care of.

Thank you

Thank you for this repo for inspriration of this method.

Citations

1D:

@inproceedings{vaswani2017attention,
  title={Attention is all you need},
  author={Vaswani, Ashish and Shazeer, Noam and Parmar, Niki and Uszkoreit, Jakob and Jones, Llion and Gomez, Aidan N and Kaiser, {\L}ukasz and Polosukhin, Illia},
  booktitle={Advances in neural information processing systems},
  pages={5998--6008},
  year={2017}
}

2D:

@misc{wang2019translating,
    title={Translating Math Formula Images to LaTeX Sequences Using Deep Neural Networks with Sequence-level Training},
    author={Zelun Wang and Jyh-Charn Liu},
    year={2019},
    eprint={1908.11415},
    archivePrefix={arXiv},
    primaryClass={cs.LG}
}

3D: Coming soon!

排名相邻
同类项目