From d9959dc076ceb2ff2e26f15bc70af30f5e04db00 Mon Sep 17 00:00:00 2001 From: Cassie Breviu Date: Tue, 12 Jan 2021 11:34:58 -0600 Subject: [PATCH 1/3] removed full top ex, moved sequential to layer ex --- .../quickstart/build_model_tutorial.py | 98 ++++++------------- 1 file changed, 30 insertions(+), 68 deletions(-) diff --git a/beginner_source/quickstart/build_model_tutorial.py b/beginner_source/quickstart/build_model_tutorial.py index a2e6316c8b0..e0d4e960ee7 100644 --- a/beginner_source/quickstart/build_model_tutorial.py +++ b/beginner_source/quickstart/build_model_tutorial.py @@ -10,22 +10,16 @@ # The data has been loaded and transformed we can now build the model. # We will leverage `torch.nn `_ predefined layers that PyTorch has that can simplify our code. # -# In the below example, for our FashionMNIT image dataset, we are using a `Sequential` -# container from class `torch.nn. Sequential `_ -# that allows us to define the model layers inline. In the "Sequential" in-line model building format the ``forward()`` -# method is created for you and the modules you add are passed in as a list or dictionary in the order that are they are defined. -# -# Another way to bulid this model is with a class -# using `nn.Module `_ +# In the below example, for our FashionMNIT image dataset, we are using `nn.Module `_ # A big plus with using a class that inherits ``nn.Module`` is better parameter management across all nested submodules. # This gives us more flexibility, because we can construct layers of any complexity, including the ones with shared weights. # # Lets break down the steps to build this model below # -########################################## -# Inline nn.Sequential Example: -# ---------------------------- +############################################# +# Import the Packages +# -------------------------- # import os @@ -35,27 +29,32 @@ from torch.utils.data import DataLoader from torchvision import datasets, transforms -device = 'cuda' if torch.cuda.is_available() else 'cpu' -print('Using {} device'.format(device)) - -# model -model = nn.Sequential( - nn.Flatten(), - nn.Linear(28*28, 512), - nn.ReLU(), - nn.Linear(512, 512), - nn.ReLU(), - nn.Linear(512, len(classes)), - nn.Softmax(dim=1) - ).to(device) - -print(model) ############################################# -# Class nn.Module Example: -# -------------------------- +# Get Device for Training +# ----------------------- +# Here we check to see if `torch.cuda `_ +# is available to use the GPU, else we will use the CPU. # +# Example: +# + +device = 'cuda' if torch.cuda.is_available() else 'cpu' +print('Using {} device'.format(device)) +############################################## +# Define the Class +# ------------------------- +# +# The ``init`` function inherits from ``nn.Module`` which is the base class for +# building neural network modules. This function defines the layers in your neural network +# then it initializes the modules to be called in the ``forward`` function. +# +# In the class implementation of the neural network we define a ``forward`` function. +# Then call the ``NeuralNetwork`` class and assign the device. When training the model we will call ``model`` +# and pass the data (x) into the forward function and through each layer of our network. +# +# class NeuralNetwork(nn.Module): def __init__(self): @@ -66,7 +65,6 @@ def __init__(self): self.output = nn.Linear(512, 10) def forward(self, x): - x = self.flatten(x) x = F.relu(self.layer1(x)) x = F.relu(self.layer2(x)) @@ -76,32 +74,14 @@ def forward(self, x): print(model) - -############################################# -# Get Device for Training -# ----------------------- -# Here we check to see if `torch.cuda `_ is available to use the GPU, else we will use the CPU. -# -# Example: -# - -device = 'cuda' if torch.cuda.is_available() else 'cpu' -print('Using {} device'.format(device)) - -############################################## -# __init__ -# ------------------------- -# -# The ``init`` function inherits from ``nn.Module`` which is the base class for -# building neural network modules. This function defines the layers in your neural network -# then it initializes the modules to be called in the ``forward`` function. -# - ############################################## # The Model Module Layers # ------------------------- # -# Lets break down each model layer in the FashionMNIST model. +# Lets break down each model layer in the FashionMNIST model. In the below example we are using a `Sequential` +# container from class `torch.nn. Sequential `_ +# that allows us to define the model layers inline. In the "Sequential" in-line model building format the ``forward()`` +# method is created for you and the modules you add are passed in as a list or dictionary in the order that are they are defined. # ################################################## @@ -166,24 +146,6 @@ def forward(self, x): print(model) -################################################### -# Forward Function -# -------------------------------- -# -# In the class implementation of the neural network we define a ``forward`` function. -# Then call the ``NeuralNetwork`` class and assign the device. When training the model we will call ``model`` -# and pass the data (x) into the forward function and through each layer of our network. -# -# -def forward(self, x): - x = self.flatten(x) - x = F.relu(self.layer1(x)) - x = F.relu(self.layer2(x)) - x = self.output(x) - return F.softmax(x, dim=1) -model = NeuralNetwork().to(device) - - ################################################ # In the next section you will learn about how to train the model and the optimization loop for this example. # From d6ccec65264d827fbe2678914ef2aaefef51d119 Mon Sep 17 00:00:00 2001 From: Cassie Breviu Date: Tue, 12 Jan 2021 12:45:38 -0600 Subject: [PATCH 2/3] fixed linear layer wording --- .../quickstart/build_model_tutorial.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/beginner_source/quickstart/build_model_tutorial.py b/beginner_source/quickstart/build_model_tutorial.py index e0d4e960ee7..2756223ecc8 100644 --- a/beginner_source/quickstart/build_model_tutorial.py +++ b/beginner_source/quickstart/build_model_tutorial.py @@ -46,13 +46,12 @@ # Define the Class # ------------------------- # -# The ``init`` function inherits from ``nn.Module`` which is the base class for -# building neural network modules. This function defines the layers in your neural network +# Here we define the `NeuralNetwork` class which inherits from ``nn.Module`` which is the base class for +# building neural network modules. The ``init`` function defines the layers in the neural network # then it initializes the modules to be called in the ``forward`` function. -# -# In the class implementation of the neural network we define a ``forward`` function. -# Then call the ``NeuralNetwork`` class and assign the device. When training the model we will call ``model`` -# and pass the data (x) into the forward function and through each layer of our network. +# Then we call the ``NeuralNetwork`` class and assign the device. When training +# the model we will call ``model`` and pass the data (x) into the forward function and +# through each layer of our network. # # @@ -107,8 +106,8 @@ def forward(self, x): # `nn.Linear `_ to add a linear layer to the model. # ------------------------------- # -# Now that we have flattened our tensor dimension we will apply a linear layer -# transform that will calculate/learn the weights and the bias. +# Now that we have flattened our tensor dimension we will apply a linear layer. The linear layer is +# a module that applies a linear transformation on the input using it's stored weights and biases. # # From the docs: # From 55ad33be7bd9e37f57108ad0f76221ae063bf6a2 Mon Sep 17 00:00:00 2001 From: Cassie Breviu Date: Tue, 12 Jan 2021 13:50:00 -0600 Subject: [PATCH 3/3] breadcrumb test --- beginner_source/quickstart/data_quickstart_tutorial.py | 9 +++++++++ beginner_source/quickstart/tensor_tutorial.py | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/beginner_source/quickstart/data_quickstart_tutorial.py b/beginner_source/quickstart/data_quickstart_tutorial.py index 3fdacfec77a..cdfec8f0a54 100644 --- a/beginner_source/quickstart/data_quickstart_tutorial.py +++ b/beginner_source/quickstart/data_quickstart_tutorial.py @@ -1,4 +1,13 @@ """ +.. raw:: html + + + +.. raw:: html + Datasets & Dataloaders =================== """ diff --git a/beginner_source/quickstart/tensor_tutorial.py b/beginner_source/quickstart/tensor_tutorial.py index 54afaa08d86..24f248efc6e 100644 --- a/beginner_source/quickstart/tensor_tutorial.py +++ b/beginner_source/quickstart/tensor_tutorial.py @@ -1,4 +1,13 @@ """ +.. raw:: html + +
+ Tensors +
+ +.. raw:: html + + Tensors and Operations ---------------------- **Tensor** is the basic computational unit in PyTorch. It is very