当前位置:网站首页>Pytorch Foundation (I) -- anaconda and pytorch installation

Pytorch Foundation (I) -- anaconda and pytorch installation

2022-06-10 16:56:00 Long way to go 2021

        PyTorch Installation , It's common Anaconda/miniconda+Pytorch+ (Pycharm/VS Code) Tools for , Our installation is divided into the following steps

1 Anaconda Installation

         stay DL and ML in , A lot of mature package. Install one by one package so much trouble , And it is prone to strange problems . and Anaconda It solved our problem very well , It integrates common scientific analysis ( machine learning , Deep learning ) A large number of package. We have omitted to install some package The process of .

1.1 install Anaconda/miniconda

(1) land Anaconda | Individual Edition, Select the appropriate system DownLoad

Anaconda | Individual Edition

(2) You can go to Tsinghua mirror to install the corresponding system ,Windows according to 64 Bits or 32 Bit download corresponds to .exe,Linux install .sh,Mac install .pkg. It is generally recommended not to choose the latest version ( Less stable ), Here, it is suggested to choose the... In the previous two years anaconda3 Any version .

Index of /anaconda/archive/ | Tsinghua University open source software image station | Tsinghua Open Source MirrorIndex of /anaconda/archive/ | Tsinghua University open source software image station , We are committed to providing high-quality open source software images for domestic and school users 、Linux Image source service , Help users more easily access open source software . This mirror station is sponsored by Tsinghua University TUNA The association is responsible for operation and maintenance .

         Here we use Linux For example , Assume that the installation package has been downloaded , You can input commands directly at the terminal , First cd Go to the folder where you have this installation package ( The assumption is Documents)

cd Documents

         Follow up bash + Installation package name ( Folder path name is OK )

bash "/home/carpediem/Downloads/Anaconda3-5.3.1-Linux-x86_64.sh"

         For detailed installation steps, please refer to the installation tutorial of each system :

1.2 Verify successful installation

        Windows Find... On the start page Anaconda Prompt, Generally in Anaconda3 Under folder ,Linux Input... At the terminal anaconda You can verify ,

         No command found after input , Probably because the environment variables have not been configured yet . Enter the following command :

vim ~/.bashrc

         Add... On the last line after entering , among username Is your account name , Here according to your own installation Anacona To modify the path of :

export PATH=/home/username/anaconda3/bin:$PATH

         Update the configuration file after adding

source ~/.bashrc

         Re input anaconda, You can verify whether the installation is successful .

1.3 Creating a virtual environment

        Linux At terminal (Ctrl+Alt+T) Conduct ,Windows stay Anaconda Prompt Conduct

         View existing virtual environments

conv info --env
conda env list

1. Creating a virtual environment
         In deep learning and machine learning , We often create different versions of virtual environments to meet some of our needs . Let's introduce the commands for creating a virtual environment . In the choice Python version , Don't choose too high , Suggested choice 3.6-3.8, Too high a version will cause the related libraries to be inapplicable .

conda create -n env_name python==version

2. Delete virtual environment command

conda remove -n env_name --all

3. Activate the environment command

conda activate env_name

4. Exit the current environment

conda deactivate

1.4 Source change

         In the installation package when , We often use pip install package_name and conda install package_name The order of , But some package Download will be slow , So we need to change the source , Switch to domestic sources , Speed up our downloads . The following is the source switching of the two corresponding methods

1.4.1 pip Source change

1. Linux
        Linux Source change under , First, we need to create a new folder under the user directory .pip, And create a new file in the folder pip.conf, The specific command is as follows

cd ~
mkdir .pip/
vi pip.conf

         And then , We need to be in pip.conf Add the following :

[global]
index-url = http://pypi.douban.com/simple
[install]
use-mirrors =true
mirrors =http://pypi.douban.com/simple/
trusted-host =pypi.douban.com

2. Windows

  1. File manager file path address bar :%APPDATA% enter , Quick to enter C:\Users\ Computer users \AppData\Roaming In the folder ;
  2. newly build pip Folder and create a new one in the folder pip.ini The configuration file ;
  3. We need to be in pip.ini Profile contents , We can choose to use Notepad to open , Enter the following , And press ctrl+s preservation , Here we use watercress as an example .
[global]
index-url = http://pypi.douban.com/simple
[install]
use-mirrors =true
mirrors =http://pypi.douban.com/simple/
trusted-host =pypi.douban.com

Common image installation source websites
(1) Alibaba cloud http://mirrors.aliyun.com/pypi/simple/
(2) douban http://pypi.douban.com/simple/
(3) Chinese academy of sciences http://pypi.mirrors.opencas.cn/simple/
(4) Tsinghua University https://pypi.tuna.tsinghua.edu.cn/simple/
(5) University of science and technology of China http://pypi.mirrors.ustc.edu.cn/simple/
(6) Huazhong University of science and technology http://pypi.hustunique.com/
(7)Python official https://pypi.python.org/simple/
(8)v2ex http://pypi.v2ex.com/simple/

1.4.2 conda Source change

(1) View source

conda config --show-sources

(2) Switch source

conda config --add channels XXXX
conda config --set show_channel_urls yes

(3) Delete source

conda config --remove channels XXX

(4) Switch back to the source

conda config --remove-key channels

1. Windows System
        TUNA Provides Anaconda The image of the warehouse and the third-party source , Each system can modify the user directory .condarc file .Windows The user cannot directly create a file named .condarc The file of , You can do it first conda config --set show_channel_urls yes After generating the file, modify .

         When this is done , We need to change C:\Users\User_name\.condarc This file , Delete the original contents in the file after opening , Copy the following and save it .

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

         After this step is completed , We need to open it Anaconda Prompt function conda clean -i Clear index cache , Make sure you use the index provided by the mirror station .

2. Linux System
         stay Linux Under the system , Directly enter the following command at the terminal

conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/peterjc123/

We still need to revise it .condarc To carry out

cd ~
vi .condarc

         stay vim Next , We need to type i Enter edit mode , Paste the content above , Press ESC Exit edit mode , Input :wq Save and exit .

         We can go through conda config --show default_channels Check whether the source change is successful . meanwhile , We still need conda clean -i Clear index cache , Make sure you use the index provided by the mirror station . If the source does not take effect , Try to .condarc In the document - defaults Remove that line


2 Check for NVIDIA GPU

         If this part only has CPU Or the small partners of centralized display can skip this part .

  1. windows
             We can pass in cmd/terminal in Input nvidia-smi(Linux and Win command )、 Use NVIDIA Control panel and use task manager to check whether you have NVIDIA Independent graphics card and its model
  1. linux

         We need to look at the version number , See what you can be compatible with CUDA edition , Later installation PyTorch Is downward compatible . The specific adaptation table is shown in the figure below .


3 PyTorch Installation

Step 1: Sign in PyTorch Official website

Step 2:Install

         In this interface, we can choose to start locally (Start Locally), Development of cloud (Cloud Partners), Former Pytorch edition (Previous PyTorch Versions), Mobile development (Mobile), Here we need to install locally .

Step 3: Choose the command

         We need to choose the command according to our own situation and copy it , And then use conda Download or pip download ( Suggest conda install )

         open Terminal, Input conda activate env_name, Activate the environment and switch to the environment , We can do it PyTorch Installed.

notes

  1. Stable Represents a stable version ,Preview Represents the previous version

  2. It can be combined with whether the computer has a graphics card , choice CPU Version or CUDA edition , The suggestion still needs NVIDIA GPU

  3. We are officially advised to use Anaconda To manage

  4. System requirements for installation

    1. Windows
      1. Windows 7 And higher ; It is recommended to use Windows 10 Or even higher
      2. Windows Server 2008 r2 And higher
    2. Linux: With the common CentOS and Ubuntu For example
      1. CentOS, Minimum version 7.3-1611
      2. Ubuntu, Minimum version 13.04, This will lead to cuda The maximum version installed is different
    3. macOS
      1. macOS 10.10 And above
  5. Some computers support cuda edition <10.2, At this point, we need to manually downgrade , That is cudatoolkit = The version you fit , But here we need to pay attention to keep Pytorch and cudatoolkit Version adaptation for . see Previous PyTorch Versions | PyTorch

Step 4: Download Online

         If we use Anaconda Prompt For downloading , We need to get through conda activate env_name, Activate our virtual environment to , Reenter command .

         notes : We need to download the following instructions -c pytorch Remove to ensure the use of Tsinghua source download , Otherwise, it will be downloaded from the official website by default .

Step 5: Download offline

Windows

         During the installation , We may have some strange problems , This leads to unsuccessful online downloading , We can also use Download offline Method .

         Download address :https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

         Download the address above , We need to download the corresponding version pytorch and torchvision package , Then open the Anaconda Prompt/Terminal in , Enter the installation path .

cd package_location
conda activate env_name

         Next, enter the following command to install the two packages

conda install --offline pytorch The full name of the compressed package ( Suffixes should not be forgotten )
conda install --offline torchvision The full name of the compressed package ( Suffixes should not be forgotten )

Step 6: Verify successful installation

         Enter the A virtual environment , And then enter python, Enter the following code .

import torch

torch.cuda.is_available()

         This command means to verify that you can call cuda, If we Installation is CPU Version will return False, To be able to call GPU Will return True. Generally, if this command does not report an error, it will prove that the installation is successful .


Reference resources

原网站

版权声明
本文为[Long way to go 2021]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101556295717.html