使用不同的输入和相同的占位符运行并行运算 [英] Run parallel op with different inputs and same placeholder

查看:65
本文介绍了使用不同的输入和相同的占位符运行并行运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须同时计算多个精度.

I have the necessity to calculate more then one accuracy in the same time, concurrently.

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

该代码段与 TensorFlow 教程中的 mnist 示例相同,但没有:

The piece of code is the same of the mnist example in the tutorial of TensorFlow but instead of having:

W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

我有两个Placeolder,因为我已经计算并存储了它们.

I have two placeolder because I already calculated and stored them.

W = tf.placeholder(tf.float32, [784, 10])
b = tf.placeholder(tf.float32, [10])

我想用我Aready拥有的值填充网络,然后计算精度,这对于我加载的每个网络都必须发生.

I want to fill the network with the values I aready have and then calculate the accuracy and this have to happen for each network I loaded.

因此,如果我加载20个网络,则我想并行计算每个网络的准确性.会话运行是否可以通过不同的输入执行相同的操作?

So if I load 20 networks I want to calculate in parallel the accuracy for each one. There is a way with the session run to execute the same operation with different input?

推荐答案

您有多种选择可以使事情并行发生:

You have multiple options to make things happen in parallel:

我认为最后一个是最简单的,因此我在下面提供了一些示例代码来帮助您入门:

I think the last one is the easiest, so I've included a bit of sample code below to get you started:

import tensorflow as tf

def construct_accuracy_calculation(i):
  W = tf.placeholder(tf.float32, [784, 10], name=("%d_W" % i))
  b = tf.placeholder(tf.float32, [10], name=("%d_b" % i))
  # ...
  correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
  accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  return (W, b, accuracy)


def main():
  accuracy_computations = []
  feed_dict={}
  for i in xrange(NUM_NETWORKS):
    (W, b) = load_network(i)
    (W_op, b_op, accuracy) = construct_accuracy_calculation(i)
    feed_dict[W_op] = W
    feed_dict[b_op] = b
    accuracy_computations.append(accuracy)

  # sess = ...
  accuracy_values = sess.run(accuracy_computations, feed_dict=feed_dict)

if __name__ == "__main__":
  main()

这篇关于使用不同的输入和相同的占位符运行并行运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆