代码
def positional_encoding(inputs,
num_units,
zero_pad = True,
scale = True,
scope = "positional_encoding",
reuse=None):
N,T = inputs.get_shape().as_list()
with tf.variable_scope(scope,reuse=True):
position_ind = tf.tile(tf.expand_dims(tf.range(T),0),[N,1])
position_enc = np.array([
[pos / np.power(10000, 2.*i / num_units) for i in range(num_units)]
for pos in range(T)])
position_enc[:,0::2] = np.sin(position_enc[:,0::2]) # dim 2i
position_enc[:,1::2] = np.cos(position_enc[:,1::2]) # dim 2i+1
lookup_table = tf.convert_to_tensor(position_enc)
if zero_pad:
lookup_table = tf.concat((tf.zeros(shape=[1,num_units]),lookup_table[1:,:]),0)
outputs = tf.nn.embedding_lookup(lookup_table,position_ind)
if scale:
outputs = outputs * num_units ** 0.5
return outputs