Pytorch installation and configuration

This article will explain in detail how to use Anaconda to install the Pytorch framework and configure its environment to Pycharm.

Anaconda installation

Anaconda is an open source Python distribution that contains many dependency packages for scientific computing. It not only makes it easy to download and manage dependency packages, but also manages distribution versions in a unified manner to avoid version conflicts between dependency packages.

1. First go to the Anaconda download webpage and click to download the Anaconda installation package. The official website also provides Mac and Linux versions, which can be downloaded according to the operating system of your computer.

2. After the download is complete, click on the installation package to install it. Follow the prompts to install step by step. Try not to install it on the C drive. It is important to remember the installation location so that you can configure the path in Pycharm later. can be found.


3. After the installation is complete, search and enter Anaconda Prompt in the navigation bar (you can find it directly in the start menu), find and open the Anaconda command window, as shown in the figure below. Base is the default installation environment of Anaconda. Generally, dependency packages are not installed in the base environment. Instead, the conda command is used to create a virtual environment.

Next create a virtual environment named “pytorch_3.8py”.

4. In the current command window, enter: conda create -n pytorch_3.8py python=3.8. This command creates a virtual environment named “pytorch_3.8py” and specifies the python version as 3.8. You can specify the virtual environment name and python version by yourself.

During the installation of the virtual environment, you need to enter y and press Enter to confirm. Then wait for the virtual environment to be downloaded and installed.

conda create -n pytorch_38py python=3.8

5. After installing the virtual environment, switch to the just-installed virtual environment and enter the command: conda activate pytorch_38py, thus switching to the pytorch_38py virtual environment. Here are some commonly used commands

# Change the virtual environment (you can add -n to specify the Python version)
conda activate pytorch_38py
# View the currently existing virtual environment
conda env list
#Exit the current virtual environment
conda deactivate
# Delete virtual environment
conda env remove -n pytorch_38py

Pytorch installation

PyTorch is a Python-based scientific computing framework focused on machine learning and deep learning tasks. It provides a wealth of tools and libraries, making it easier and more efficient to build neural network models, perform tensor calculations, and train models.

Here are some key features of PyTorch:

1. Dynamic calculation graph: PyTorch uses dynamic calculation graph, allowing you to define and modify the calculation graph, simplifying the debugging and development process. This is different from static calculation graph frameworks (such as TensorFlow), which require the entire calculation graph to be defined first and then executed.

2. GPU acceleration: PyTorch is optimized for GPU programming and uses CUDA to accelerate calculations, making the training and reasoning of deep learning models more efficient.

3. Rich toolset: PyTorch provides a rich set of tools and libraries, including automatic differentiation, various optimization algorithms, rich neural network layers and model structures, as well as data loading and processing tools, making it easier to build machine learning models.

4. Community support: PyTorch has huge community support, which means there are a large number of tutorials, documentation, examples, and open source projects for learning and reference.

5. Flexibility: PyTorch provides a high degree of flexibility, allowing users to develop deep learning models in the Python language. This flexibility gives users the freedom to explore, tweak, and experiment with different model structures and algorithms.

1. To install the GPU version, you must first know whether your computer configuration has a separate graphics card (not integrated graphics). If you do not install the CPU version, you can

2. Open the PyTorch official website page and scroll down until you see the location shown in the picture below. The default configuration is as shown in the figure. Conda is preferred for installation. Of course, pip can also be used for installation. Just choose a version of Compute Platform that is smaller than the CUDA version of your computer. If there is no GPU, select CPU for installation.

To check the CUDA driver version, press the “Win + R” keys, enter cmd and press Enter, open the command line window, and enter nvidia-smi. If the version is too old, you can update the graphics card driver yourself.

# Check GPU usage, driver version and CUDA version
nvidia-smi

3. Copy the command statement on the right side of Run this Command to the previous Anaconda prompt window, paste it and press Enter to run the download and installation. The download process may be slow, you can switch the mirror source (find a method by yourself). After the execution is completed, the following prompt appears to indicate that the installation is complete.

4. Installation completed Finally, to test whether pytorch can call cuda, enter python to enter the python running environment, and then enter import torch and torch.cuda.is_available() in sequence. If it is True, the test is successful. In this way we have completed the installation of Pytorch.

# Enter the python running environment
python
#Import torch package, not pytorch
import torch
# Whether cuda can be used
torch.cuda.is_available()

Pycharm configures Anaconda virtual environment

1. After the previous steps are successful, you can configure the virtual environment into Pycharm (you can configure the Chinese environment in pycharm first).

Open Pycharm, create or open a project, click Settings on the right -> Add interpreter -> Add local interpreter.

2. Select the Conda environment on the left side of the pop-up window. In the Conda executable file on the right, you need to select Scripts\conda.exe under the Anaconda installation path. My path is: D:\Anaconda3\Scripts\conda .exe.

After selecting, Pycharm will recognize Anaconda’s virtual environment. Just select the virtual environment in the drop-down box.

Summary

This article mainly explains the installation of pytorch and configuring the environment in pycharm. pytorch is divided into GPU version and CPU version

There are several prerequisites for using the PyTorch GPU version (This article does not explain it. If necessary, please look it up yourself (mainly because I am too lazy to write it)):

1. Have an NVIDIA graphics card: PyTorch GPU version requires an NVIDIA graphics card to take advantage of CUDA acceleration. Make sure you have an NVIDIA GPU device in your computer.

2. Install NVIDIA driver: In order to use the GPU, you need to install the latest NVIDIA graphics card driver and ensure that the driver version is compatible with the CUDA version required by PyTorch.

3. Install CUDA Toolkit: PyTorch uses CUDA to interact with NVIDIA GPUs. Installing the appropriate version of the CUDA Toolkit is a prerequisite for using PyTorch GPU Edition. It is recommended to choose a CUDA version that is compatible with PyTorch.

4. Install cuDNN: cuDNN is an NVIDIA deep learning library that provides GPU-accelerated deep neural network functionality. Make sure the installed cuDNN version is compatible with the selected PyTorch version.

On the PyTorch official website, you can find information about CUDA versions and cuDNN versions that are compatible with the required PyTorch version. At the same time, the PyTorch official website also provides corresponding installation guides and recommended driver and library versions.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeArtificial intelligenceDeep learning 387077 people are learning the system