我想在 for 循环中多次对我的输入运行 keras 模型的预测。有没有办法并行化这个 for 循环,因为循环的每次迭代都是相互独立的?我只想将每次迭代的预测存储在 mc_predictions
数组中。
mc_predictions = []
for i in range(100):
y_p = model.predict(x)
mc_predictions.append(y_p)
我在模型中使用了 dropout 层,并将训练属性设置为 True。因此,每次我都会得到不同的结果,因为不同的神经元在每次迭代中都会被停用。我在这里使用蒙特卡洛估计。
最佳答案
Q : "Is there a way to parallelize this for loop as each iteration of the loop is independent of the other?"
好吧,如果不深入了解实际的模型
- 实例就不会。
The
model
-instance could easily be a state-full-system and changing some part of its internal state by each call to the.predict()
-method ... so one cannot be sure a-priori, without re-inspecting themodel
's implementation, could one?
– user3666197 6 min ago
如预告片所示was添加“...不同的神经元将在每次迭代中停用。”情况已经改变。
没有办法让定义的纯-[SERIAL]
过程“发生”成为True-[PARALLEL]
。
Q.E.D.
But is this really a pure serial process? I mean, each iteration is independent. What neurons get deactivated in an iteration does not matter as long as random neurons are getting deactivated.
– Crazzay1903 4 hours ago
在最有可能的情况下是,这是一个纯粹的[SERIAL]
过程(除非以某种方式并被彻底证明不是 - 但是,那么它也会陷入无法保持可重复和重新验证准备的科学的陷阱(有人可能会认为它可以合法地成为一种主要是非确定性的随机过程 - 公平,好的 - 然而,即使如此,证明已经实现了如此鲁棒的非确定性随机过程,以便能够证明它不会失去任何最终的随机性质量属性在某些 True-[PARALLEL]
流程实现下(如果经过尝试并证明是这样的话),但是 .预测()
- 方法必须而且不能,否则,比任何严格的、定量支持的公平科学方法更像是一种洞察力,不是吗?)这不是一个目标,不是吗?) .
Q.E.D.
关于python - 在 for 循环中并行执行模型预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62211686/