python 手动实现transformer中的绝对位置编码

PE(pos,2i) = sin(pos/10000^(2i/d_model))

PE(pos,2i+1) = cos(pos/10000^(2i/d_model))

实现主要是使用numpy的分片方法,提高效率。

import numpy as np
def getPosEncodingMatrix(max_len,d_emb):
    pos_enc = np.array([[pos/np.power(10000,2*(j//2)/d_emb) for j in range(d_emb)] if pos != 0 else np.zeros(d_emb) for pos in range(max_len)])
    pos_enc[1:,0::2] = np.sin(pos_enc[1:,0::2])
    pos_enc[1:,1::2] = np.cos(pos_enc[1:,1::2])
    return pos_enc
    
a = getPosEncodingMatrix(15,6)

你可能感兴趣的:(pytorch,NLP,bert)