我具有要并行化的功能。
def sample_x(n, y, x, k_star, theta, psi, l, k, m, s, score):
p, score[n, l] = calculate_score(theta, psi, n, l)
x_nmk = np.random.multinomial(y[n, l], p / score[n, l])
if (n, l, k) in x:
if x[(n, l, k)] > 0:
m[n, l] += x_nmk - x[(n, l, k)]
s[l, k] += x_nmk - x[(n, l, k)]
k_star[n, l] = k
x[(n, l, k)] = x_nmk
但是,如果我按以下方式称呼,则x,k_star,m,s都不会改变。
def sample_x_all(x, y, psi, theta, m, s, k_star, score, pool):
k_star[:] = np.zeros((y.shape[0], y.shape[1]))
for k in range(psi.shape[0]):
for l in range(psi.shape[1]):
pool_x = partial(sample_x, y=y, x=x, k_star=k_star, theta=theta,
psi=psi, l=l, k=k, m=m, s=s, score=score)
pool.map(pool_x, np.arange(theta.shape[0]))
谁能告诉我为什么?我也不想最后写一个for循环并填写x。