实现Transformers的Positional Encoding层。
使用Transformer架构中指定的正弦和余弦函数计算序列长度(位置)和模型维度(`d_model`)的定位编码。如果`position`为0,或`d_model`小于或等于0,则函数应返回-1。输出应为类型为'float16'的numpy数组。
在一行上输入两个整数,分别表示序列长度(位置)和模型维度(`d_model`),两个数之间用空格分隔。
输出一个numpy数组,表示位置编码。返回时指定dtype为np.float16
2 8
[[0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0], [0.84130859375, 0.099853515625, 0.01000213623046875, 0.0010004043579101562, 0.54052734375, 0.9951171875, 1.0, 1.0]]
1.对应的输入、输出已给出,您只用实现核心功能函数即可。2.支持numpy、scipy、pandas、scikit-learn库。
位置编码不是直接拼接,而是交错拼接起来的,这题答案就是有问题
def pos_encoding(position: int, d_model: int) -> np.ndarray:
if position <= 0 or d_model <= 0:
return np.array(-1)
positions = np.arange(position, dtype=np.float32)
div_term = np.exp(- np.arange(0, d_model, 2, dtype=np.float32) * (np.log(10000) / d_model) )
pos = positions[:,None] * div_term[None,:]
x, y = np.sin(pos), np.cos(pos)
pos_embed = np.empty((position, d_model), dtype=np.float16)
pos_embed[:,::2] = x
pos_embed[:,1::2] = y
# pos_embed = np.concatenate([x, y], axis=1)
return pos_embed.astype(np.float16)