赞
踩
原式(见1706.03762.pdf (arxiv.org))
合并化简得
参数解读:
以一个2个词,每个词维度维度为4的输入为例:
维度0(i=0) | 维度1(i=1) | 维度2(i=2) | 维度3(i=3) | |
词向量0(pos=0) | ||||
词向量1(pos=1) |
由上可见,位置编码与词向量所包含的值无关,仅与位置和维度大小有关。
代码示例如下:
- import numpy as np
-
-
- def PE(pos, i, dmodel):
- n = 10000
- if i % 2 == 0:
- # i is even
- return np.sin(pos / np.power(n, (i / dmodel)))
- else:
- # i is odd
- return np.cos(pos / np.power(n, ((i - 1) / dmodel)))
-
-
- sample = np.zeros((2, 4)) # 2 word vectors; each vector has four dimensions
- dmodel = sample.shape[1]
- for pos in range(sample.shape[0]):
- for i in range(sample.shape[1]):
- sample[pos][i] = np.round(PE(pos, i, dmodel), 5)
- print(sample)
-
- # output
- # [[0. 1. 0. 1. ]
- # [0.84147098 0.54030231 0.00999983 0.99995 ]]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。