Tensorflow with cpu is fine if the project does not involve in huge workload of calculation, and the installation for tensorflow-cpu is quite simple (just install it with command like “conda install tensorflow” or “pip install tensorflow”).
If we want to use tensorflow-gpu, we can follow the instructions below.
The order of setting up is:
- GPU driver
- Cuda
- Cudnn
- Tensorflow-gpu
The version of the GPU driver to be installed is determined according to what kind of GPU it is in our computer, and the correspondence between them can be found at NVIDIA website (https://www.geforce.com/drivers).
The last three components are relevant and most difficult part to be set. Cuda of a certain version can only link to cudnn of a corresponding version, and they support tensorflow of some certain versions. The correspondence between them can be found at the websites (for windows: https://www.tensorflow.org/install/source_windows; for ubuntu: https://www.tensorflow.org/install/source).
For windows:
For linux:
It is suggested to use python 3.5, because python 3.7 does not support tensorflow now and python 3.6 cannot fit several relevant packages. The tools of the old version may be out of fashion but they are stable. Do not get the newest conda, if python 3.5 is going to be installed. Find the one of a proper version, since the newest one only supports python 3.7 (June, 2019).
Before setting up, make a decision:
The first thing needs to be considered is for what purpose it will be used. If we want to run other’s codes using tensorflow, we would better to know when this code is released before setting up an environment for that, because we may need to install tensorflow of an old version which is used in that code. That means we cannot install the latest cuda and cudnn.
Then we have to make a decision:
a. set up the environment with those components of an older version;
b. set up the environment with those components of the newest version and then re-write some parts of the code to fit the environment.
It is up to you. However, this decision is worthy of consideration, and the reason is the methods of package application may be slightly different as before, which means we need to learn how to use these new-version packages if we re-write codes. For instance, a number of functions in keras change after the version 2.1.6.
Here is an example to install tensorflow:
-
install GPU driver (many computers have it in default)
-
cuda
a. In the installation process, we can choose not to install NVIDIA Accelerated Graphics Driver, because we already have it.b. After the installation, we type the command “nvcc --verison” for checking whether it is installed successfully or not.
-
Cudnn
Decompress the cudnn file downloaded, and then move the three sub-files “bin”, “include” and “lib” into the corresponding files (“bin”, “include”, “lib”) in the directory “C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0”. Add this directory in system path. -
tensorflow
It is strongly suggested to install tensorflow via conda. The visual C++ build tools 2015 is the pre-requisite of tensorflow, and it can be installed automatically with tensorflow by conda “conda install -c conda-forge tensorflow”.
This is how to set up the environment for tensorflow projects, but it is not exactly all the stuff needed. In practice, there are some other packages that need to be installed. One simple way to deal with it is just to run the project and then solve the instantiated warning or error one by one.
Good luck.