01 Py Torch Internet Celebrities Top Star

01 PyTorch Internet Celebrities Top Star #

Hello, I’m Fang Yuan.

Starting from this lesson, we officially begin the study of PyTorch Basics.

In the Basics section, we will guide you to understand the development trend of PyTorch and the installation method of the framework, and then focus on explaining the common knowledge points of NumPy and Tensors.

Mastering these basic knowledge and skills will make you more efficient when using the PyTorch framework, and it is also the first step in learning machine learning and deep learning from scratch. Sharpening the knife will not delay the chopping of firewood, so through this module, our goal is to prepare for learning.

In today’s lesson, we will start with the installation of PyTorch and common programming tools, allowing you to have a good understanding of the language, tools, and technologies used in PyTorch, so as to better embark on the learning journey that follows.

Introduction of PyTorch #

I have already mentioned why we chose the PyTorch framework in the preface. Since 2019, PyTorch has dominated both the academic and engineering fields, and it can be said that PyTorch is the mainstream framework at present.

We are no stranger to “Py” here, it stands for Python. But what is “Torch”? Literally translated, it means a “torch”.

Image

So what is a torch? In fact, it is similar to the “Tensor” in TensorFlow. We can see it as a matrix that can be computed on the GPU.

So how do we use the PyTorch framework specifically? Simply put, it is a computational tool. With it, we can use the computer to complete complex computational processes.

However, we all know that machines and humans do not have a common “language”. In order for the machine to complete complex calculations for us, we need to first translate the data into something the machine can understand. Whether it is image data, text data, or numeric data, it needs to be converted into a matrix for subsequent transformations and computations.

Once we have handled the data input step, we rely on PyTorch to handle various complex computational tasks afterwards. These computational tasks include forward propagation, backward propagation, and even other very complex calculations, all of which are implemented by the PyTorch framework.

PyTorch will pass the matrices we need to compute into the GPU (or CPU), and perform various computations that we need in the GPU (or CPU). Because GPU is faster for matrix operations, it is generally preferred to use GPU for computations in neural networks. However, for learning purposes, CPU is sufficient.

What we need to do is design the entire task flow and network architecture so that PyTorch can smoothly complete the subsequent computational processes and help us to compute accurately.

Installing PyTorch and its Environment #

Before installing PyTorch, you need to install Python 3 and pip, which are the most basic operations. You can easily find instructions for these online, and I believe you can do them independently.

Here, I will start directly with the installation of PyTorch. Installing PyTorch is very, very easy, and there are many methods. Let’s start with the simplest method: using pip.

Installing PyTorch using pip #

For CPU version installation:

# Linux
pip install torch==1.9.0+cpu torchvision==0.10.0+cpu torchaudio==0.9.0 -f https://download.pytorch.org/whl/torch_stable.html
# Mac & Windows
pip install torch torchvision torchaudio

For GPU version installation (default CUDA version 11.1):

# Linux & Windows
pip install torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio==0.9.0 -f https://download.pytorch.org/whl/torch_stable.html

You just need to copy the above commands into the command line of your computer to quickly install PyTorch.

There are two versions here, one for GPU and one for CPU. It is recommended to install the GPU version if you have an NVIDIA graphics card. Training deep learning models with a GPU is very fast, so in actual projects, GPUs are used for training models.

However, if you do not have an NVIDIA GPU for development, you can install the CPU version. In the learning process, the CPU is sufficient for running small experiments.

Additionally, before installing the GPU version, you need to install the corresponding version of the CUDA toolkit. You can go to the NVIDIA official website and choose the CUDA toolkit for the corresponding operating system to download and install. If your hardware does not have an NVIDIA graphics card, you can skip this step.

The current stable version of PyTorch is 1.9.0. If PyTorch is updated to a newer version in the future, you only need to modify the version number in the commands.

Other methods to install PyTorch #

Here is the official website of PyTorch. On this page, you can see the configuration options and installation commands.

Image

You can select the PyTorch version, your operating system, installation method, programming language, and compute platform according to the instructions on the page, and then install it using the command provided at the bottom.

Please note that Mac operating systems can only install the CPU version. From my experience, the easiest method is still to use pip for installation.

Verifying the installation #

In the terminal, enter “python” to enter the Python interactive mode.

First, enter the following code. If there is no error, it means that PyTorch has been successfully installed:

import torch

Next, enter the following code. If the output is “True”, it means that you can use the GPU. This line of code checks if the GPU is available:

torch.cuda.is_available()

You may have a question here: why did I install the GPU version but the code returns “False” and shows that the GPU is not available?

For this question, we can check step by step according to the following instructions.

  1. Check if your computer has a GPU that supports CUDA.

First, check the graphics card model and whether you have a dedicated graphics card. If there is no dedicated graphics card starting with “NVIDIA”, it means that CUDA is not supported, and therefore the GPU is not available.

Then, you can check if your GPU supports CUDA on this page. If your GPU model is listed on the page, it means that your computer is equipped with a modern GPU that can accelerate applications using CUDA. Otherwise, the GPU is also not available.

If the GPU supports CUDA, make sure you have completed the installation of the CUDA toolkit mentioned above.

  1. Check the graphics card driver version.

In the terminal, enter the command “nvidia-smi” to display the graphics card driver version and CUDA version, as shown in the following image:

Image

If the graphics card driver version is too low and does not match the CUDA version, the GPU is also not available. You need to update the graphics card driver according to the graphics card model.

I have summarized the correspondence between the CUDA version and GPU driver version in a table. You can refer to this table and check it according to the driver on your computer. For example, CUDA 11.1 is supported by Linux drivers 450.80.02 and above:

Image

You can download and install the graphics card driver program here.

  1. Check if the PyTorch version and CUDA version match.

The PyTorch version also corresponds to the CUDA version. You can see the correspondence between them on this page. If the versions do not match, you can reinstall the corresponding version of PyTorch or upgrade the CUDA toolkit.

Using Docker #

Using PyTorch through Docker is also very simple. You don’t even need to install anything, but you need to be familiar with Docker.

If you are proficient in using Docker, I recommend the following link for your reference. Here, you can find many PyTorch Docker images. You can pull an image that suits your needs onto your server or local machine and start it directly without additional environment configuration.

Common Programming Tools #

Before we start programming with PyTorch, let’s take a look at several common programming tools. However, you are not required to use them; you can freely choose according to your preference.

Sublime Text #

Sublime Text is a lightweight and powerful text editing tool that comes with many quick and handy features for development.

Image

For example, it can automatically generate indexes for classes, methods, and functions in a project, allowing us to track the code. Specifically, it uses its “goto anything” feature to find the corresponding code lines in the project based on certain keywords. Additionally, it supports a wide range of plugin functionalities.

PyCharm #

PyCharm is an editor specifically designed for Python. It is easy to configure, powerful, and saves time and effort, making it beginner-friendly. It has all the features typically found in an Integrated Development Environment (IDE), such as syntax highlighting, project management, code navigation, code completion, debugging, unit testing, version control, and more.

Vim #

Vim is a text editing tool commonly used in Linux systems. It is highly convenient, fast, and powerful. We often use it in projects.

In our projects, we often need to log in to a server for development. Servers are usually based on Linux systems, so we use Vim to open and directly edit code.

For those who haven’t used Linux or are used to programming with IDEs, Vim may not seem very convenient initially. However, Vim offers a rich set of shortcuts and is highly efficient for Shell and Python development.

But as mentioned earlier, Vim has a learning curve and requires you to learn how to use it. However, once you learn it, I guarantee you will love it (I also recommend interested students to check out the adjacent Vim Practical Skills You Must Know column).

Jupyter Notebook & Lab #

Jupyter Notebook is an open-source web application and the tool I most want to recommend to you. It allows you to create and share documents that contain executable code, visualizations, and explanatory text.

In the upcoming courses, if we need to generate images or display results, we will also use Jupyter Notebook. I recommend you install it beforehand.

In short, Jupyter Notebook is opened in a web page where you can directly write and execute code. The code execution results are displayed directly below the code cells. For example, when writing documentation during programming, you can write directly in the same page, making it easy to explain and clarify in a timely manner.

Jupyter Lab can be seen as the ultimate evolution of Jupyter Notebook. It not only includes all the features of Jupyter Notebook but also integrates features such as operating terminals, interactive mode, viewing CSV files and images, among others.

Image

Jupyter Notebook is very active in our field of deep learning. It is more convenient than coding directly in .py files during the experimental testing phase. When writing project reports after the project is completed, I think it is also more suitable to use Jupyter Notebook.

Installing Jupyter using pip #

To install Jupyter Notebook using pip, use the following command.

pip install jupyter

To install Jupyter Lab using pip, use the following command.

pip install jupyterlab

Starting Jupyter #

Once the installation is complete, you can start Jupyter Notebook. Simply execute the following command directly in the terminal.

jupyter notebook

To start Jupyter Lab, execute the following command in the terminal.

jupyter lab

Regardless of whether you are using macOS or Windows, after successfully starting Jupyter Notebook or Jupyter Lab using any of the above methods, the browser will automatically open the development environment for Jupyter Notebook or Jupyter Lab (you can refer to the interface in the “Jupyter Notebook & Lab” example).

Running Jupyter Notebook #

Once in the Jupyter Notebook interface, let’s try creating a new Python notebook. The specific steps are shown in the following figure. Click the “New” drop-down menu and then click the “Python 3” option to create a new Python notebook.

We have already discussed the installation method for PyTorch above. We can execute the following code to see if PyTorch has been successfully installed.

import torch
torch.__version__

Click the run button, and we can see the result of the code execution, which outputs the version of PyTorch currently installed, PyTorch 1.9.0 with GPU support. This indicates that the PyTorch framework has been successfully installed.

Summary #

Congratulations on completing this lesson.

Today, we have learned about the uses of the PyTorch framework. In simple terms, it is a framework that can utilize GPU to handle a series of complex calculations in deep learning.

To use this tool effectively, we need to design the overall task process and network architecture so that PyTorch can implement various complex computational functions.

After that, we learned about the installation method of the PyTorch framework, and I also recommended some commonly used tools for deep learning programming. The tool I recommend the most is Jupyter Notebook, which is often used in the field of deep learning. We will also use it for image generation or result demonstration in future lessons.

These are the preparations for the course. Let’s get started together, set up the environment, and choose a development tool that you feel comfortable using, and start the exploration journey of PyTorch!

A journey of a thousand miles begins with a single step. I’ll see you in the next lesson, and if you have any questions, you can communicate with me through the comments section.

I am Fang Yuan, see you in the next lecture!