【问题标题】:Tensorflow - How to use the GPU instead of a CPU for tf.Estimator() CNNsTensorflow - 如何为 tf.Estimator() CNN 使用 GPU 而不是 CPU
【发布时间】:2017-11-14 03:28:37
【问题描述】:

我认为它应该与with tf.device("/gpu:0") 一起使用,但我应该把它放在哪里?我不认为是:

with tf.device("/gpu:0"):
    tf.app.run()

那么我应该把它放在tf.appmain() 函数中,还是我用于估计器的模型函数中?

编辑:如果这有帮助,这是我的main() 函数:

def main(unused_argv):
  """Code to load training folds data pickle or generate one if not present"""

  # Create the Estimator
  mnist_classifier = tf.estimator.Estimator(
      model_fn=cnn_model_fn2, model_dir="F:/python_machine_learning_codes/tmp/custom_age_adience_1")

  # Set up logging for predictions
  # Log the values in the "Softmax" tensor with label "probabilities"
  tensors_to_log = {"probabilities": "softmax_tensor"}
  logging_hook = tf.train.LoggingTensorHook(
      tensors=tensors_to_log, every_n_iter=100)

  # Train the model
  train_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"x": train_data},
      y=train_labels,
      batch_size=64,
      num_epochs=None,
      shuffle=True)
  mnist_classifier.train(
      input_fn=train_input_fn,
      steps=500,
      hooks=[logging_hook])

  # Evaluate the model and print results
  """Code to load eval fold data pickle or generate one if not present"""

  eval_logs = {"probabilities": "softmax_tensor"}
  eval_hook = tf.train.LoggingTensorHook(
      tensors=eval_logs, every_n_iter=100)
  eval_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"x": eval_data},
      y=eval_labels,
      num_epochs=1,
      shuffle=False)
  eval_results = mnist_classifier.evaluate(input_fn=eval_input_fn, hooks=[eval_hook])

如您所见,我在这里没有明确声明会话,那么我究竟应该把with tf.device("/gpu:0") 放在哪里?

【问题讨论】:

    标签: python tensorflow tensorflow-estimator


    【解决方案1】:

    你可以把它放在你的模型函数的开头,也就是说,当你定义你的模型时,你应该写:

    def cnn_model_fn2(...):
        with tf.device('/gpu:0'):
            ...
    

    但是,我希望 tensorflow 会自动将 gpu 用于您的模型。您可能需要检查它是否被正确检测到:

    from tensorflow.python.client import device_lib
    device_lib.list_local_devices()
    

    【讨论】:

      【解决方案2】:

      使用估算器没有任何类似的语句

      sess = tf.Session(config = xxxxxxxxxxxxx)
      

      都不是声明

      sess.run()
      

      所以...不幸的是,tensorflow 网络中没有完整的文档。 我正在尝试使用 RunConfig 的不同选项

      # Create a tf.estimator.RunConfig to ensure the model is run on CPU, which
      # trains faster than GPU for this model.
      run_config = tf.estimator.RunConfig().replace(
              session_config=tf.ConfigProto(log_device_placement=True,
                                            device_count={'GPU': 0}))
      

      尝试处理这个......实际上我正在处理类似你的任务,所以如果我得到一些进展,我会在这里发布。

      看这里: https://github.com/tensorflow/models/blob/master/official/wide_deep/wide_deep.py 在此示例中,他们使用上面显示的代码和 .replace 语句来确保模型在 CPU 上运行。

      【讨论】:

      • 是的,我正在这里寻找更新。链接失效了。
      【解决方案3】:

      我想知道使用tf.contrib.distribute 指定设备放置策略是否有效。

      def main(unused_argv):
          """Code to load training folds data pickle or generate one if not present"""
      
          strategy = tf.contrib.distribute.OneDeviceStrategy(device='/gpu:0')
          config = tf.estimator.RunConfig(train_distribute=strategy)
      
          # Create the Estimator
          mnist_classifier = tf.estimator.Estimator(
              model_fn=cnn_model_fn2,
              config=config,
              model_dir="F:/python_machine_learning_codes/tmp/custom_age_adience_1")
      
          ......
      

      【讨论】:

        猜你喜欢
        • 2021-12-20
        • 2021-03-23
        • 1970-01-01
        • 1970-01-01
        • 2017-10-11
        • 2018-05-18
        • 2011-04-23
        • 1970-01-01
        • 2021-10-14
        相关资源
        最近更新 更多