np.choice와 chainer.utils.WalkerAlias의 속도 대결
코드
난수 생성
import numpy as np
import chainer
from time import time
import matplotlib.pyplot as plt
%matplotlib inline
loops = 10 # for taking average
verc_size = 10 ** np.arange(1, 7) # i
n_samples = 10 ** np.arange(1, 7) # j
t_choice = np.zeros((len(verc_size), len(n_samples)))
t_alias = np.zeros((len(verc_size), len(n_samples)))
t_aliasv = np.zeros((len(verc_size), len(n_samples)))
for i in range(len(verc_size)):
p = np.random.randint(100, size=verc_size[i])
p = p / p.sum()
# p = p.astype(np.float32) # for gpu?
c = np.random.randint(100, size=verc_size[i])
start_time = time()
for loop in range(loops):
sampler = chainer.utils.WalkerAlias(p)
end_time = time()
e_time = end_time - start_time
t_aliasv[i, :] = e_time / loops
for j in range(len(n_samples)):
# numpy choice
start_time = time()
for loop in range(loops):
res = np.random.choice(c, size=n_samples[j], p=p)
end_time = time()
e_time = end_time - start_time
print('choice', e_time, n_samples[j], verc_size[i])
t_choice[i, j] = e_time / loops
# chainer alisas
start_time = time()
for loop in range(loops):
res = sampler.sample(n_samples[j])
res = c[res]
end_time = time()
e_time = end_time - start_time
print('alias ', e_time, n_samples[j], verc_size[i])
t_alias[i, j] = e_time / loopsimport numpy as np
플롯
플롯fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(15, 5))
def myax(ax, title):
ax.set_yscale('log')
ax.set_xscale('log')
ax.set_ylim(1e-4, 1e2)
ax.set_title(title)
ax.set_xlabel('# samples')
ax.set_ylabel('time [s]')
for i in range(len(verc_size)):
ax[0].plot(n_samples, t_choice[i], label=verc_size[i])
myax(ax[0], 'np.choice')
ax[0].legend(loc='best')
ax[1].plot(n_samples, t_alias)
myax(ax[1], 'chainer.alias sampling only')
ax[2].plot(n_samples, t_alias + t_aliasv)
myax(ax[2], 'chainer.alias init + sampling')
결과
import numpy as np
import chainer
from time import time
import matplotlib.pyplot as plt
%matplotlib inline
loops = 10 # for taking average
verc_size = 10 ** np.arange(1, 7) # i
n_samples = 10 ** np.arange(1, 7) # j
t_choice = np.zeros((len(verc_size), len(n_samples)))
t_alias = np.zeros((len(verc_size), len(n_samples)))
t_aliasv = np.zeros((len(verc_size), len(n_samples)))
for i in range(len(verc_size)):
p = np.random.randint(100, size=verc_size[i])
p = p / p.sum()
# p = p.astype(np.float32) # for gpu?
c = np.random.randint(100, size=verc_size[i])
start_time = time()
for loop in range(loops):
sampler = chainer.utils.WalkerAlias(p)
end_time = time()
e_time = end_time - start_time
t_aliasv[i, :] = e_time / loops
for j in range(len(n_samples)):
# numpy choice
start_time = time()
for loop in range(loops):
res = np.random.choice(c, size=n_samples[j], p=p)
end_time = time()
e_time = end_time - start_time
print('choice', e_time, n_samples[j], verc_size[i])
t_choice[i, j] = e_time / loops
# chainer alisas
start_time = time()
for loop in range(loops):
res = sampler.sample(n_samples[j])
res = c[res]
end_time = time()
e_time = end_time - start_time
print('alias ', e_time, n_samples[j], verc_size[i])
t_alias[i, j] = e_time / loopsimport numpy as np
플롯
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(15, 5))
def myax(ax, title):
ax.set_yscale('log')
ax.set_xscale('log')
ax.set_ylim(1e-4, 1e2)
ax.set_title(title)
ax.set_xlabel('# samples')
ax.set_ylabel('time [s]')
for i in range(len(verc_size)):
ax[0].plot(n_samples, t_choice[i], label=verc_size[i])
myax(ax[0], 'np.choice')
ax[0].legend(loc='best')
ax[1].plot(n_samples, t_alias)
myax(ax[1], 'chainer.alias sampling only')
ax[2].plot(n_samples, t_alias + t_aliasv)
myax(ax[2], 'chainer.alias init + sampling')
결과
Reference
이 문제에 관하여(np.choice와 chainer.utils.WalkerAlias의 속도 대결), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/tttamaki/items/ee7264818ed8fe0a464f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)