python-tensorflowHow can I use a GPU with Python TensorFlow?
Using a GPU with Python TensorFlow is relatively straightforward and can provide a significant speedup over using a CPU. To use a GPU with TensorFlow, you must have a supported GPU with a compatible driver installed.
Example code to check if the GPU is available and visible to TensorFlow:
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
Output example
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 12097170122906550962, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 4930941747
locality {
bus_id: 1
links {
}
}
incarnation: 14582080206794650090
physical_device_desc: "device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1"]
The code above prints a list of local devices, including the CPU and any GPUs available.
To use a GPU with TensorFlow, you must specify which GPU to use when creating a session. Example code to create a session on GPU 0:
import tensorflow as tf
with tf.Session() as sess:
with tf.device("/GPU:0"):
# Create model
...
# Run model
...
The code above creates a session on GPU 0, and any operations created within that session will be placed on GPU 0.
Helpful links
More of Python Tensorflow
- How can I use Python and TensorFlow to handle illegal hardware instructions in Zsh?
- How do I resolve a SymbolAlreadyExposedError when the symbol "zeros" is already exposed as () in TensorFlow Python util tf_export?
- ¿Cómo implementar reconocimiento facial con TensorFlow y Python?
- How can I use Python and TensorFlow to build a number recognition system?
- How can I use Python and TensorFlow to implement YOLO object detection?
- How can I compile Python TensorFlow code online?
- How can I use Python and TensorFlow to implement YOLOv4?
- How can I use Python TensorFlow in W3Schools?
- How can I install and use TensorFlow on a Windows machine using Python?
- How can I use TensorFlow 2.x to optimize my Python code?
See more codes...