Tensorflow - 如何为tf.Estimator()CNN使用GPU而不是CPU

时间:2017-11-14 03:28:38

标签: python tensorflow tensorflow-estimator

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

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

那么我应该将它放在main()的{​​{1}}函数中,还是我用于估算器的模型函数中?

编辑:如果这有帮助,这是我的tf.app功能:

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

3 个答案:

答案 0 :(得分:0)

使用估算器,没有任何声明,如

sess = tf.Session(config = xxxxxxxxxxxxx)

既不是

的陈述
sess.run()

所以......遗憾的是,张量流网络中没有完整的文档。 我正在尝试使用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上运行。

答案 1 :(得分:0)

您可以将它放在模型函数的开头,即,当您定义模型时,您应该写:

model = Model(rn50.input, 
        Dense(len(possible_labels), activation='softmax')
        (rn50.get_layer(layer_name).output))

但是,我希望tensorflow能够自动为你的模型使用gpu。您可能想检查是否已正确检测到它:

import numpy 
try: from StringIO import StringIO
except ImportError: from io import StringIO

foo = '16.72083152\t12.91868366\t14.37818919\n16.9504402\t7.81951173\t12.81342726\n'
fn = StringIO.StringIO(foo) #make a file object from the string
data = numpy.loadtxt(fn) #use loadtxt with default settings.

答案 2 :(得分:0)

我想知道使用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")

    ......