Gensoukyou1337
Gensoukyou1337

Reputation: 1527

Tensorflow - How to use the GPU instead of a CPU for tf.Estimator() CNNs

I think it was supposed to be used with with tf.device("/gpu:0"), but where do I put it? I don't think it's:

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

So should I put it in the main() function of the tf.app, or the model function I use for the estimator?

EDIT: If this helps, this is my main() function:

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])

As you can see, I have no explicit declaration of a session anywhere here, so where exactly do I put the with tf.device("/gpu:0")?

Upvotes: 8

Views: 8941

Answers (3)

jkjung13
jkjung13

Reputation: 944

I wondered whether using tf.contrib.distribute to specify the device placement strategy works.

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")

    ......

Upvotes: 0

jan
jan

Reputation: 320

You can put it at the beginning of your model function, i.e., when you define your model, you should write:

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

However, I would expect tensorflow to automatically use the gpu for your model. You may want to check whether it is properly detected:

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

Upvotes: 1

WinterZ
WinterZ

Reputation: 79

with estimator there isn't any statement like

sess = tf.Session(config = xxxxxxxxxxxxx)

neither a statement as

sess.run()

So...unfortunately there isn't a full documentation in the tensorflow web. I'm trying with the different options of 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}))

Try to work with this...Actually I'm working with something like your task so if I get some advances I will post it here.

Take a look here: https://github.com/tensorflow/models/blob/master/official/wide_deep/wide_deep.py In this example they are using the code showed above with the .replace statement to ensure that the model is running on CPU.

Upvotes: 0

Related Questions