【问题标题】:Multi GPU seems not work on TensorFlow1.0多 GPU 似乎不适用于 TensorFlow1.0
【发布时间】: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


    【解决方案1】:

    查看您的程序,您正在不同的 GPU 设备上创建多个并行子图(通常称为“塔”),但在第一个 for 循环的每次迭代中都会覆盖 prediction 张量:

    for core in range(num_cores):
      with tf.device('/gpu:%d'%core):
        prediction = model(example_batch)
    # ...
    for step in range(iter):
      myprediction = sess.run(prediction, feed_dict={example_batch:input_array})
    

    因此,当您调用 sess.run(prediction, ...) 时,您将只运行在第一个 for 循环的最终迭代中创建的子图,该循环仅在一个 GPU 上运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-02
      • 2020-04-04
      • 2023-04-06
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多