Numpy 向量化 python for 循环

标签 numpy vectorization

这是使用 Keras 库创建模型的代码片段:

    for state, action, reward, next_state, done in minibatch:
        target = reward
        if not done:
            target = (reward + self.gamma *
                      np.amax(self.model.predict(next_state)[0]))
        target_f = self.model.predict(state)
        #print (target_f)
        target_f[0][action] = target
        self.model.fit(state, target_f, epochs=1, verbose=0)

我正在尝试对其进行矢量化。我认为唯一的方法是: 1. 创建一个 numpy 表,每行 = (state, action,reward, next_state, done, target)。因此,将会有“小批量”行数。 2. 根据其他列更新目标列(使用屏蔽数组):

target[done==True] ==reward
target[done==False] == reward + self.gamma 
*np.amax(self.model.predict(next_state)[0])
  • 现在更新 self.model.fit(state, target_f, epochs=1, verbose=0)
  • 注意:状态是 8 维的,因此状态向量有 8 个元素。

    尽管经过数小时的努力,我仍无法正确编码。 是否可以实际矢量化这段代码?

    最佳答案

    你们很接近!假设 minibatch 是一个 np.array:

    首先找到done为true的所有索引。假设 done 的索引号为 4。

    minibatch_done=minibatch[np.where(minibatch[:,4]==True)]
    minibatch_not_done=minibatch[np.where(minibatch[:,4]==False)]
    

    现在我们用它来有条件地更新小批量矩阵。假设索引 2 是 reward,索引 3 是 next_state

    target = np.empty((minibatch.shape[0]))
    n_done = minibatch_done.shape[0]
    # First half (index 0...n_done)
    target[:n_done] = minibatch_done[:,2]+self.gamma*np.amax(self.model.predict(minibatch_done[:,3]))
    target[n_done:] = minibatch_not_done[:,2]
    

    这就是你想要的:)

    编辑:修复了目标问题中的索引错误

    关于Numpy 向量化 python for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50975430/

    相关文章:

    python - 取消引用 numpy 数组或列表的 void 指针

    c++ - 如何使msvc向量化浮点加法?

    python - 用python按列乘​​以两个矩阵

    c# - 如何使用矢量 SSE 操作将图像像素数据的字节数组转换为灰度

    python - 有效地删除 numpy 图像数组的行/列

    python - 如何从对角线生成值来填充矩阵

    python - 无法导入 cv2; "DLL load failed"

    NumPy vectorize() 或 dot() 出现错误

    matlab - 在Matlab中高效计算元胞数组元素之间的成对交集

    python - 循环 clump_masked 索引