Pytorch Sequential

You have already learned how to create a Neural Network in Pytorch. In this chapter of the Pytorch tutorial, you will learn about the Pytorch Sequential API and how to create a Neural Network using it. Creating Neural Networks using the Pytorch Sequential API makes it simple, easy, and compact while reducing redundancy and complications.

In this chapter, we will create the same Neural Network that we created in the last one, but using the Sequential API.

The Sequential API

The use of nn.Sequential allows you to stack Neural Network layers on top of each other. This stack of layers is treated as a single layer when creating the architecture of the Neural Network. This stack of layers is also treated as a single layer when defining the flow of data through the model. The flow of data from one layer to the next layer is in the same order as they are passed to nn.Sequential.

Example

In this example, we are making use of nn.Sequential to create a stack of 3 Dense Layers and 2 ReLU activation layers in a specified order.

# densenet is a stack of multiple layers chained together
densenet = nn.Sequential([
               nn.Linear(784, 300),
               nn.ReLU(),
               nn.Linear(300, 100),
               nn.ReLU(),
               nn.Linear(100, 10)
           ])

Notice how we are chaining a stack of multiple layers and treating it like a single layer- densenet.


Creating a Neural Network

Creating a Neural Network using the Sequential API is not much different from creating a Neural Network otherwise. The steps for creating a Neural Network while using the Sequential API are-

  • Create a class for your Neural Network by sub-classing the nn.Module class.
  • Define the architecture of Neural Network in the constructor and using the constructor of the parent class to initialize parameters.
  • Implement the forward() method of the class to define the flow of data through the model.

It is inside the constructor of the class that you make of the Sequential API. Once you have made use of the Sequential API to create the architecture of the Neural Network, you have to define the flow of data through this architecture inside the forward() method.

Example

In the following example we are creating a Neural Network class by making use of the densenet layer we created in the previous example.

class MyNeuralNetwork(nn.Module):
    def __init__(self):
        super(MyNeuralNetwork, self).__init__()
        self.densenet=nn.Sequential(
            nn.Linear(784, 300)
            nn.ReLU()
            nn.Linear(300, 100)
            nn.ReLU()
            nn.Linear(100, 10)
        )
    def forward(self, x):
        x = self.densenet(x)
        return x

Notice how the densenet layer is treated as a single layer inside the forward() method.


Creating the Model

Once you have created the class for your Neural Network, you just need to instantiate it to create your model. This will create a Neural Network.

# Creating a Neural Network model using Sequential API
mynet = MyNeuralNetwork()