Python循环中的竞争条件

2024-09-30 00:24:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个求和值的循环。我的任务是使循环平行。 问题:使用aray循环,其中i元素依赖于i-1。 所以在每次迭代中,我们必须有(i)和(i-1)值。 这是我的密码:

def sumLoss(xs, hs, ys, ps, t):

  xs[t] = np.zeros((vocab_size,1)) # encode in 1-of-k representation
  xs[t][inputs[t]] = 1
  hs[t] = np.tanh(np.dot(Wxh, xs[t]) + np.dot(Whh, hs[t-1]) + bh) # hidden state
  ys[t] = np.dot(Why, hs[t]) + by # unnormalized log probabilities for next chars
  ps[t] = np.exp(ys[t]) / np.sum(np.exp(ys[t])) # probabilities for next chars

  return -np.log(ps[t][targets[t],0])

这就是我解决问题的可悲尝试:

res = Parallel(n_jobs=num_cores)(delayed(sumLoss)(xs, hs, ys, ps, t) for t in range(len(inputs)))

当然,这是一个完全错误的决定,而且我对同步原语并不熟悉。你知道吗


Tags: inlogfornpdotnextpshs

热门问题