python-tensorflowHow can I use Python TensorFlow with a GPU?
Using Python TensorFlow with a GPU is a great way to increase the speed of your machine learning algorithms. The following example code block shows how to set up a TensorFlow session to use a GPU:
import tensorflow as tf
# Creates a graph.
with tf.device('/gpu:0'):
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))
Output example
[[22. 28.]
[49. 64.]]
The code above does the following:
- Imports the TensorFlow library.
- Creates a graph with a multiplication operation using two constants.
- Creates a session with the log_device_placement set to True.
- Runs the operation and prints the output.
This example code shows how to set up a TensorFlow session to use a GPU, but there are many more parameters and options to consider when using a GPU with TensorFlow. For more information, see the following links:
More of Python Tensorflow
- How do I resolve a SymbolAlreadyExposedError when the symbol "zeros" is already exposed as () in TensorFlow Python util tf_export?
- How can I use YOLOv3 with Python and TensorFlow?
- How can I determine the best Python version to use for TensorFlow?
- ¿Cómo implementar reconocimiento facial con TensorFlow y Python?
- How can I use Tensorflow 1.x with Python 3.8?
- How do I check which version of TensorFlow I am using with Python?
- How can I install and use TensorFlow on a Windows machine using Python?
- How can I free up GPU memory when using Python and TensorFlow?
- How do I use a Python Tensorflow Autoencoder?
- How can I use Python and TensorFlow to handle illegal hardware instructions in Zsh?
See more codes...