TensorFlow Installation Tutorial
TensorFlow Installation Tutorial
TensorFlow is a symbolic math system based on data flow programming, widely used for programming implementations of various machine learning algorithms. Its predecessor is Google’s neural network algorithm library DistBelief.
Now, let’s learn how to install TensorFlow.
Preparation
-
On the TensorFlow official website, it is recommended to use Python versions 3.6-3.9. If you are currently using a version above 3.10, you may encounter compatibility issues. You need to install an older version of Python first.
-
Go to the Python official website and find the download for the Windows version.
Scroll down to find a version of Python 3.8, click to download, and make sure to select the installer version.
-
After downloading, double-click to install and make sure to check Add Python 3.8 to PATH; otherwise, using commands later will be troublesome.
-
We also need to install the C++ Redistributable, which you can find a link to on the TensorFlow official website and click directly. Scroll down to find
Latest Microsoft Visual C++ Redistributable
, and download the X64 version. -
Double-click to install.
CPU Version Installation
-
Installing the CPU version is relatively simple and can be completed with just two commands. Open Command Prompt (recommended to run as administrator) and enter the following two commands: the first command updates the pip package manager, and the second command installs TensorFlow.
1
2pip install --upgrade pip
pip install tensorflowIf your internet speed is slow, you can use Tsinghua University’s mirror source (recommended! TensorFlow packages are large, and downloading from the official foreign source is very slow and prone to failure).
1
2pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install tensorflow -i https://pypi.tuna.tsinghua.edu.cn/simple -
Enter the Python console and try applying it. If there are no errors, the installation is successful.
1
2import tensorflow as tf
tf.__version__
GPU Version Installation
-
First, ensure your computer has an NVIDIA GPU and that the driver is installed correctly (as long as the NVIDIA Control Panel appears).
-
Confirm that your GPU supports CUDA and cuDNN.
-
Go to the CUDA official website and download the CUDA driver. Based on the table above, download the corresponding version. I installed tensorflow-2.6.0, so I downloaded CUDA11.2.
-
After downloading, double-click to install. The software will first extract, then proceed to the installation program. (Since the software has been updated, the steps in the later version may differ from the earlier version.)
-
In the installation options, select
Custom
, checkCUDA
, and uncheckVisual Studio Integration
. -
In
Other components
andDriver components
, compare the components between the new version and the current version. If the current version is higher than the new version, uncheck it. -
You can change the installation path to another drive. Make sure to remember this path, as you will need it later.
-
Wait for the installation to complete, and it’s done.
-
Go to the cuDNN official website and download the corresponding version of cuDNN.
-
After downloading, it’s a compressed package. Extract it and copy all the files to the folder where you just installed it.
-
Open
System Environment Variables
. -
Add four paths in the system variables, as shown in the image below
-
In Command Prompt, enter the following (command V is uppercase), and it will display the version number if the installation is successful.
1
nvcc -V
-
Open Command Prompt (recommended to run as administrator) and enter the following two commands: the first command updates the pip package manager, and the second command installs TensorFlow, specifying the version because TensorFlow from 2.11.0 onwards can only call CUDA in WSL, so we install version 2.10.0.
1
2pip install --upgrade pip
pip install tensorflow==2.10.0If your internet speed is slow, you can use Tsinghua University’s mirror source (recommended! TensorFlow packages are large, and downloading from the official foreign source is very slow and prone to failure).
1
2pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install tensorflow==2.10.0 -i https://pypi.tuna.tsinghua.edu.cn/simple -
Enter the Python console and try applying it. If it returns
true
, the installation is successful.1
2import tensorflow as tf
tf.test.is_built_with_cuda()