Create a pytorch virtual environment using Anaconda3

1. Conda configures Pytorch environment

1.conda installs Pytorch environment 

Open Anaconda Prompt and enter the command line: 

conda create -n pytorch python=3.6

Enter y and press Enter.

Wait a moment and the Pytorch environment installation is completed. We can activate the pytorch environment using the following command.

conda activate pytorch

When the (pytorch) prefix appears in front, it means that you have entered the pytorch environment.

You can view the built environment by running the following command

conda env list

        Among them, base is the default environment of Anaconda, mypytorch is the environment built before, and my2pytorch is the pytorch environment built now.

We can use the following command in the activated pytorch environment to see which packages are included in the newly created environment.

conda list

You can see that the packages in the newly created environment do not contain pytorch related packages, and we need to install them manually.

2.conda install pytorch package

        Using the nvidia-smi command to find the native CUDA version, you can find the command to install the corresponding model of pytorch package here in PyTorch. If the computer does not have an independent graphics card, this command input is invalid.

nvidia-smi

Note: Some command line operations later require administrator status, so it is recommended to run the command line window as an administrator from the beginning.

         Here are the main instructions for installing the GPU version of the pytorch package. Because the pytorch package needs to match the CUDA version, you need to select the corresponding version of the pytorch package according to your CUDA version for installation. https://pytorch.org/

        In the activated pytorch environment, copy the command in the box to start downloading the installation package ( note: it must be entered in the pytorch environment!!! Otherwise it will be installed in Anaconda's default base environment!!!).

        Among them, the c in -c pytorch represents channel, which is the download channel. It is a foreign channel, so it is very likely that the installation will be very slow. Then we can choose a domestic mirror source to download (I have not used it). You can use the following commands to view information such as download channels and environment installation paths.

conda info

Use the above command to install the pytorch package, and enter the download command in the activated pytorch environment:

conda install pytorch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 cudatoolkit=10.2 -c pytorch

Note: It must be entered in the pytorch environment! ! ! Otherwise, it will be installed into Anaconda’s default base environment! !

After the download is completed, you can enter the conda list command as above in the pytorch environment to view the list of all installed packages.

conda list

3. Test

        To test the installation of the pytorch package in the pytorch environment, you can first enter the pytorch environment, enter the python command in the environment to enter the python interface, and then enter the following commands in sequence:

python
import torch
torch.cuda.is_available()

If no error is reported and the final output is True, it means that the GPU version of the pytorch package is successfully installed and the pytorch environment is set up.

2. Common commands for configuring the environment

 Taking the environment name pytorch, corresponding to python3.6 as an example, the following conda instructions in the command line are summarized as follows:

 1. Create an environment:

conda create -n pytorch python=3.6
conda create --name pytorch python=3.6

2. Delete the environment:

conda remove -n pytorch --all

 3. Activate the environment: (for the base environment, it can be directly abbreviated as activate)

activate pytorch

 4. Exit the environment:

deactivate

5. Download/uninstall package:

conda install package_name
conda remove package_name

 6. Query all packages in the environment:

conda list

 7. Query the built environment:

conda env list

 8. Query information such as related paths and download channels in the environment:

conda info

 9. Open the python interpreter:

python

3. PyTorch Quick Start Tutorial

PyTorch Deep Learning Quick Start Tutorial (Absolutely easy to understand!) [Little Mound] 

Guess you like

Origin blog.csdn.net/weixin_43200943/article/details/131937956