Deep learning environment configuration series articles (2): Anaconda configures Python and PyTorch

Deep learning environment configuration series article directory

Chapter 1 Introduction to Professional Names and Configuration Schemes
Chapter 2 Configuring Python and PyTorch in Anaconda
Chapter 3 Configuring the Python Environment of VS Code and Jupyter
Chapter 4 Configuring Windows 11 and Linux Dual Systems
Chapter 5 Configuring Docker Deep Learning Development Environment



foreword

Anaconda has powerful package management and environment management functions. After use, you can easily use and switch scientific computing libraries such as Python and PyTorch in different versions. This article will introduce its download, installation and use methods. Chapter 2 and Chapter 3 respectively provide the ways to install Anaconda on Windows system and Linux system. Readers can choose one of them to install according to their own computer system.

1. Install Anaconda on Windows system

Official website download address: https://www.anaconda.com/
However, due to network speed reasons, we generally choose mirror websites to download and install. The address is as follows https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/

Then select the corresponding Anaconda version from the mirror website to download. If the download stops due to network problems during the download process, right-click to continue.

Here, I also provide my personal Baidu cloud download link as follows:
Window system version Anaconda download link: https://pan.baidu.com/s/1E84x36YcoyXGdjKpsMU7kQ
Extraction code: 7799

After the download is complete, double-click the downloaded Anaconda3-2022.05-Windows-x86_64.exe file, and the following interface will appear, just click Next.

Figure 2-1 Installing Anaconda on Windows

The next two options shown in Figure 2-2 are recommended to only check the second one, which are automatically added to the environment variable and use Python3.9 by default.
Figure 2-2 Installing Anaconda on Windows

Click the "install" button and wait for the installation to complete.
After the installation is complete, open the Anaconda Prompt terminal as shown in Figure 2-3.

Figure 2-3 Installing Anaconda on Windows
Enter the command in the terminal.

1.conda --version  

If you can see the currently installed Anaconda version number, the installation is complete.

2. Install Anaconda on Linux system

First download Anaconda. Note that you need to download the Linux version, that is, the Anaconda installation file with the suffix .sh.
After downloading, put the downloaded "Anaconda3-2021.04-Linux-x86_64.sh" file in the /home/gustuy/software folder (the installation package can be placed anywhere).
Open the Linux terminal and use the following command to enter the folder where the Anaconda installation package is located.

1.cd /home/gustuy/software  

After entering, use the following command to run the installation file.

1.sh ./Anaconda3-2021.04-Linux-x86_64.sh  

After entering the command and executing it, you will be prompted to view the "license file", just press the "Enter" key. After pressing the "Enter" key, the software "license file" will appear, this file is very long, you can keep pressing the "Enter" key until you reach the end of the file.
After turning to the "End of the license file", a prompt "Do you accept the license terms" will appear, enter "yes" and press the "Enter" key.

In short, there will be some asking information before the official installation, just press the "Enter" key, if you need to enter yes/no, enter "yes" to complete the installation.
After the installation is complete, enter the command "conda info -e" to view the current Anaconda environment, and then you will find that the word (base) appears in front of the command line, which means that the installation has been successful, and the environment variables and default Python have been installed. If there is an error conda: command not found, it is because the environment variable is not configured successfully. The solution is as follows.

Enter the following command, use the vim editor to edit the configuration file bashrc (you need to make sure you have a vim editor first)

1.vim ~/.bashrc    

Then press the "i" key to enter "edit mode" and add it to the last line.

1.export PATH=$PATH:/root/anaconda3/bin   

Note: the above commands cannot be copied and pasted directly. The above "PATH" is because the Linux user name used by the author is root, and Anaconda is installed in the /root/anaconda3/bin directory, so you need to change to your own Anaconda installation directory, ie.

1.export PATH=$PATH:[你的Anaconda的安装目录]  

After adding the address command, press the "ESC" key to exit the "Edit Mode", then input ":wq" in sequence, and press the "Enter" key to save and exit.
Then, use the following command to refresh the environment variables.

1.source ~/.bashrc    

Finally, enter the command "conda info -e" to view the current Anaconda environment, which can be displayed successfully.

Three, Anaconda's quick start

Open the Anaconda Prompt terminal, and the following commands will be executed in the terminal.
create new environment

1.conda create -n pytorch_gpu python=3 # 创建一个名为pytorch_gpu的环境并指定Python版本为3(的最新版本)    

switch environment

1.conda activate env_name  #切换到env_name环境    

List all environments managed by Conda

1.conda env list   

List all packages in the current environment

1.conda list    

delete environment

1.conda remove -n env_name --all #删除名为env_name的环境  

Install third-party packages
The requests package can be installed with the following command.

1.conda install requests    
2.# 或者:    
3.pip install requests    

Uninstall third-party packages
You can use the following command to uninstall the requests package.

1.conda install requests    
2.# 或者:    
3.pip install requests 

Import and export environment
can be used if you want to export the package information of the current environment.

1.#将软件包信息存入environment.yaml文件中    
2.conda env export > environment.yaml 

You can see the imported packages under the folder where you run this command.
It can be used when you need to recreate the same virtual environment.

1.#用environment.yaml文件创建一个与文件描述相同的虚拟环境    
2.conda env create -f environment.yaml   

Fourth, Anaconda configures the PyTorch deep learning environment

This section explains in detail how Anaconda downloads, installs and runs the PyTorch framework. The Python version used by the author is 3.9, and the graphics card model of the computer is NVIDIA GeForce RTX 3070.
The methods of downloading PyTorch for Windows and Linux are similar, and will be explained together in the following content.

1. Anaconda creates a new environment
First use the Conda command to create and activate a new Python environment "pytorch_gpu". We will configure the PyTorch deep learning framework in this environment to prevent pollution of the base environment. Enter and execute the following two commands respectively.

1.conda create -n pytorch_gpu python=3.9 #创建了名为pytorch_gpu的环境并指定Python为3.9    
2.conda activate pytorch_gpu #激活pytorch_gpu   

After completion, we can download and configure PyTorch in the environment "pytorch_gpu".

2. Anaconda changes the default download source (optional).
Due to the difference between domestic and foreign networks, when downloading some foreign software, the download speed is often slow or even the download fails. Many people suggest changing the download source. However, it is recommended that you try to download without changing the source first. The author feels that the download speed is not slow, and PyTorch officially does not recommend changing the source for installation.
If you want to change the source download, the steps are as follows: first open Anaconda Prompt, you can enter the command "conda config --show channels" to view your Anaconda download source directory, when we do not add a download source, there is only a default item that comes with it, As shown in Figure 2-4.
Figure 2-4 Anaconda download source

The first line of statement represents the command to add the download source, and the second line represents the command to delete the download source, where "key" refers to the keyword, and "value" refers to the value corresponding to the keyword, which can be some link or file location.

1.conda config --add key value    
2.conda config --remove key value    

Use the above two statements to add or delete download sources, for example, we can input them in sequence.

1.# 添加清华镜像源    
2.conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/    
3.conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge     
4.conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/    
5.conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/   

The above example adds the Tsinghua mirror source, and there are several common mirror sources in China, such as Ali Mirror and Zhongke Dayuan.
You can display the installation channels with the command "conda config --show channels".
To restore default settings, use the following command.

1.conda config --remove-key channels    

3. Select the PyTorch download version
and enter the official PyTorch installation website (https://pytorch.org/get-started/locally/) to see the download options shown in Figure 2-5.
Figure 2-5 PyTorch download parameters

The main selected parameters are the second and fourth lines.
In the second line, select the corresponding PyTorch version according to your computer system.
In the fourth line, select the corresponding PyTorch version according to your computer graphics card configuration.
In the fourth line, if you choose CUDA or CPU version, you need to check whether your computer has an available GPU. The judgment method is as follows.
Click Task Manager > Performance, if you see Figure 2-6, it means that it has a GPU, and the upper right corner is the graphics card model.
Figure 2-6 Computer GPU query

Note that only NVIDIA graphics cards support GPU acceleration, otherwise download the corresponding PyTorch CPU version.
For more detailed information, you can open the NVIDIA control panel in the computer, as shown in Figure 2-7, click "Help" → "System Information".
Figure 2-7 NVIDIA control panel

As shown in Figure 2-8, pay attention to the values ​​on the figure, and compare it with the CUDA comparison table on the NVIDIA website to find out which model of CUDA is suitable for your computer.
The query URL is as follows https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html
For example, the author's computer is 512.89, so you can choose CUDA11.6 or below.
Figure 2-8 CUDA comparison table

4. After downloading and installing PyTorch
and finding a compatible model, open the Anaconda Prompt terminal or Linux terminal (select the terminal according to your own computer system), and activate the environment to be installed (such as the "pytorch_gpu" environment created in 2.2.5.1, below This step needs to be executed when the terminal is executed), just enter the corresponding download command on the official website (note that the parameters of the download command are configured according to your computer).
The following is the official download command, taking Windows system, CUDA11.6 as an example, copy the corresponding official website code and press Enter, there will be an installation prompt, enter "y" and press the "Enter" key to install.

1.# NOTE: 'conda-forge' channel is required for cudatoolkit 11.6    
2.conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c pytorch -c conda-forge  

If the download fails (HTTP 000… error), it may be due to network fluctuations. In this case, it is recommended to repeat the above command several times, or to switch to another network, for example, using a mobile hotspot. If none of the above methods can solve the problem, you can try to replace the source described in Chapter 2.

Note that "NOTE" in the code indicates that the Conda official website does not recommend that you change the source to prevent installation errors. After the installation is successful, the terminal will display "done".

As shown in Figure 2-9, enter "Python" at the Anaconda prompt terminal to enter the Python environment (note, to enter the environment where PyTorch has just been installed, do not query in the base environment), then enter "import torch" and press "Enter" key, if no errors are reported, the installation is successful.

If the GPU version of PyTorch is installed, enter the command "torch.cuda.is_available()", and if it returns "True", the installation is successful.
Figure 2-9 PyTorch installation results

The method of configuring the PyTorch environment in the Linux system is similar to the method of configuring the PyTorch environment in the Window system. First, configure the parameters of the download command on the PyTorch official website, and then copy them to the terminal for execution. The biggest difference is whether the execution environment of the above commands is in the Anaconda Prompt terminal or the Linux terminal.

Guess you like

Origin blog.csdn.net/qq_39297053/article/details/130653122