본문 바로가기
Tech Development/Computer Vision (PyTorch)

PyTorch: The Fundamentals & Applications in CNN

by JK from Korea 2023. 4. 23.

<PyTorch: The Fundamentals & Applications in CNN>

 

Date: 2023.01.11                

 

* The PyTorch series will mainly touch on the problem I faced. For actual code, check out my github repository.

 

[Intro to PyTorch]

In the previous posts, I built a CNN from scratch using python. The process was fruitful because it touched the specific functionalities of specific functions and variables. However, the code could have been shorter and more efficient. Thus, using PyTorch, I will rebuild the neural network.

 

So what is PyTorch?

 

PyTorch is a Python-based library that provides multiple APIs over various fields of applications. I have yet to dive into the functionalities of PyTorch, but briefly skimming through the official website, torch.nn and torchvision packages will be mainly used to build our neural network.

 

Hopefully, by rebuilding the CNN through PyTorch, I can better grasp the concepts in neural networks and optimization that I needed clarification on in my previous posts.

 

I will be using a youtube tutorial guide as my guidance for these series. There are several environments to start building networks via pytorch. I chose Jupyter Notebook installed on my mac and coded the majority on vscode, which can be integrated with jupyter notebook.

 

Tutorial Link: https://www.youtube.com/watch?v=Z_ikDlimN6A&list=WL&index=20&t=6879s

 

[The SetUp]

I am using a local version of Jupyter Notebook, and I have encountered several problems while setting up pytorch. So far, I have managed to solve them. The local setup in vscode differs from the recommended setup from the video, so you might not want to go down this road. Here is my setup.

 

[Guideline for Installation]

Go to the official pytorch website and find the installation command for your case. After installing pytorch, I started a session on my jupyter notebook. I had jupyter notebook installed from a previous datathon I participated in. The installation is relatively simple, and you can just google it.

 

[This is the code in the first code cell of jupyter notebook]

 

[It should look something like this on your screen&hellip;]

As per the code above, the cell should print 1.13.1, your pytorch version, when you run this. However, if it fails, press “command+shift+p” to open your vscode editor commands and type “Python: Select Interpreter,” which will look like the figure below.

 

[Choose the Global Interpreter since the Conda might cause you trouble]

The “Python: Select Interpreter” command displays a list of available environments depending on what you installed or where your current workspace is. In my case, you can see that I have the default global environment and conda. You can also choose to work in a virtual environment that separates your extensions and installations within the virtual environment. However, you must access the virtual environment while working on your project.

 

[Interpreters and Environments]

As for any language, interpreters are simply translators for the machine to understand code. The interpreter will check errors and output the logic within the program.

 

So how is the interpreter related to the environment? As said, the interpreter translates. The interpreter might need certain ingredients to understand and translate the code in this translation process. For example, if we use pytorch, the interpreter needs to know the functionalities of pytorch to understand the pytorch functionalities. Thus, the environment contains the ingredients for the interpreter. For example, if we install pytorch in the global environment, the interpreter of the global environment will understand any pytorch syntax; however, separate environments such as conda or virtual (venv) will not.

 

In conclusion, depending on which environment you install pytorch, you should use that environment. If you install pytorch in the conda environment and try to execute pytorch code in the global environment, the code will fail.

 

The global environment is the default one. Conda is a popular package manager and environment management system that allows the user to create and manage environments for python. With conda, you can more effectively create isolated environments that include python specific libraries and modules for the project. The virtual environment is a lighter version of conda.

 

For my case, I ended up installing pytorch for both global and conda. So the interpreter in either environment understands what I’m doing.

728x90
반응형

댓글