我找不到一个从for循环到矢量化numpy操作的非常简单的转换中的错误。代码如下蟒蛇for循环和3D numpy矩阵添加之间的等效
for null_pos in null_positions:
np.add(singletree[null_pos, parent.x, :, :],
posteriors[parent.u, null_pos, :, :],
out=singletree[null_pos, parent.x, :, :])
既然是2D矩阵之间的简单相加,我概括成3D除了
np.add(singletree[null_positions, parent.x, :, :],
posteriors[parent.u, null_positions, :, :],
out=singletree[null_positions, parent.x, :, :])
的事情是,它出现的结果是不同的!你能明白为什么吗?
谢谢!
更新: 看来
singletree[null_positions, parent.x, :, :] = \
posteriors[parent.u, null_positions, :, :] +
singletree[null_positions, parent.x, :, :]
解决了这个问题。这与添加操作有何不同? (除了分配一个新的矩阵,我感兴趣的语义方面)
输入数组的形状是什么? –
你可以假设:singletree是一个L×M×C×(C + 1)矩阵,后验是N×L×C×(C + 1)。看看我的更新(我会在一分钟后发布),因为它不依赖于尺寸,我相信 – diningphil