TensorFlow GPU management
In TensorFlow the supported devices are represented as strings. For example:
/cpu:0
: The CPU of your machine/gpu:0
: The GPU of your machine, if you have one/gpu:1
: The second GPU of your machine, and so on
The execution flow gives priority when an operation is assigned to a GPU device.
Programming example
To use a GPU in your TensorFlow program, just type the following:
with tf.device("/gpu:0"):
Followed by the setup operations. This line of code will create a new context manager, telling TensorFlow to perform those actions on the GPU.
Let's consider the following example, in which we want to execute the following sum of two matrices, An + Bn
.
Define the basic imports:
import numpy as np import tensorflow as tf import datetime
We can configure a program to find out which devices your operations and tensors are assigned. To realize this, we'll create a session with the following log_device_placement
parameter set to True
:
log_device_placement = True
Then we fix the n
parameter...