diff --git a/beginner_source/quickstart/build_model_tutorial.py b/beginner_source/quickstart/build_model_tutorial.py index 728bb610ca6..3f10e798457 100644 --- a/beginner_source/quickstart/build_model_tutorial.py +++ b/beginner_source/quickstart/build_model_tutorial.py @@ -1,25 +1,22 @@ """ Build Model Tutorial ======================================= -""" -############################################### -# The data has been loaded and transformed we can now build the model. -# We will leverage `torch.nn `_ +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. -# 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. -# The neural network modules layers will be added to it in the order they are passed in. -# -# Another way to bulid this model is with a class -# using `nn.Module `_ 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 -# +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 `_ +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: @@ -86,6 +83,15 @@ def forward(self, x): 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 # ------------------------- @@ -94,30 +100,29 @@ def forward(self, x): # ################################################## -# `nn.Flatten `_ to reduce tensor dimensions to one. +# `nn.Flatten `_ # ----------------------------------------------- # +# First we call nn.Flatten to reduce tensor dimensions to one. +# # From the docs: # ``torch.nn.Flatten(start_dim: int = 1, end_dim: int = -1)`` # Here is an example using one of the training_data set items: tensor = training_data[0][0] print(tensor.size()) -# Output: torch.Size([1, 28, 28]) - model = nn.Sequential( nn.Flatten() ) flattened_tensor = model(tensor) flattened_tensor.size() -# Output: torch.Size([1, 784]) - ############################################## # `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 +# transform that will calculate/learn the weights and the bias. # # From the docs: @@ -134,17 +139,48 @@ def forward(self, x): output = model(input) output.size() - -# Output: -# torch.Size([1, 28, 28]) -# torch.Size([1, 512]) - ################################################# # Activation Functions # ------------------------- # -# - `nn.ReLU `_ Activation -# - `nn.Softmax `_ Activation +# After the first two linear layer we will call the `nn.ReLU `_ +# activation function. Then after the third linear layer we call the `nn.Softmax `_ +# activation to rescale between 0 and 1 and sum to one. +# + +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) + + +################################################### +# 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. # # Next: Learn more about how the `optimzation loop works with this example `_. # diff --git a/beginner_source/quickstart/save_load_run_tutorial.py b/beginner_source/quickstart/save_load_run_tutorial.py index 3573e87b763..4f4d671ec04 100644 --- a/beginner_source/quickstart/save_load_run_tutorial.py +++ b/beginner_source/quickstart/save_load_run_tutorial.py @@ -42,16 +42,7 @@ # These two steps are illustrated here: # recreate model -loaded_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) -) - +loaded_model = NeuralNetwork() # hydrate state dictionary loaded_model.load_state_dict(torch.load('model.pth')) diff --git a/beginner_source/quickstart_tutorial.py b/beginner_source/quickstart_tutorial.py index eaa06edc2c4..436340930b2 100644 --- a/beginner_source/quickstart_tutorial.py +++ b/beginner_source/quickstart_tutorial.py @@ -178,15 +178,7 @@ def test(dataloader, model): # inference). Check out more details on `saving, loading and running models with Pytorch `_ # -loaded_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) - ) +loaded_model = NeuralNetwork() loaded_model.load_state_dict(torch.load('model.pth')) loaded_model.eval()