Using Sequential module to build a neural network

Dipanwita Mallick
IWriteAsILearn
Published in
2 min readMay 23, 2021

In my previous post(follow link), I have talked about building your neural network using nn module offered by PyTorch.

In this article, I am going to show you how you can make the same neural network by using the Sequential module in PyTorch. But before that,

what is the Sequential module?

The nn.Sequential is a module that contains other modules and applies them in sequence to produce its output. Each Linear Module computes output from the input using a linear function and holds internal Tensors for its weight and bias. (reference: link)

Okay so now, let’s get started:

Step 1: Prepare data (the goal here is to train the model to subtract)

Step 2: We need Dataset and DataLoader for batch processing of our data

You can use iter and next to view the items in the dataloader like this:

every time you hit next, you will get the next item until the last one, also note that the return list has 2 data points since the batch size we specified is 2

Step 3: build the neural network

Step 4: Train the model

Step 5: Plot the losses with the epoch counts

Step 6: Model on the unknown data

sum(-1) essential means — return the last dimension values in NumPy

Step 7: Print the model summary

model.state_dict() returns a dictionary(OrderedDict) of keys and values

Okay, that was easy! :)

Leave a comment with any suggestions. Thank you for taking your time to read this article.

*****************************The End***************************

--

--

Dipanwita Mallick
IWriteAsILearn

I am working as a Senior Data Scientist at Hewlett Packard Enterprise. I love exploring new ideas and new places !! :)