RTX 3060 quickly configures GPU version tensorflow

Table of contents

1. Anaconda installation

1. Anaconda Download

Method 1: Download from the official website

Method 2: Tsinghua Mirror Download

2. Anaconda installation

3. Anaconda configuration

3.1 Configure environment variables

3.2 Configure domestic mirror sources

3.3 Restore the default source:

3.4 Conda common commands

2. Install Tensorflow-gpu

1. Use Anaconda to create the environment

2. Install CUDA

3. Install cuDNN

4. Install the GPU version of tensorflow

 3. Import the configured environment into pycharm

1. Create a project

 2. New test code (GPU version)

3. Run successfully

Four. The second way of CUDA installation


1. Anaconda installation

1. Anaconda Download


Method 1: Download from the official website


Download address portal:

Official website home page: https://www.anaconda.com/
Official website download page: https://www.anaconda.com/products/individual#Downloads
Just select the corresponding installer directly, the speed is not slow

Method 2: Tsinghua Mirror Download


If the download from the official website is slow or easy to break, it is recommended to use the following Tsinghua mirror method:

Download address: https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
After opening, you can sort by Date to find the latest version to download

è¿éæå¥å¾çæè¿°
The speed of course depends on different network environments.

2. Anaconda installation


After downloading, it is the same as normal software installation,

Here is an example with the Windows version:


Select the user All Users, and the installation address is the default


In the next step, you don’t need to choose Add sys path, just check Register. It is recommended to manually configure SysPath after installation to avoid problems caused by configuration in later use


wait for completion, next step


The last two items do not need to be selected, click Finish to complete the installation

3. Anaconda configuration


3.1 Configure environment variables


Open the computer advanced system configuration:


Click on System Environment Variables


Find the following system variable -> Path, then click Edit or double-click directly

Add the installation directory and the corresponding bin directory, as shown in the figure below, to complete the configuration

Note that because I installed anaconda in the default location, it is installed in an invisible file on the C drive, so we need to click on an anaconda application to select the open file location and find the file directory of anaconda3

Under normal circumstances, the ProgramData here is not visible on the C drive of this computer. 

è¿éæå¥å¾çæè¿°
After that, you can simply enter the conda command through cmd to check whether the installation configuration is successful:
input , you can see the version number of Anaconda , which means that Anaconda is installed successfully !conda -V或者conda --version

At the same time, you can use python --version to see that the version of python is already the version brought by conda:
 

3.2 Configure domestic mirror sources

Method 1 : Enter in cmd and conda config --set show_channel_urls yesa .condarc file will be generated in the user directory of drive C.

conda config --set show_channel_urls yes


Then open it with Notepad , replace it with the following content and save it.

channels:
  - defaults
show_channel_urls: true
channel_alias: https://mirror.tuna.tsinghua.edu.cn/anaconda
default_channels:
  - https://mirror.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirror.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - https://mirror.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirror.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
  - https://mirror.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirror.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirror.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirror.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirror.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirror.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirror.tuna.tsinghua.edu.cn/anaconda/cloud

Finally save, and then enter conda clean -i in CMD , at this time Anaconda has changed the source successfully!

Then you can also change the source of Python, which can significantly speed up the download speed. Enter the following code in CMD to change the source successfully!

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

Method 2: In cmd, configure Tsinghua source for conda by entering the following command:


conda config --add channels    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/

.condarc is the configuration file of anaconda, under C:\users\username\, it contains the source. The default is not to create automatically, only when the user uses the conda config command for the first time, the system will automatically create the .condarc file

Show source :

conda config --show-sources


3.3 Restore the default source :

conda config --remove-key channels

3.4 Conda common commands

2. Install Tensorflow-gpu

Before installing Tensorflow-gpu, we need to install cuda and cudnn etc. The most important thing about the gpu version of Tensorflow is the recipe (the combination of CUDA, cuDNN, and Tensorflow versions).
The version configuration of the GPU version is given below:

GPU:

Through the above chart, you can confirm the collocation of each version you need! Remember to install according to the chart above!
After determining the version you need, the official installation begins! (I take version 2.3.0 as an example)


1. Use Anaconda to create the environment


Here the environment name is named tensorflow, enter it in CMD

conda create -n tf38 python=3.8

Enter y to confirm

When you see done, an activation environment prompt appears, indicating that the virtual environment has been created successfully!

2. Install CUDA

First check the cuda version corresponding to the local GPU , as shown in the figure below, the local cuda version is 11.5, and the cuda version selected later should not exceed the version here.


In our specific use, in fact, what is really needed is not the whole CUDA , but cudatoolkit , so we install cudatoolkit directly here, without downloading more than 3 G CUDA for local installation.

 

In many tutorials, I saw that many of them went to NVIDIA's official website to download the installation files of CUDA and cuDNN and then installed them. However, this is unnecessary and troublesome. We can directly install the corresponding versions by entering commands in conda.


First enter activate tf38 in CMD to activate the environment. The name of the virtual environment will be displayed on the left side of the directory.

Then enter conda install cudatoolkit=10.1, see the figure below, that is, the installation of cudatoolkit (CUDA) is complete (this version number is just an example, and it can be changed according to actual needs)

3. Install cuDNN


In the active environment type

conda install cudnn=7.6

See the situation in the figure below, indicating that the installation has been successful!

4. Install the GPU version of tensorflow


The way to install Tensorflow, CUDA, and cuDNN is slightly different. CUDA and cuDNN are downloaded using the conda command, and Tensorflow is downloaded through pip , which is also entered in the activation environment

pip install tensorflow-gpu==2.3.0

Wait for the download to complete, that is, the installation is successful, as shown in the figure below, it means that the installation is successful!

So far, the GPU version of Tensorflow has been successfully installed, and now it is necessary to test whether it is available or not.
Enter python in the activate environment to enter the python environment.
then type

import tensorflow as tf

It can be seen as follows:

 re-enter

tf.test.is_gpu_available()

If it is True at the end, it means that the GPU is available, which proves that the GPU version of Tensorflow has been successfully installed, as shown in the figure below:

 3. Import the configured environment into pycharm

1. Create a project

 Or test the existing project file, select Add Interpreter in the lower left corner

 2. New test code (GPU version)

import tensorflow as tf
 
tensorflow_version = tf.__version__
gpu_avilable = tf.test.is_gpu_available()
 
print("tensorflow version: ", tensorflow_version, "\tGPU aviable:", gpu_avilable)
 
a = tf.constant([1.0, 2.0], name='a')
b = tf.constant([1.0, 2.0], name='b')
result = tf.add(a, b, name='add')
print(result)

3. Run successfully

Four. The second way of CUDA installation

To be honest, this method is really troublesome and unnecessary.

But still add it, learn from others.

1. After checking cuda, go to https://developer.nvidia.com/cuda-toolkit-archive and select the corresponding cudatoolkit version to download (tensorflow-gpu uses cudatoolkit, there is no need to download the entire cuda), As shown in the figure, the version you choose to download for this machine is 10.1. You must pay attention to the version you choose here, because the cuda version will determine the version of cudnn and tensorflow-gpu you download. If they are inconsistent, there will be problems such as version mismatch and cannot be used. The details are described below.

 2. After downloading cuda
      ① double-click the installation package, a prompt box will appear, allowing you to choose a temporary decompression location (the content of this location will be automatically deleted after you install cuda), here is the default, click ok.

è¿éæå¥å¾çæè¿° 

      ② After completing the previous step, select Custom, and then click Next.
      ③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.

è¿éæå¥å¾çæè¿° 

      ④ Then go to the next step and wait for the end of the installation (when you choose the installation location in the middle, you can change the directory, it is best to only change the drive letter in the front, and keep the folders in the back consistent, which is convenient for future management).
      ⑤ 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.
      a. Open computer properties, find advanced system settings, and select environment variables to open.

      b. Check whether there are the following environment variables. If not, you need to add them yourself. Corresponding to the name and value on the picture, configure the location where your computer CUDA is installed.

è¿éæå¥å¾çæè¿°

      c. Open the Path of the system variable and check whether there are the following two items. If not, you need to add it yourself. Be sure to configure the installation location.

è¿éæå¥å¾çæè¿°

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

Step 2: Install cuDNN
      Open this website https://developer.nvidia.com/rdp/cudnn-archive , when you click download for the first time, you will be asked to register and log in first, and then download, fill in the content carefully during the registration process No problem, skip it here, and then enter the download link.
      ① First, download the corresponding cuDNN version according to the installed CUDA version, and check the corresponding tensoflow-gpu version and python version from here. For detailed information, see the website https://tensorflow.google.cn/install/source_windows .

      Because of the CUDA10.1 version installed on this machine, choose cuDNN7.6 to download, and you can choose all the ones in the red box below.

      ② After the download is complete, unzip the file, and move the corresponding file in the cudnn file to the cuda pair installation directory, and be careful not to move it to the wrong location.

The decompressed cudnn file The installed cuda directory

      The installation of cuDNN is over here, and the next step is tensorflow-gpu.
 

Guess you like

Origin blog.csdn.net/Sciws/article/details/125249099