0
我想找到一种方法来加快我的代码。TensorFlow并行CPU与Numpy
总之,我有一个训练有素的模型,我使用类似的东西来获得预测,对它们进行排序并输出排名。
def predict(feed_dict, truth):
# Feed dict contains about 10K candidates to obtain scores
pred = self.sess.run([self.mdl.predict_op], feed_dict)
pred = np.array(pred)
# With the scores, I sort them by likelihood
sort = np.argsort(pred)[::-1]
# I find the rank of the ground truth
rank = np.where(sort==truth)[0][0] + 1
return rank
但是,这个过程非常缓慢。我有大约10K个测试样本。我相信会话不适合python中的标准多处理库,而multi-cpu/gpu支持仅适用于tensorflow操作。
是否有任何优雅的方式来加速通过多处理?或者我必须将它作为TF中计算图的一部分来实现。
非常感谢!
哪一部分是慢? –
顺便提一下,'tf.nn.top_k(pred)[1]'和你的'np.argsort'行是一样的。如果您将所有内容都转换为TF图,则不需要多处理 - 并行'session.run'调用可以在同一进程中从不同的Python线程启动。 –
缓慢来自我必须每次在有效或测试集上调用10K +次的事实。 – op10no4