微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 正弦嵌入 – 注意力就是你所需要的

Attention Is All You Need中,作者实现了位置嵌入(添加了关于单词在序列中的位置的信息).为此,他们使用正弦嵌入:

PE(pos,2i) = sin(pos/10000**(2*i/hidden_units))
PE(pos,2i+1) = cos(pos/10000**(2*i/hidden_units))

pos是位置,我是维度.它必须导致形状[max_length,embedding_size]的嵌入矩阵,即,给定序列中的位置,它返回PE [position,:]的张量.

我找到了Kyubyong’s实现,但我不完全理解它.

我尝试通过以下方式实现它:

hidden_units = 100 # Dimension of embedding
vocab_size = 10 # Maximum sentence length
# Matrix of [[1, ..., 99], [1, ..., 99], ...]
i = np.tile(np.expand_dims(range(hidden_units), 0), [vocab_size, 1])
# Matrix of [[1, ..., 1], [2, ..., 2], ...]
pos = np.tile(np.expand_dims(range(vocab_size), 1), [1, hidden_units])
# Apply the intermediate funcitons
pos = np.multiply(pos, 1/10000.0)
i = np.multiply(i, 2.0/hidden_units)
matrix = np.power(pos, i)
# Apply the sine function to the even colums
matrix[:, 1::2] = np.sin(matrix[:, 1::2]) # even
# Apply the cosine function to the odd columns
matrix[:, ::2] = np.cos(matrix[:, ::2]) # odd
# Plot
im = plt.imshow(matrix, cmap='hot', aspect='auto')

Result of matrix plot

我不明白这个矩阵如何提供有关输入位置的信息.有人可以先告诉我这是否是正确的计算方法,其次是什么原因呢?

谢谢.

解决方法:

我在pytorch implementation找到了答案:

# keep dim 0 for padding token position encoding zero vector
position_enc = np.array([
    [pos / np.power(10000, 2*i/d_pos_vec) for i in range(d_pos_vec)]
    if pos != 0 else np.zeros(d_pos_vec) for pos in range(n_position)])

position_enc[1:, 0::2] = np.sin(position_enc[1:, 0::2]) # dim 2i
position_enc[1:, 1::2] = np.cos(position_enc[1:, 1::2]) # dim 2i+1
return torch.from_numpy(position_enc).type(torch.FloatTensor)

其中d_pos_vec是嵌入维度,n_position是最大序列长度.

编辑:

在论文中,作者说嵌入矩阵的这种表示允许“模型推断到序列长度比训练期间遇到的长度更长”.

两个位置之间的唯一区别是pos变量.检查下面的图像以获得图形表示.

embeddings

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐