Skip to content

Commit 74be97c

Browse files
cassiebreviusubramen
authored andcommitted
Some updates based on feedback to quickstart and build model (pytorch#29)
* Added more detail to the intro of the quickstart * primitive dataset text update * fix build model
1 parent 15a5cbc commit 74be97c

File tree

2 files changed

+124
-78
lines changed

2 files changed

+124
-78
lines changed
Lines changed: 120 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,144 @@
11
"""
2-
Optimizing Model Parameters
3-
===================
4-
Now that we have a model and data it's time to train, validate and test our model by optimizating it's paramerters on our data!
5-
To do this we need to understand a how to handle 5 core deep learning concepts in PyTorch
6-
1. Hyperparameters (learning rates, batch sizes, epochs etc)
7-
2. Optimization Loops
8-
3. Loss
9-
4. AutoGrad
10-
5. Optimizers
11-
Let's dissect these concepts one by one and look at some code at the end we'll see how it all fits together.
12-
Hyperparameters
13-
-----------------
2+
Build Model Tutorial
3+
=======================================
144
"""
155

16-
######################################################
17-
# Hyperparameters are adjustable parameters that let you control the model optimization process. For example, with neural networks, you can configure:
6+
###############################################
7+
# The data has been loaded and transformed we can now build the model.
8+
# We will leverage `torch.nn <https://pytorch.org/docs/stable/nn.html>`_
9+
# predefined layers that Pytorch has that can simplify our code.
10+
#
11+
# In the below example, for our FashionMNIT image dataset, we are using a `Sequential`
12+
# container from class `torch.nn. Sequential <https://pytorch.org/docs/stable/generated/torch.nn.Sequential.html>`_
13+
# that allows us to define the model layers inline.
14+
# The neural network modules layers will be added to it in the order they are passed in.
15+
#
16+
# Another way to bulid this model is with a class
17+
# using `nn.Module <https://pytorch.org/docs/stable/generated/torch.nn.Module.html)>`_ This gives us more flexibility, because
18+
# we can construct layers of any complexity, including the ones with shared weights.
1819
#
19-
# - **Number of Epochs**- the number times iterate over the dataset to update model parameters
20-
# - **Batch Size** - the number of samples in the dataset to evaluate before you update model parameters
21-
# - **Cost Function** - the method used to decide how to evaluate the model on a data sample to update the model parameters
22-
# - **Learning Rate** - how much to update models parameters at each batch/epoch set this to large and you won't update optimally if you set it to small you will learn really slowly
20+
# Lets break down the steps to build this model below
21+
#
22+
23+
##########################################
24+
# Inline nn.Sequential Example:
25+
# ----------------------------
26+
#
27+
28+
import os
29+
import torch
30+
import torch.nn as nn
31+
import torch.onnx as onnx
32+
from torch.utils.data import DataLoader
33+
from torchvision import datasets, transforms
34+
35+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
36+
print('Using {} device'.format(device))
2337

24-
learning_rate = 1e-3
25-
batch_size = 64
26-
epochs = 5
38+
# model
39+
model = nn.Sequential(
40+
nn.Flatten(),
41+
nn.Linear(28*28, 512),
42+
nn.ReLU(),
43+
nn.Linear(512, 512),
44+
nn.ReLU(),
45+
nn.Linear(512, len(classes)),
46+
nn.Softmax(dim=1)
47+
).to(device)
48+
49+
print(model)
2750

28-
######################################################
29-
# Optimizaton Loops
30-
# -----------------
51+
#############################################
52+
# Class nn.Module Example:
53+
# --------------------------
3154
#
32-
# Once we set our hyperparameters we can then optimize our our model with optimization loops.
55+
56+
class NeuralNework(nn.Module):
57+
def __init__(self, x):
58+
super(NeuralNework, self).__init__()
59+
self.layer1 = nn.Linear(28*28, 512)
60+
self.layer2 = nn.Linear(512, 512)
61+
self.output = nn.Linear(512, 10)
62+
63+
def forward(self, x):
64+
x = F.relu(self.layer1(x))
65+
x = F.relu(self.layer2(x))
66+
x = self.output(x)
67+
return F.softmax(x, dim=1)
68+
69+
#############################################
70+
# Get Device for Training
71+
# -----------------------
72+
# Here we check to see if `torch.cuda <https://pytorch.org/docs/stable/notes/cuda.html>`_ is available to use the GPU, else we will use the CPU.
3373
#
34-
# The optimziation loop is comprized of three main subloops in PyTorch.
74+
# Example:
3575
#
3676

37-
############################################################
38-
# .. figure:: /_static/img/quickstart/optimizationloops.png
39-
# :alt:
77+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
78+
print('Using {} device'.format(device))
79+
80+
##############################################
81+
# The Model Module Layers
82+
# -------------------------
83+
#
84+
# Lets break down each model layer in the FashionMNIST model.
4085
#
4186

42-
#############################################################
43-
# 1. The Train Loop - Core loop iterates over all the epochs
44-
# 2. The Validation Loop - Validate loss after each weight parameter update and can be used to gauge hyper parameter performance and update them for the next batch.
45-
# 3. The Test Loop - is used to evaluate our models performance after each epoch on traditional metrics to show how much our model is generalizing from the train and validation dataset to the test dataset it's never seen before.
87+
##################################################
88+
# `nn.Flatten <https://pytorch.org/docs/stable/generated/torch.nn.Flatten.html>`_ to reduce tensor dimensions to one.
89+
# -----------------------------------------------
4690
#
91+
# From the docs:
92+
# ``torch.nn.Flatten(start_dim: int = 1, end_dim: int = -1)``
93+
#
94+
95+
# Here is an example using one of the training_data set items:
96+
tensor = training_data[0][0]
97+
print(tensor.size())
98+
99+
# Output: torch.Size([1, 28, 28])
47100

48-
for epoch in range(num_epochs):
49-
# Optimization Loop
50-
# Train loop over batches
51-
model.train() # set model to train
52-
# Model Update Code
53-
model.eval() # After exiting batch loop set model to eval to speed up evaluation and not track gradients (this is explained below)
54-
# Validation Loop
55-
# - Put sample validation metric logging and hyperparameter update code here
56-
# After exiting train loop set model to eval to speed up evaluation and not track gradients (this is explained below)
57-
# Test Loop
58-
# - Put sample test metric logging and hyperparameter update code here
101+
model = nn.Sequential(
102+
nn.Flatten()
103+
)
104+
flattened_tensor = model(tensor)
105+
flattened_tensor.size()
59106

60-
######################################################
61-
# Loss
62-
# -----------------
107+
# Output: torch.Size([1, 784])
108+
109+
##############################################
110+
# `nn.Linear <https://pytorch.org/docs/stable/generated/torch.nn.Linear.html>`_ to add a linear layer to the model.
111+
# -------------------------------
112+
#
113+
# Now that we have flattened our tensor dimension we will apply a linear layer transform that will calculate/learn the weights and the bias.
63114
#
64-
# The loss is the value used to update our parameters. To calculate the loss we make a prediction using the inputs of our given data sample.
115+
# From the docs:
116+
#
117+
# ``torch.nn.Linear(in_features: int, out_features: int, bias: bool = True)``
65118
#
66119

67-
preds = model(inputs)
68-
loss = cost_function(preds, labels)
69-
# Make sure previous gradients are cleared
70-
optimizer.zero_grad()
71-
# Calculates gradients with respect to loss
72-
loss.backward()
73-
optimizer.step()
120+
input = training_data[0][0]
121+
print(input.size())
122+
model = nn.Sequential(
123+
nn.Flatten(),
124+
nn.Linear(28*28, 512),
125+
)
126+
output = model(input)
127+
output.size()
74128

75-
######################################################
76-
# The standard method for optimization is called Stochastic Gradient Descent, to learn more check out this awesome video by `3blue1brown <https://www.youtube.com/playlist?list=PLZHQObOWTQDNU6R1_67000Dx_ZCJB-3pi>`_. There are many different optimizers and variations of this method in PyTorch such as ADAM and RMSProp that work better for different kinds of models, they are out side the scope of this Blitz, but can check out the full list of optimizers `here <https://pytorch.org/docs/stable/optim.html>`_
77129

78-
######################################################
79-
# Putting it all together lets look at a basic optimization loop
80-
# -----------------
81-
#
82-
# Initilize optimizer and example cost function
130+
# Output:
131+
# torch.Size([1, 28, 28])
132+
# torch.Size([1, 512])
133+
134+
#################################################
135+
# Activation Functions
136+
# -------------------------
83137
#
84-
# For loop to iterate over epoch
85-
# - Train loop over batches
86-
# - Set model to train mode
87-
# - Calculate loss using
88-
# - clear optimizer gradient
89-
# - loss.backword
90-
# - optimizer step
91-
# - Set model to evaluate mode and start validation loop
92-
# - calculate validation loss and update optimizer hyper parameters
93-
# - Set model to evaluate test loop
138+
# - `nn.ReLU <https://pytorch.org/docs/stable/generated/torch.nn.ReLU.html)>`_ Activation
139+
# - `nn.Softmax <https://pytorch.org/docs/stable/generated/torch.nn.Softmax.html>`_ Activation
94140
#
95-
# Next: Learn more about `AutoGrad <autograd_tutorial.html>`_.
141+
# Next: Learn more about how the `optimzation loop works with this example <optimization_tutorial.html>`_.
96142
#
97-
98-
##################################################################
99143
# .. include:: /beginner_source/quickstart/qs_toc.txt
100-
#
144+
#

beginner_source/quickstart_tutorial.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
===================
44
55
6-
The basic machine learning concepts in any framework should include: Working with data, Creating models, Optimizing Parameters, Saving and Loading Models. In this quickstart we will go through an example of an applied machine learning model using the FashionMNIST dataset that demonstrates these core steps using Pytorch.
6+
The basic machine learning concepts in any framework should include: Working with data, Creating models, Optimizing Parameters, Saving and Loading Models. In this PyTorch Quickstart we will go through these concepts and how to apply them with PyTorch. That dataset we will be using is the FashionMNIST clothing images dataset that demonstrates these core steps applied to create ML Models. You will be introduced to the complete ML workflow using PyTorch with links to learn more at each step. Using this dataset we will be able to predict if the image is one of the following classes: T-shirt/top, Trouser, Pullover, Dress, Coat, Sandal, Shirt, Sneaker, Bag, or Ankle boot. Lets get started!
77
88
Working with data
99
-----------------
@@ -12,8 +12,10 @@
1212
######################################################################
1313
#
1414
# PyTorch has two basic data primitives: ``DataSet`` and ``DataLoader``.
15-
# These ``DataSet`` objects include a ``transforms`` mechanism to
15+
# The `torchvision.datasets` ``DataSet`` object includes a ``transforms`` mechanism to
1616
# modify data in-place. Below is an example of how to load that data from the Pytorch open datasets and transform the data to a normalized tensor.
17+
18+
# This example is using the `torchvision.datasets` which is a subclass from the primitive `torch.utils.data.Dataset`. Note that the primitive dataset doesnt have the built in transforms param like the built in dataset in `torchvision.datasets.`
1719
#
1820
# To see more examples and details of how to work with Tensors, Datasets, DataLoaders and Transforms in Pytoch with this example checkout these resources:
1921
#

0 commit comments

Comments
 (0)