Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_sinusoid_encoding() #2

Open
Dpw506 opened this issue Jun 25, 2021 · 1 comment
Open

get_sinusoid_encoding() #2

Dpw506 opened this issue Jun 25, 2021 · 1 comment

Comments

@Dpw506
Copy link

Dpw506 commented Jun 25, 2021

    if self.has_pos_embed:
        self.pos_embed = nn.Parameter(data=get_sinusoid_encoding(n_position=num_patches, d_hid=embed_dim), requires_grad=False)
        self.pos_drop = nn.Dropout(p=drop_rate)

Hello, is get_sinusoid_encoding used? Can I remove it if I don't use it? This function doesn't seem to be defined.

@realeve
Copy link

realeve commented Oct 1, 2021

https://github.com/yitu-opensource/T2T-ViT/blob/88afb7cf30b603703c02b6e29c06be23c49cf6a2/models/transformer_block.py#L78

def get_sinusoid_encoding(n_position, d_hid):
    ''' Sinusoid position encoding table '''

    def get_position_angle_vec(position):
        return [position / np.power(10000, 2 * (hid_j // 2) / d_hid) for hid_j in range(d_hid)]

    sinusoid_table = np.array([get_position_angle_vec(pos_i) for pos_i in range(n_position)])
    sinusoid_table[:, 0::2] = np.sin(sinusoid_table[:, 0::2])  # dim 2i
    sinusoid_table[:, 1::2] = np.cos(sinusoid_table[:, 1::2])  # dim 2i+1

    return torch.FloatTensor(sinusoid_table).unsqueeze(0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants