In [1]:
%matplotlib inline
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import seaborn as sns
In [2]:
def make_position_encoding(xp, batch, length, n_units, f=10000.):
assert(n_units % 2 == 0)
position_block = xp.broadcast_to(
xp.arange(length)[None, None, :],
(batch, n_units // 2, length)).astype('f')
unit_block = xp.broadcast_to(
xp.arange(n_units // 2)[None, :, None],
(batch, n_units // 2, length)).astype('f')
rad_block = position_block / (f * 1.) ** (unit_block / (n_units // 2))
sin_block = xp.sin(rad_block)
cos_block = xp.cos(rad_block)
emb_block = xp.concatenate([sin_block, cos_block], axis=1)
return emb_block
In [3]:
sns.set()
a = make_position_encoding(np, 1, 100, 512, 10000)[0]
plt.figure(figsize=(20, 10))
sns.heatmap(a, cmap='afmhot')
Out[3]:
In [4]:
sns.set()
a = make_position_encoding(np, 1, 100, 512, 10000)[0]
plt.figure(figsize=(20, 10))
sns.heatmap(a, cmap="bwr")
Out[4]:
In [5]:
# change 10000 -> 100
sns.set()
# a = make_position_encoding(np, 1, 100, 512, 10000)[0]
a = make_position_encoding(np, 1, 100, 512, 100)[0]
plt.figure(figsize=(20, 10))
sns.heatmap(a, cmap="bwr")
Out[5]:
In [ ]: