4-6-1-1. Welcome!


Welcome! In this lesson, you’ll learn how to use PyTorch for building deep learning models. PyTorch was released in early 2017 and has been making a pretty big impact in the deep learning community. It’s developed as an open source project by the Facebook AI Research team, but is being adopted by teams everywhere in industry and academia. In my experience, it’s the best framework for learning deep learning and just a delight to work with in general. By the end of this lesson, you’ll have trained your own deep learning model that can classify images of cats and dogs.

I’ll first give you a basic introduction to PyTorch, where we’ll cover tensors – the main data structure of PyTorch. I’ll show you how to create tensors, how to do simple operations, and how tensors interact with NumPy.

Then you’ll learn about a module called autograd that PyTorch uses to calculate gradients for training neural networks. Autograd, in my opinion, is amazing. It does all the work of backpropagation for you by calculating the gradients at each operation in the network which you can then use to update the network weights.

Next you’ll use PyTorch to build a network and run data forward through it. After that, you’ll define a loss and an optimization method to train the neural network on a dataset of handwritten digits. You’ll also learn how to test that your network is able to generalize through validation.

However, you’ll find that your network doesn’t work too well with more complex images. You’ll learn how to use pre-trained networks to improve the performance of your classifier, a technique known as transfer learning.

Follow along with the videos and work through the exercises in your own notebooks. If you get stuck, check out my solution videos and notebooks.

Get the notebooks

The notebooks for this lesson will be provided in the classroom, but if you wish to follow along on your local machine, then the instructions below will help you get setup and ready to learn!

All the notebooks for this lesson are available from our deep learning repo on GitHub. Please clone the repo by typing

git clone https://github.com/udacity/deep-learning-v2-pytorch.git

in your terminal. Then navigate to the intro-to-pytorch directory in the repo.

Follow along in your notebooks to complete the exercises. I’ll also be providing solutions to the exercises, both in videos and in the notebooks marked (Solution).


These notebooks require PyTorch v0.4 or newer, and torchvision. The easiest way to install PyTorch and torchvision locally is by following the instructions on the PyTorch site. Choose the stable version, your appropriate OS and Python versions, and how you’d like to install it. You’ll also need to install numpy and jupyter notebooks, the newest versions of these should work fine. Using the conda package manager is generally best for this,

conda install numpy jupyter notebook

If you haven’t used conda before, please read the documentation to learn how to create environments and install packages. I suggest installing Miniconda instead of the whole Anaconda distribution. The normal package manager pip also works well. If you have a preference, go with that.

The final part of the series has a soft requirement of a GPU used to accelerate network computations. Even if you don’t have a GPU available, you’ll still be able to run the code and finish the exercises. PyTorch uses a library called CUDA to accelerate operations using the GPU. If you have a GPU that CUDA supports, you’ll be able to install all the necessary libraries by installing PyTorch with conda. If you can’t use a local GPU, you can use cloud platforms such as AWSGCP, and FloydHub to train your networks on a GPU.


If you have problems with the notebooks, please contact support or create an issue on the repo. We’re also happy to incorporate your improvements through pull requests.


Make the Environment

1. Installation

Download the latest version of miniconda that matches your system.

64-bit64-bit (bash installer)64-bit (bash installer)64-bit (exe installer)
32-bit32-bit (bash installer)32-bit (exe installer)

Install miniconda on your machine. Detailed instructions:

2. Create and Activate the Environment

For Windows users, these following commands need to be executed from the Anaconda prompt as opposed to a Windows terminal window. For Mac, a normal terminal window will work.

Git and version control

These instructions also assume you have git installed for working with Github from a terminal window, but if you do not, you can download that first with the command:

conda install git

If you’d like to learn more about version control and using git from the command line, take a look at our free course: Version Control with Git.

Now, we’re ready to create our local environment!

  1. Clone the repository, and navigate to the downloaded folder. This may take a minute or two to clone due to the included image data.
git clone https://github.com/udacity/deep-learning-v2-pytorch.git
cd deep-learning-v2-pytorch
  1. Create (and activate) a new environment, named deep-learning with Python 3.6. If prompted to proceed with the install (Proceed [y]/n) type y.
    • Linux or Mac: conda create -n deep-learning python=3.6 source activate deep-learning
    • Windows: conda create --name deep-learning python=3.6 activate deep-learning At this point your command line should look something like: (deep-learning) <User>:deep-learning-v2-pytorch <user>$. The (deep-learning) indicates that your environment has been activated, and you can proceed with further package installations.
  2. Install PyTorch and torchvision; this should install the latest version of PyTorch.
    • Linux or Mac: conda install pytorch torchvision -c pytorch
    • Windows: conda install pytorch -c pytorch pip install torchvision
  3. Install a few required pip packages, which are specified in the requirements text file (including OpenCV).
pip install -r requirements.txt
  1. That’s it!

Now most of the deep-learning libraries are available to you. Very occasionally, you will see a repository with an addition requirements file, which exists should you want to use TensorFlow and Keras, for example. In this case, you’re encouraged to install another library to your existing environment, or create a new environment for a specific project.

Now, assuming your deep-learning environment is still activated, you can navigate to the main repo and start looking at the notebooks:

cd deep-learning-v2-pytorch
jupyter notebook

To exit the environment when you have completed your work session, simply close the terminal window.

For reference:


%d 블로거가 이것을 좋아합니다: