From the course: AI Workshop: Build a Neural Network with PyTorch Lightning (2023)
Setting up the virtual environment - PyTorch Tutorial
From the course: AI Workshop: Build a Neural Network with PyTorch Lightning (2023)
Setting up the virtual environment
In this course, we'll first build a simple neural network model for regression using PyTorch. And you'll see that there are a lot of granular details that you need to know about model building in order to work with PyTorch directly. There will be a lot of boilerplate code. Then we'll basically build the same neural network using PyTorch Lightning. With PyTorch Lightning, we'll eliminate a lot of the boilerplate code and create reusable components. You'll see how much cleaner the code is with PyTorch Lightning. Before we do any of that, let's set up a virtual environment within which we'll install PyTorch and build and train our neural network models. Here I am on my local machine and I have Python installed. Make sure you have a recent version of Python. You can see that I'm working with Python 3.10.9. I'll now create a Python virtual environment. A virtual environment is just an isolated environment for Python projects, ensuring that each project can have its own dependencies regardless of what dependencies other projects may have. This command creates a virtual environment called pytorch_venv using the venv module. When you create a virtual environment, this sets up a directory under your current working directory with the name of your virtual environment. You can see the pytorch_venv directory here. This is where all of the packages that we install in the virtual environment will be set up and stored. Now, to activate the virtual environment, you'll run the source command pytorch_venv/bin/activate. This will activate the virtual environment. Notice that my prompt has changed. The name of the virtual environment is now part of the prompt. The same version of Python that I had installed on my local machine should now be available here in my virtual environment. It's possible to create virtual environments using different versions of Python, but I'm happy with the Python version that I have. In order to be able to work within this virtual environment on a Jupyter Notebook, let's install the ipykernel module in Python. Use pip install to install the latest version of ipykernel. This is the Python package that provides the kernel for Jupyter Notebook and Jupyter Lab. The kernel is just the computational engine that executes the code on the notebook. Now, once we have this installed, run jupyter kernelspec list to see what Python kernels you have available. You can see I have just the one Python 3. I'll now install the kernel associated with my virtual environment using this command here, python -m ipykernel install the kernel with name pytorch_venv. Remember, pytorch_venv is a folder in our current working directory, and it is that folder that will be used to set up the kernel. Thus, we have a kernel with our virtual environment available when we use Jupyter Notebooks. Run jupyter kernelspec list once again, and you can see the pytorch_venv kernel. That's the kernel that we'll use to run our code. We are within our virtual environment. We've installed the pytorch_venv kernel. Let's bring up the Jupyter Notebook server so that we can work within Jupyter Notebook to build and train models using PyTorch and PyTorch Lightning. Here, let's open up a new notebook, click on "New" and make sure that you choose pytorch_venv as the kernel that you want to run your code on. If you choose this kernel, we'll be running within our virtual environment. On the top right, notice the kernel. It's pytorch_venv. If you happen to be in some other kernel and you need to switch, simply select the kernel dropdown menu here on this page, and there you'll find an option to switch your kernel. Meanwhile, let's rename this notebook and give it a meaningful name. Let's call this TrainingRegressionModelUsingPyTorch because first, we'll work with PyTorch, which is much more low-level and involves much more boilerplate in order to understand the different components that you'll use to build a neural network model and then we'll switch to PyTorch Lightning. In order to have more screen space for code, I'm going to toggle the header and toggle the toolbar here in this notebook. So we are only left with the code cells. We don't have all this extra stuff here on top.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
Setting up the virtual environment4m 24s
-
(Locked)
Loading and exploring regression data5m 20s
-
(Locked)
Preprocessing data for training6m 23s
-
(Locked)
Creating a simple neural network5m 43s
-
(Locked)
Setting up the dataset and DataLoader2m 26s
-
(Locked)
Training a neural network using PyTorch7m 45s
-
(Locked)
Visualizing losses and evaluating models2m 47s
-
(Locked)
Building and training a more complex neural network6m 14s
-
-
-
-