Installing PyTorch and Common Tools

Installing PyTorch and Common Tools

Posted by Byolio on November 30, 2024
🌐 中文 English

This article aims to introduce the installation of PyTorch and its commonly used tools. (Please enable VPN throughout to resolve network issues)

What is PyTorch

PyTorch is an open-source deep learning framework developed by Facebook’s AI Research Lab (FAIR). It is known for its dynamic computational graph, intuitive API, and powerful GPU acceleration capabilities. PyTorch is primarily used in fields such as computer vision and natural language processing, supporting researchers and developers in quickly building and training neural network models.

Why Choose PyTorch

  1. Dynamic Computational Graph: PyTorch uses a dynamic computational graph mechanism, allowing changes to the network structure at runtime, which facilitates debugging and development.
  2. Ease of Use: PyTorch’s API design is intuitive and aligns with Python coding style, lowering the learning curve.
  3. Strong Community Support: It has an active community and a large number of third-party libraries, making the development process more convenient.
  4. High Extensibility: Supports custom layers and operations, making development easier.

    Installing PyTorch (Using Windows as an Example)

  5. Install CUDA (GPU version of PyTorch requires CUDA) Open the terminal and enter:
    1
    
    nvidia-smi
    

    Check the driver version. The official CUDA documentation lists the minimum driver requirements for each toolkit version. For example, CUDA 12.7 typically requires driver version 525.85.12 or higher. cudaVision Download the CUDA driver from this link (I have already selected the options, just download). CUDA Restart your computer after installation. After restarting, open the terminal and enter:

    1
    
    nvcc -V
    

    If CUDA-related information is displayed, the installation was successful.

  6. Install PyTorch Open the PyTorch official download page pytorchDownload Select the corresponding command, copy it to the terminal, and download (it is not recommended to install in the base environment).

    Installing Common PyTorch Tools

    I use conda for installation. How to install conda.

  7. jupyter (A brief introduction; advanced jupyter content will not be covered here) Conda comes with jupyter. I will mainly explain how to create a virtual environment with PyTorch installed and import this virtual environment into jupyter.
    Use the following code (note to use your own Python version; copy python -V into the terminal to check your Python version):
    1
    2
    
    conda create -n pytorch python=3.10
    conda activate pytorch
    

    Then, install PyTorch in this environment using the method described earlier. After installation, restart the terminal. Create the corresponding IPython kernel:

    1
    
    python -m ipykernel install --user --name=pytorch --display-name "Pytorch_env"
    

    Start jupyter:

    1
    
    jupyter notebook
    

    You will now see the newly added jupyter kernel.

  8. Tensorboard (Optional) Switch to the environment with PyTorch and install:
    1
    
    conda install tensorboard
    

    Start Tensorboard:

    1
    
    tensorboard --logdir=logs
    

    logs is the folder where logs are saved; please replace it accordingly.

  9. Pycharm A basic IDE tool; no detailed explanation will be provided. Simply download and install. Advanced content will be covered later.
    Ensure the interpreter is set to the environment with PyTorch. You can test by importing torch.

FAQ

  1. If conda commands cannot be used Check that the environment variables are correctly configured, then enter in the terminal:
    1
    
    conda init
    
  2. How to test if GPU can be used
    1
    2
    
    import torch
    print(torch.cuda.is_available())
    

    If it returns True, the GPU can be used.

  3. How to test GPU compute capability
    1
    2
    
    import torch
    print(torch.cuda.get_device_capability())
    

Summary

The above covers the installation methods for PyTorch. I hope this is helpful to everyone.