#coding=utf-8
import numpy as np
import tensorflow as tf
def get_angles(pos, i, d_model):
    """
    parameter:
        pos:(position * 1)
        i:(1 * d)
    return:(position * d)
    """
    angle_rates = 1 / np.power(10000, (2 * (i//2)) / np.float32(d_model))
    return pos * angle_rates
def positional_encoding(position, d_model):
#     print(np.arange(position)[:, np.newaxis])
#     print(np.arange(d_model)[np.newaxis, :])
    angle_rads = get_angles(np.arange(position)[:, np.newaxis],
                          np.arange(d_model)[np.newaxis, :],
                          d_model)
    # 将 sin 应用于数组中的偶数索引（indices）；2inohup python -u run.py --train --gpu 1 --batch_size 64 --nohup python -u run.py --train --gpu 1 --batch_size 64 --model ioi --learning_rate 0.0001 --corpus ubuntu > ioi_ubuntu.log &model ioi --learning_rate 0.0001 --corpus ubuntu > ioi_ubuntu.log &
    angle_rads[:, 0::2] = np.sin(angle_rads[:, 0::2])
    # 将 cos 应用于数组中的奇数索引；2i+1
    angle_rads[:, 1::2] = np.cos(angle_rads[:, 1::2])
    pos_encoding = angle_rads[np.newaxis, ...]
    return tf.cast(pos_encoding, dtype=tf.float32)


        
    
