Tensorflow-gpu nanny level installation tutorial (Win11, Anaconda3, Python3.9)
-
- foreword
- Preparations for Tensorflow-gpu version installation
- (1) Check the graphics card of the computer:
- (2) Anaconda installation
- (3), cuda download and installation
- (4), cudnn download and install
- (5), configure environment variables
- (6) Create tensorflow environment
- (7) Test whether Tensorflow-gpu is successfully installed
- Uninstall and reinstall
foreword
CPU
GPU
The difference between version and version is mainly in the running speed , GPU
the version running speedfaster, so if the computer graphics card supports it cuda
, it is recommended to install gpu
the version.
-
For the CPU version , no additional preparation is required,
CPU
and the version can be installed on general computers, without additional preparation for the content of the graphics card, (如果安装CPU版本请参考网上其他教程!
) -
The GPU version needs to be downloaded in advance
cuda
andcuDNN
. (本文为GPU版本安装教程。
)
Preparations for Tensorflow-gpu version installation
Say the important thing three times:
before installation mustTo check the environment configuration of your computer , and then queryTensorflow-gpu
, Python
, cuda
, cuDNN
version relationship, you need toone-to-one correspondence!
before installation mustTo check the environment configuration of your computer , and then queryTensorflow-gpu
, Python
, cuda
, cuDNN
version relationship, you need toone-to-one correspondence!
before installation mustTo check the environment configuration of your computer , and then queryTensorflow-gpu
, Python
, cuda
, cuDNN
version relationship, you need toone-to-one correspondence!
Tensorflow-gpu and Python, cuda, cuDNN version relationship query
My installation environment is:
operating system | graphics card | Python |
---|---|---|
win11 | NVIDIA GeForce RTX2050 | 3.9.13 |
My tensorflow-gpu
installed version is:
tensorflow-gpu | cuda | cuDNN |
---|---|---|
tensorflow-gpu 2.7.0 | cuda 11.5.2 | cuDNN 8.3.2 |
Note: My corresponding relationship is to check other people's successful installation cases on the Internet. Don't combine them at will , otherwise the installation will easily fail, or you can install them according to the combination searched on the official website. The installation process is the same!
(1) Check the graphics card of the computer:
1), Right-click 此电脑
→ Right-click to select 管理
→ 设备管理器
→ 显示适配器
Mainly look at the independent display:GeForce RTX 2050
You can see that the click appears NVIDIA GeForce ...
, that is, the model of your computer graphics card .
- If it appears, it means that
Tensorflow-gpu
the version can be used, if not, you can only install the CPU version honestly. - Then you can go to NIVIDIA official website to check the computing power of your computer graphics card: https://developer.nvidia.com/cuda-gpus , it is recommended to install computing power >=3.5 .
I didn't find out the model number. The computing power of NVIDIA graphics cards in recent years is generally enough.
2), Right click 显卡
→ 属性
→ 驱动程序
, you can view the driver of the graphics card:
3) Check the GPU driver version, which is our "CUDA Version". In Windows 11 version, it is generally version 12.0. Press + on the keyboard at the same time, enter, open the command win
window r
, cmd
and enter in the command window:
nvidia-smi
(2) Anaconda installation
The installation tensorflow
is pre-installed Anaconda
. I will not focus on the introduction here. I have also written related articles in detail before:
Anaconda installation - super detailed version (2023)
Anaconda
After the installation is successful, enter tensorflow
the installation below!
The subsequent tensorflow
installation can be divided into three steps:
cuda
installationcuDNN
The neural network acceleration library installation- Configure environment variables
(3), cuda download and installation
download cuda
and cuDNN
. Download the corresponding cuda
version from the official website cuDNN
. The version can be lower than the version found above CUDA
but not higher than the version supported by the computer.
- cuda download address: CUDA Toolkit Archive | NVIDIA Developer ;
- cudnn download address: cuDNN Archive | NVIDIA Developer .
1), download:
What I downloaded is CUDA Toolkit 11.5.2
, click on the previousCUDA Toolkit 11.5.2
Select the corresponding system, version and other options, click Download to download:
2), install
a. Double-click the installation package, and a prompt box will appear, allowing you to choose a temporary decompression location (the contents of this location will be saved after you install cudaautomatically delete), here is the default , click ok.
b. Click Agree and continue:
c. After completing the previous step, select Custom, and then click Next:
d. Complete the previous step. CUDA must be checked here . The following options can be unchecked, and it will have no effect on the follow-up.
- In the component CUDA column, uncheck Visual Studio Integration (because we are not using the Visual Studio environment, even if it is checked, the installation will fail)
- Compare the new version of Display Driver with the current version information in the Driver components column.
- If the current version is higher than the new version, uncheck Display Driver;
- If the current version is lower than the new version, just keep the default installation information, otherwise the computer will freeze or freeze, or even blue screen. ! ! !
e. The installation location can be changed by yourself. Take a screenshot to record where you installed it,to be used later! I chose the default install location.
f. Installing
g. The installation is successful!
Click to close!
Check environment variables :
After the installation is complete, check whether the environment variables exist. Generally, the environment variables will be automatically configured after the installation is completed. If not, you need to configure them manually. The specific process is as follows.
-
Open Computer Properties , find Advanced System Settings , select Environment Variables to open.
-
Check if there are the following system variables , if not, you need to add them yourself, corresponding to the name and value on the picture, configure
CUDA
the location where your computer is installed.
- Open the Path of the system variable to see if there are the following two items. If not, you need to add it yourself. Be sure to configure the location of the installation.
After configuring the environment variables, let's check whether CUDA is installed successfully.
- Open cmd and enter the following command to check whether CUDA is installed successfully (choose one of the two options).
If the following information cannot be displayed, it means that the installation failed.
nvcc -V
nvcc --version
- You can also look at the environment variables set by CUDA.
set cuda
- We can also search the installation directory of CUDA to find the "nvcc.exe" file.
The installation of CUDA is over, and then download and decompress the cuDNN file.
(4), cudnn download and install
CUDA
It is not an implemented GPU
neural network acceleration library. If we want to accelerate the neural network, we also need to install the cuDNN
neural network acceleration library.
-
cuDNN
It's not an application program, but several file packages. After downloading, copy itCUDA
to the directory of .
Download address: cuDNN Archive | NVIDIA Developer . -
When you click download for the first time, you will be asked to register and log in first, and then download. It is no problem to fill in the content carefully during the registration process. Skip it here, and then enter the download link.
1), download:
Download the corresponding version cuDNN
. Here is the choice cuDNN v8.3.2 for CUDA 11.5
.
- , After downloading and decompressing the installation package, we can see four files after decompression:
3), this step of the tutorial shouldextra attention!
- To copy
cudnn
all the files under the corresponding folder in the file to the corresponding installation directory , - andnoCopy
cudnn
the folder in the file to the past.eg: What is copied is not the foldercudnn
in the folder, but all the files under the folder . (It is normal to have duplicate files, just overwrite them!)bin
bin
- Open the folder
cudnn
in the filebin
, and all thedocument Copy and paste intoCUDA\v11.5\bin
the folder:
- Open the folder
cudnn
in the fileinclude
, and all thedocument Copy and paste intoCUDA\v11.5\include
the folder:
- Open the folder
cudnn
in the filelib
, and all thedocument Copy and paste intoCUDA\v11.5\lib\x64
the folder:
- Open
cudnn
the file inremaining files, copy and paste intoCUDA\v11.5
the folder:
cuDNN
In fact, it is CUDA
just a patch, optimized for deep learning operations, and then we add environment variables! keep going down.
(5), configure environment variables
- , Open the system variable
Path
, add the following path under the path of the system variablepath
: (specifically, adjust according to your own installation path)
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.5\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.5\libnvvp
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.5
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.5\lib\x64
After adding it looks like this:
2), After configuring the environment, we need to verify whether the environment variables are configured successfully:
Open cmd
, enter your own CUDA installation path ...\CUDA\v11.5\extras\demo_suite
:, I am the default path , so mine is:
cd \Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.5\extras\demo_suite
Then execute the following two commands respectively:
.\bandwidthTest.exe
.\deviceQuery.exe
If Result
all are PASS
, the configuration is successful!
3) After everything is installed, we can continue to enter the information to nvidia-smi
be viewed CUDA
, and then implement the installation and use of other libraries (environments) according to the information of the installed version!
nvidia-smi
As shown in the figure, you can see that the driver version is 527.41
; the highest supported CUDA
version is 12.0
version.
(6) Create tensorflow environment
I use it here Anaconda
(if you choose this step, you don’t need additional downloads python
, and various common toolkits, it will be packaged and downloaded)
1), openanaconda prompt
2) To create tensorflow
an environment, enter the command: conda create -n tensorflow python=3.9
, which means to create an environment named tensorflow
, the version used in this environment python
is 3.9
the version, if it is created by default, it will be in C盘
!
conda create -n tensorflow python=3.9
3) After the creation is successful, enter the command: conda env list
, you can see that tensorflow
the environment has been created, and the asterisk is the current environment (basic environment base
).
conda env list
4), enter the environment, enter the command: activate tensorflow
, you can enter tensorflow
the environment
conda activate tensorflow
To exit the environment, enter:
conda deactivate
5) Because my conda
environment is in the D drive, I changed the path as follows. If anaconda
it is the default path during installation, this step is not required.
d:
cd \WorkSoftware\Install\Anaconda3\envs\tensorflow\
6), install the specified version tensorflow-gpu
, what I installed is 2.7.0
, according toyour own companion versionTo install, enter the command:
pip install tensorflow-gpu==2.7.0 -i https://pypi.mirrors.ustc.edu.cn/simple
It should be installed when no error is reported.
7) Open python
the environment, import tensorflow
the package for testing, view tensorflow
the version information, and enter the command:
import tensorflow as tf
- If the import package has the following error (If there is no error, please ignore it!):
(tensorflow) C:\Users\Rmzh>python
Python 3.9.16 | packaged by conda-forge | (main, Feb 1 2023, 21:28:38) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\WorkSoftware\Install\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
from tensorflow.python.tools import module_util as _module_util
File "D:\WorkSoftware\Install\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\__init__.py", line 41, in <module>
from tensorflow.python.eager import context
File "D:\WorkSoftware\Install\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\eager\context.py", line 33, in <module>
from tensorflow.core.framework import function_pb2
File "D:\WorkSoftware\Install\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\core\framework\function_pb2.py", line 16, in <module>
from tensorflow.core.framework import attr_value_pb2 as tensorflow_dot_core_dot_framework_dot_attr__value__pb2
File "D:\WorkSoftware\Install\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\core\framework\attr_value_pb2.py", line 16, in <module>
from tensorflow.core.framework import tensor_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__pb2
File "D:\WorkSoftware\Install\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\core\framework\tensor_pb2.py", line 16, in <module>
from tensorflow.core.framework import resource_handle_pb2 as tensorflow_dot_core_dot_framework_dot_resource__handle__pb2
File "D:\WorkSoftware\Install\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\core\framework\resource_handle_pb2.py", line 16, in <module>
from tensorflow.core.framework import tensor_shape_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__shape__pb2
File "D:\WorkSoftware\Install\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow\core\framework\tensor_shape_pb2.py", line 36, in <module>
_descriptor.FieldDescriptor(
File "D:\WorkSoftware\Install\Anaconda3\envs\tensorflow\lib\site-packages\google\protobuf\descriptor.py", line 561, in __new__
_message.Message._CheckCalledFromGeneratedFile()
TypeError: Descriptors cannot not be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.If you cannot immediately regenerate your protos, some other possible workarounds are:
1. Downgrade the protobuf package to 3.20.x or lower.
2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).
More information: https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates
- Solve the above error, enter
exit()
the exitpython
environment to import the following packages:
pip install protobuf==3.19.0 -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
Then re-enter python
the environment, view tensorflow
the version information, and enter the command:
import tensorflow as tf
tf.__version__
Exit tensorflow
the environment:
conda deactivate
At the same time, conda
the console is opened by default base
, if you want to manage this setting
conda config --set auto_activate_base false / true
(7) Test whether Tensorflow-gpu is successfully installed
- Open
Anaconda
, selecttensorflow
the environment, openspyder
, the first time you open it, you need to install itSpyder
, just click belowinstall
.
- Enter the following test code:
import tensorflow as tf
print(tf.__version__)
print(tf.test.gpu_device_name())
print(tf.config.experimental.set_visible_devices)
print('GPU:', tf.config.list_physical_devices('GPU'))
print('CPU:', tf.config.list_physical_devices(device_type='CPU'))
print(tf.config.list_physical_devices('GPU'))
print(tf.test.is_gpu_available())
# 输出可用的GPU数量
print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
# 查询GPU设备
- The version of the current environment appears
tensorflow
along with some other information, my version is2.7.0
, - If the following appears
True
, it means that ourtensorflow-gpu
has been successfully installed and can be used normally!
- Let's test
GPU
the computing speed!
import tensorflow as tf
import timeit
#指定在cpu上运行
def cpu_run():
with tf.device('/cpu:0'):
cpu_a = tf.random.normal([10000, 1000])
cpu_b = tf.random.normal([1000, 2000])
c = tf.matmul(cpu_a, cpu_b)
return c
#指定在gpu上运行
def gpu_run():
with tf.device('/gpu:0'):
gpu_a = tf.random.normal([10000, 1000])
gpu_b = tf.random.normal([1000, 2000])
c = tf.matmul(gpu_a, gpu_b)
return c
cpu_time = timeit.timeit(cpu_run, number=10)
gpu_time = timeit.timeit(gpu_run, number=10)
print("cpu:", cpu_time, " gpu:", gpu_time)
- It can be seen
gpu
that the speed ratiocpu
is still much faster! - For the training of neural network models in machine learning, it can greatly speed up our training process and save us a lot of time, which is still very good!
Uninstall and reinstall
If there is an error in the installation, you can uninstall and reinstall:
tensorflow-gpu uninstall
Note: The personal installation process is only for learning reference, if there are any deficiencies, please correct me!