Tensorflow-gpu nanny level installation tutorial (Win11, Anaconda3, Python3.9)

foreword

CPUGPUThe difference between version and version is mainly in the running speed , GPUthe version running speedfaster, so if the computer graphics card supports it cuda, it is recommended to install gputhe version.

  • For the CPU version , no additional preparation is required, CPUand 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 cudaand cuDNN. ( 本文为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 , cuDNNversion relationship, you need toone-to-one correspondence

before installation mustTo check the environment configuration of your computer , and then queryTensorflow-gpu , Python, cuda , cuDNNversion relationship, you need toone-to-one correspondence

before installation mustTo check the environment configuration of your computer , and then queryTensorflow-gpu , Python, cuda , cuDNNversion relationship, you need toone-to-one correspondence

Tensorflow-gpu and Python, cuda, cuDNN version relationship query

insert image description here

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

insert image description here

You can see that the click appears NVIDIA GeForce ..., that is, the model of your computer graphics card .

insert image description here

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:

insert image description here

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 winwindow r, cmdand enter in the command window:

nvidia-smi

insert image description here

(2) Anaconda installation

The installation tensorflowis 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)

AnacondaAfter the installation is successful, enter tensorflowthe installation below!

The subsequent tensorflowinstallation can be divided into three steps:

  1. cudainstallation
  2. cuDNNThe neural network acceleration library installation
  3. Configure environment variables

(3), cuda download and installation

download cudaand cuDNN. Download the corresponding cudaversion from the official website cuDNN. The version can be lower than the version found above CUDAbut not higher than the version supported by the computer.

1), download:

What I downloaded is CUDA Toolkit 11.5.2, click on the previousCUDA Toolkit 11.5.2
insert image description here

Select the corresponding system, version and other options, click Download to download:
insert image description here
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.

insert image description here

b. Click Agree and continue:

insert image description here

c. After completing the previous step, select Custom, and then click Next:

insert image description here

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)

insert image description here

  • 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. ! ! !

insert image description here

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.

insert image description here

f. Installing

insert image description here

g. The installation is successful!

insert image description here

Click to close!

insert image description here

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.

  1. Open Computer Properties , find Advanced System Settings , select Environment Variables to open.

  2. 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 CUDAthe location where your computer is installed.

insert image description here

  1. 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.

insert image description here

After configuring the environment variables, let's check whether CUDA is installed successfully.

  1. 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

insert image description here

  • You can also look at the environment variables set by CUDA.
set cuda

insert image description here

  • We can also search the installation directory of CUDA to find the "nvcc.exe" file.

insert image description here

The installation of CUDA is over, and then download and decompress the cuDNN file.

(4), cudnn download and install

CUDAIt is not an implemented GPUneural network acceleration library. If we want to accelerate the neural network, we also need to install the cuDNNneural network acceleration library.

  • cuDNNIt's not an application program, but several file packages. After downloading, copy it CUDAto 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.

insert image description here

  1. , After downloading and decompressing the installation package, we can see four files after decompression:
    insert image description here

3), this step of the tutorial shouldextra attention

  • To copycudnn all the files under the corresponding folder in the file to the corresponding installation directory ,
  • andnoCopy cudnnthe folder in the file to the past.eg: What is copied is not the folder cudnnin the folder, but all the files under the folder . (It is normal to have duplicate files, just overwrite them!)binbin

insert image description here

  1. Open the folder cudnnin the file bin, and all thedocument Copy and paste into CUDA\v11.5\binthe folder:

insert image description here

  1. Open the folder cudnnin the file include, and all thedocument Copy and paste into CUDA\v11.5\includethe folder:

insert image description here

  1. Open the folder cudnnin the file lib, and all thedocument Copy and paste into CUDA\v11.5\lib\x64the folder:

insert image description here

  1. Open cudnnthe file inremaining files, copy and paste into CUDA\v11.5the folder:

insert image description here
cuDNNIn fact, it is CUDAjust a patch, optimized for deep learning operations, and then we add environment variables! keep going down.

(5), configure environment variables

  1. , Open the system variable Path, add the following path under the path of the system variable path: (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:

insert image description here

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

insert image description here
insert image description here

If Resultall are PASS, the configuration is successful!

3) After everything is installed, we can continue to enter the information to nvidia-smibe viewed CUDA, and then implement the installation and use of other libraries (environments) according to the information of the installed version!

nvidia-smi

insert image description here

As shown in the figure, you can see that the driver version is 527.41; the highest supported CUDAversion is 12.0version.

(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

insert image description here

2) To create tensorflowan 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 pythonis 3.9the version, if it is created by default, it will be in C盘!

W11 download anaconda on the d drive, how to solve the new virtual environment is always on the c drive

conda create -n tensorflow python=3.9

insert image description here

3) After the creation is successful, enter the command: conda env list, you can see that tensorflowthe environment has been created, and the asterisk is the current environment (basic environment base).

conda env list

insert image description here

4), enter the environment, enter the command: activate tensorflow, you can enter tensorflowthe environment

conda activate tensorflow

insert image description here

To exit the environment, enter:

conda deactivate

5) Because my condaenvironment is in the D drive, I changed the path as follows. If anacondait is the default path during installation, this step is not required.

d:
cd \WorkSoftware\Install\Anaconda3\envs\tensorflow\

insert image description here

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  

insert image description here
It should be installed when no error is reported.

7) Open pythonthe environment, import tensorflowthe package for testing, view tensorflowthe 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 exit pythonenvironment 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 pythonthe environment, view tensorflowthe version information, and enter the command:

import tensorflow as tf
tf.__version__

insert image description here

Exit tensorflowthe environment:

conda deactivate

insert image description here
At the same time, condathe 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

  1. Open Anaconda, select tensorflowthe environment, open spyder, the first time you open it, you need to install it Spyder, just click below install.

insert image description here

  1. 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设备

insert image description here

  • The version of the current environment appears tensorflowalong with some other information, my version is 2.7.0,
  • If the following appears True, it means that our tensorflow-gpuhas been successfully installed and can be used normally!
  1. Let's test GPUthe 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)

insert image description here

  • It can be seen gputhat the speed ratio cpuis 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!

Guess you like

Origin blog.csdn.net/weixin_43412762/article/details/129824339