1. Introduction
Deep learning has rapidly developed in recent years and is being applied in various industrial fields. The background of this advancement includes the popularity of Python and various deep learning libraries, especially PyTorch. PyTorch is loved by many researchers and developers due to its dynamic computation graph and simple usability. In this course, we will explain how to set up a virtual environment before installing PyTorch.
2. Necessity of a Virtual Environment
A virtual environment is a tool that helps manage projects independently. When different projects require different library versions, a virtual environment can solve these issues. In Python, tools like venv
or conda
can be used to create virtual environments.
3. Creating a Virtual Environment
3.1. Creating a Virtual Environment Using venv
Starting from Python 3.3, the venv
module is included. It allows for easy creation of virtual environments.
mkdir myproject
cd myproject
python -m venv myenv
source myenv/bin/activate # Unix or MacOS
myenv\Scripts\activate # Windows
You can create and activate a new virtual environment using the commands above. Now, you can install the necessary packages in this environment.
3.2. Creating a Virtual Environment Using conda
If you are using Anaconda, you can create a virtual environment using the conda
command.
conda create --name myenv python=3.8
conda activate myenv
Subsequently, you can install various packages along with Python in the virtual environment.
4. Installing PyTorch
When the virtual environment is activated, the method of installing PyTorch may vary depending on different options. The official PyTorch website provides installation commands suitable for your system. The common installation method is as follows.
4.1. Installing PyTorch Using pip
The simplest way to install PyTorch is to use pip
. You can install the CPU version with the command below.
pip install torch torchvision torchaudio
4.2. Installing PyTorch with CUDA Support
If you want to use a GPU, you need to install the version that supports CUDA. Below is an installation command based on CUDA 11.7.
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
4.3. Verifying Installation
After the installation is complete, you can run the following Python code to check if PyTorch has been installed correctly.
import torch
print(torch.__version__)
print('CUDA available:', torch.cuda.is_available())
5. Conclusion
In this course, we explained how to create a Python virtual environment and how to install PyTorch. Creating a virtual environment allows for independent management of various projects, and you can easily install PyTorch as needed, which will be very helpful in starting Python deep learning development. In the next course, we will cover the basic usage of PyTorch and model training, so please look forward to it.