How do I use TensorFlow GPU?
Solution 1
Follow this tutorial Tensorflow GPU I did it and it works perfect.
Attention! - install version 9.0! newer version is not supported by Tensorflow-gpu
Steps:
- Uninstall your old tensorflow
- Install tensorflow-gpu
pip install tensorflow-gpu
- Install Nvidia Graphics Card & Drivers (you probably already have)
- Download & Install CUDA
- Download & Install cuDNN
- Verify by simple program
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
Solution 2
The 'new' way to install tensorflow GPU if you have Nvidia, is with Anaconda. Works on Windows too. With 1 line.
conda create --name tf_gpu tensorflow-gpu
This is a shortcut for 3 commands, which you can execute separately if you want or if you already have a conda environment and do not need to create one.
-
Create an anaconda environment
conda create --name tf_gpu
-
Activate the environment
conda activate tf_gpu
-
Install tensorflow-GPU
conda install tensorflow-gpu
You can use the conda environment.
Solution 3
Follow the steps in the latest version of the documentation. Note: GPU and CPU functionality is now combined in a single tensorflow package
pip install tensorflow
# OLDER VERSIONS pip install tensorflow-gpu
https://www.tensorflow.org/install/gpu
This is a great guide for installing drivers and CUDA if needed: https://www.quantstart.com/articles/installing-tensorflow-22-on-ubuntu-1804-with-an-nvidia-gpu/
Solution 4
First you need to install tensorflow-gpu, because this package is responsible for gpu computations. Also remember to run your code with environment variable CUDA_VISIBLE_DEVICES = 0 (or if you have multiple gpus, put their indices with comma). There might be some issues related to using gpu. if your tensorflow does not use gpu anyway, try this
Solution 5
For conda
environment.
-
conda search tensorflow
to search the available versions of tensorflow. The ones that havemkl
are optimized for CPU. You can choose the ones withgpu
. - Then check the version of your cuda using
nvcc --version
and find the proper version of tensorflow in this page, according to your version of cuda. - For example, for cuda/10.1,and python3.8, you can use
conda install tensorflow=2.2.0=gpu_py38hb782248_0
Guruku
Updated on March 31, 2022Comments
-
Guruku about 2 years
How do I use TensorFlow GPU version instead of CPU version in Python 3.6 x64?
import tensorflow as tf
Python is using my CPU for calculations.
I can notice it because I have an error:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
I have installed tensorflow and tensorflow-gpu.
How do I switch to GPU version?
-
Jorge Leitao almost 6 yearsHave you tried uninstalling
tensorflow
and just keep thetensorflow-gpu
installed? -
CtrlAltF2 almost 6 yearsTry downloading CUDA and installing the GPU version.
-
iacolippo almost 6 yearsThat's just a warning, if you have an NVIDIA GPU, Tensorflow-gpu will automatically use that. To know more and how to disable the warning: stackoverflow.com/a/47227886/4892874 To check that you're using the GPU:
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
-
Mohan Radhakrishnan almost 6 yearsAre you using Anaconda ? if you are you have to switch the interpreter of the particular environment in PyCharm.
-
Guruku almost 6 yearsDevice mapping: no known devices. I do not use Anaconda
-
Mathieu almost 6 yearsWhat is your GPU?
-
Guruku almost 6 yearsNVIDIA 940mx, it is relative new Nvidia card. @JorgeLeitão yes, then I have no TensorFlow
-
-
Guruku almost 6 yearsSo, CUDA 9.2 can be the problem? Should I install 9.0 then? Yesterday I had an error after installing CUDA 9.2. It said that I need DLL from CUDA 9.0 and I knew it was using GPU version, today all the time I see the error about CPU, so it is using CPU. I will try it step by step, thanks.
-
Ashwel almost 6 yearsYea, i had same problem about 2 month ago. I tried CUDA 9.1 and Tensorflow doesn't support it..Then i uninstall everything and repeat all steps with CUDA 9.0 and now it works without problems.
-
Guruku almost 6 yearsMaybe I should ask in another question, but is tensorflow on GPU is faster than CPU at your PC? I tried with linear regression model and for 10000 epos and step 0.01 my CPU can calculate it in 13 second and when I use GPU it takes 39 second.
-
Ashwel almost 6 yearsOfc i have CPU i7 8700k and GPU nVidia GTX 1060 STRING 6GB and on GPU is faster 20 times aproximate.
-
Paul Bendevis almost 4 yearsNote that newer versions of tensorflow combine the cpu and gpu packages together into a single package. tensorflow.org/install/gpu
-
dato nefaridze almost 4 yearsI did the same, and does tensors need to be uploaded on gpu, like they need in pytorch?
-
Rodrigo Hjort almost 3 yearsIn 2:
conda activate tf_gpu