Skip to content

Commit 5cc89d8

Browse files
authored
Merge pull request #956 from pytorch/jlin27_tutorials_refresh
Creating preview with some new recipes
2 parents 3752185 + a087673 commit 5cc89d8

File tree

9 files changed

+947
-8
lines changed

9 files changed

+947
-8
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,4 @@ html-noplot:
115115

116116
clean-cache:
117117
make clean
118-
rm -rf advanced beginner intermediate
118+
rm -rf advanced beginner intermediate recipes

index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ Frontend APIs
194194
:tooltip: Implement custom classes in C++ for TorchScript
195195
:description: :doc:`/advanced/torch_script_custom_classes`
196196
:figure: _static/img/cpp_logo.png
197-
197+
198198
.. customgalleryitem::
199199
:tooltip: Autograd in C++ Frontend
200200
:figure: /_static/img/cpp-pytorch.png
@@ -264,7 +264,7 @@ Parallel and Distributed Training
264264
:tooltip: PyTorch distributed trainer with Amazon AWS
265265
:description: :doc:`/beginner/aws_distributed_training_tutorial`
266266
:figure: _static/img/distributed/DistPyTorch.jpg
267-
267+
268268
.. customgalleryitem::
269269
:tooltip: Implementing a Parameter Server Using Distributed RPC Framework
270270
:description: :doc:`/intermediate/rpc_param_server_tutorial`

recipes_source/recipes/README.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
PyTorch Recipes
2+
---------------------------------------------
3+
1. loading_data_recipe.py
4+
Loading Data in PyTorch
5+
https://pytorch.org/tutorials/recipes/recipes/loading_data_recipe.html
6+
7+
2. defining_a_neural_network.py
8+
Defining a Neural Network in PyTorch
9+
https://pytorch.org/tutorials/recipes/recipes/defining_a_neural_network.html
10+
11+
3. what_is_state_dict.py
12+
What is a state_dict in PyTorch
13+
https://pytorch.org/tutorials/recipes/recipes/what_is_state_dict.html
14+
15+
4. saving_and_loading_models_for_inference.py
16+
Saving and loading models for inference in PyTorch
17+
https://pytorch.org/tutorials/recipes/recipes/saving_and_loading_models_for_inference.html
18+
19+
4. custom_dataset_transforms_loader.py
20+
Developing Custom PyTorch Dataloaders
21+
https://pytorch.org/tutorials/recipes/recipes/custom_dataset_transforms_loader.html
22+
23+
24+
5. Captum_Recipe.py
25+
Model Interpretability using Captum
26+
https://pytorch.org/tutorials/recipes/recipes/Captum_Recipe.html
27+
28+
6. dynamic_quantization.py
29+
Dynamic Quantization
30+
https://pytorch.org/tutorials/recipes/recipes/dynamic_quantization.html
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
"""
2+
Defining a Neural Network in PyTorch
3+
====================================
4+
Deep learning uses artificial neural networks (models), which are
5+
computing systems that are composed of many layers of interconnected
6+
units. By passing data through these interconnected units, a neural
7+
network is able to learn how to approximate the computations required to
8+
transform inputs into outputs. In PyTorch, neural networks can be
9+
constructed using the ``torch.nn`` package.
10+
11+
Introduction
12+
------------
13+
PyTorch provides the elegantly designed modules and classes, including
14+
``torch.nn``, to help you create and train neural networks. An
15+
``nn.Module`` contains layers, and a method ``forward(input)`` that
16+
returns the ``output``.
17+
18+
In this recipe, we will use ``torch.nn`` to define a neural network
19+
intended for the `MNIST
20+
dataset <https://pytorch.org/docs/stable/torchvision/datasets.html#mnist>`__.
21+
22+
Setup
23+
-----
24+
Before we begin, we need to install ``torch`` if it isn’t already
25+
available.
26+
27+
::
28+
29+
pip install torchaudio
30+
31+
32+
"""
33+
34+
35+
######################################################################
36+
# Steps
37+
# -----
38+
#
39+
# 1. Import all necessary libraries for loading our data
40+
# 2. Define and intialize the neural network
41+
# 3. Specify how data will pass through your model
42+
# 4. [Optional] Pass data through your model to test
43+
#
44+
# 1. Import necessary libraries for loading our data
45+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46+
#
47+
# For this recipe, we will use ``torch`` and its subsidiaries ``torch.nn``
48+
# and ``torch.nn.functional``.
49+
#
50+
51+
import torch
52+
import torch.nn as nn
53+
import torch.nn.functional as F
54+
55+
56+
######################################################################
57+
# 2. Define and intialize the neural network
58+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59+
#
60+
# Our network will recognize images. We will use a process built into
61+
# PyTorch called convolution. Convolution adds each element of an image to
62+
# its local neighbors, weighted by a kernel, or a small martrix, that
63+
# helps us extract certain features (like edge detection, sharpness,
64+
# blurriness, etc.) from the input image.
65+
#
66+
# There are two requirements for defining the ``Net`` class of your model.
67+
# The first is writing an ``__init__`` function that references
68+
# ``nn.Module``. This function is where you define the fully connected
69+
# layers in your neural network.
70+
#
71+
# Using convolution, we will define our model to take 1 input image
72+
# channel, and output match our target of 10 labels representing numbers 0
73+
# through 9. This algorithm is yours to create, we will follow a standard
74+
# MNIST algorithm.
75+
#
76+
77+
class Net(nn.Module):
78+
def __init__(self):
79+
super(Net, self).__init__()
80+
81+
# First 2D convolutional layer, taking in 1 input channel (image),
82+
# outputting 32 convolutional features, with a square kernel size of 3
83+
self.conv1 = nn.Conv2d(1, 32, 3, 1)
84+
# Second 2D convolutional layer, taking in the 32 input layers,
85+
# outputting 64 convolutional features, with a square kernel size of 3
86+
self.conv2 = nn.Conv2d(32, 64, 3, 1)
87+
88+
# Designed to ensure that adjacent pixels are either all 0s or all active
89+
# with an input probability
90+
self.dropout1 = nn.Dropout2d(0.25)
91+
self.dropout2 = nn.Dropout2d(0.5)
92+
93+
# First fully connected layer
94+
self.fc1 = nn.Linear(9216, 128)
95+
# Second fully connected layer that outputs our 10 labels
96+
self.fc2 = nn.Linear(128, 10)
97+
98+
my_nn = Net()
99+
print(my_nn)
100+
101+
102+
######################################################################
103+
# We have finished defining our neural network, now we have to define how
104+
# our data will pass through it.
105+
#
106+
# 3. Specify how data will pass through your model
107+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108+
#
109+
# When you use PyTorch to build a model, you just have to define the
110+
# ``forward`` function, that will pass the data into the computation graph
111+
# (i.e. our neural network). This will represent our feed-forward
112+
# algorithm.
113+
#
114+
# You can use any of the Tensor operations in the ``forward`` function.
115+
#
116+
117+
class Net(nn.Module):
118+
def __init__(self):
119+
super(Net, self).__init__()
120+
self.conv1 = nn.Conv2d(1, 32, 3, 1)
121+
self.conv2 = nn.Conv2d(32, 64, 3, 1)
122+
self.dropout1 = nn.Dropout2d(0.25)
123+
self.dropout2 = nn.Dropout2d(0.5)
124+
self.fc1 = nn.Linear(9216, 128)
125+
self.fc2 = nn.Linear(128, 10)
126+
127+
# x represents our data
128+
def forward(self, x):
129+
# Pass data through conv1
130+
x = self.conv1(x)
131+
# Use the rectified-linear activation function over x
132+
x = F.relu(x)
133+
134+
x = self.conv2(x)
135+
x = F.relu(x)
136+
137+
# Run max pooling over x
138+
x = F.max_pool2d(x, 2)
139+
# Pass data through dropout1
140+
x = self.dropout1(x)
141+
# Flatten x with start_dim=1
142+
x = torch.flatten(x, 1)
143+
# Pass data through fc1
144+
x = self.fc1(x)
145+
x = F.relu(x)
146+
x = self.dropout2(x)
147+
x = self.fc2(x)
148+
149+
# Apply softmax to x
150+
output = F.log_softmax(x, dim=1)
151+
return output
152+
153+
154+
######################################################################
155+
# 4. [Optional] Pass data through your model to test
156+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
157+
#
158+
# To ensure we receive our desired output, let’s test our model by passing
159+
# some random data through it.
160+
#
161+
162+
# Equates to one random 28x28 image
163+
random_data = torch.rand((1, 1, 28, 28))
164+
165+
my_nn = Net()
166+
result = my_nn(random_data)
167+
print (result)
168+
169+
170+
######################################################################
171+
# Each number in this resulting tensor equates to the prediction of the
172+
# label the random tensor is associated to.
173+
#
174+
# Congratulations! You have successfully defined a neural network in
175+
# PyTorch.
176+
#
177+
# Learn More
178+
# ----------
179+
#
180+
# Take a look at these other recipes to continue your learning:
181+
#
182+
# - TBD
183+
# - TBD

0 commit comments

Comments
 (0)