Using GPU with TensorFlow: Specifying Device with tf.device Function

TensorFlow programs can specify the device for each operation using the tf.device function.

This device can be a local CPU or GPU, or it can be a remote server.

TensorFlow assigns a name to each available device, and the tf.device function can specify the device for executing operations using the device’s name. For example, the name for CPU in TensorFlow is /cpu:0.

By default, even if the machine has multiple CPUs, TensorFlow does not differentiate between them; all CPUs use /cpu:0 as their name.

— The names of different GPUs on a machine are different, with the nth GPU in TensorFlow being named /gpu:n.

— For instance, the first GPU is named /gpu:0, the second GPU is named /gpu:1, and so on.

— TensorFlow provides a convenient way to see the device running each operation.

— When creating a session, you can set the log_device_placement parameter to print the device running each operation.

Using GPU with TensorFlow: Specifying Device with tf.device Function

Please see the example below:

The following program demonstrates the use of the log_device_placement parameter, and you can run the code directly on the machine:

–import tensorflow as tf

–a = tf.constant([1.0, 2.0, 3.0], shape=[3], name=’a’)

–b = tf.constant([1.0, 2.0, 3.0], shape=[3], name=’b’)

–c = a + b

–# Use log_device_placement parameter to output the device running each operation.

–sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

–print(sess.run(c))

In the above code, the TensorFlow program includes the log_device_placement=True parameter when creating the session, so it will output the device running each operation to the screen.

— In addition to seeing the final calculation result, you can also see outputs like “add: /job:localhost/replica:0/task:0/cpu:0”

— These outputs show the device executing each operation. For example, the addition operation add is run by the CPU because its device name includes /cpu:0.

— In a TensorFlow environment configured with a GPU, if an operation does not explicitly specify the running device, TensorFlow will prioritize using the GPU.

Running the above code on a machine without a GPU yields the following output:

–Device mapping: no known devices.

–add: /job:localhost/replica:0/task:0/cpu:0

–b: /job:localhost/replica:0/task:0/cpu:0

–a: /job:localhost/replica:0/task:0/cpu:0

–[ 2. 4. 6.]

–”’

Institute of Computing Technology, Chinese Academy of Sciences Training Center

Learn more about high-quality courses

Please visit the official website: www.tcict.cn

Or call the consultation hotline: 010-82661221

Using GPU with TensorFlow: Specifying Device with tf.device Function

Long press the image above, scan the QR code, and follow the official WeChat!

Leave a Comment