【发布时间】:2017-03-09 17:12:37
【问题描述】:
我正在使用 TensorFlow 1.0,并且我开发了一个简单的程序来测量性能。我有一个愚蠢的模型如下
def model(example_batch):
h1 = tf.layers.dense(inputs=example_batch, units=64, activation=tf.nn.relu)
h2 = tf.layers.dense(inputs=h1, units=2)
return h2
还有一个运行模拟的简单函数:
def testPerformanceFromMemory(model, iter=1000 num_cores=2):
example_batch = tf.placeholder(np.float32, shape=(64, 128))
for core in range(num_cores):
with tf.device('/gpu:%d'%core):
prediction = model(example_batch)
init_op = tf.global_variables_initializer()
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
sess.run(init_op)
tf.train.start_queue_runners(sess=sess)
input_array = np.random.random((64,128))
for step in range(iter):
myprediction = sess.run(prediction, feed_dict={example_batch:input_array})
如果我运行 python 脚本,然后运行 nvidia-smi 命令,我可以看到 GPU0 正在以很高的使用率运行,但 GPU1 的使用率为 0%。
我读到这个:https://www.tensorflow.org/tutorials/using_gpu 和这个:https://github.com/tensorflow/models/blob/master/tutorials/image/cifar10/cifar10_multi_gpu_train.py,但我不知道为什么我的示例不能在多 GPU 中运行。
PS 如果我从 tensorflow 存储库中加载 ciphar 10 示例,它会以多 GPU 模式运行。
编辑:正如 mrry 所说,我正在覆盖预测,所以我在这里以正确的方式发布:
def testPerformanceFromMemory(model, iter=1000 num_cores=2):
example_batch = tf.placeholder(np.float32, shape=(64, 128))
prediction = []
for core in range(num_cores):
with tf.device('/gpu:%d'%core):
prediction.append([model(example_batch)])
init_op = tf.global_variables_initializer()
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
sess.run(init_op)
tf.train.start_queue_runners(sess=sess)
input_array = np.random.random((64,128))
for step in range(iter):
myprediction = sess.run(prediction, feed_dict={example_batch:input_array})
【问题讨论】:
标签: python machine-learning tensorflow