Updated April 7, 2023
Definition of PyTorch sequential
PyTorch provides the different types of classes to the user, in which that sequential is, one of the classes that are used to create the PyTorch neural networks without any explicit class. Basically, the sequential module is a container or we can say that the wrapper class is used to extend the nn modules. The sequential container is added to the constructor otherwise we can use the forward() method to pass the sequential container to the constructor. Normally sequential containers work in a chain manner that is the input of one container we pass to the next container as per our requirement and finally it returns the output of the last modules.
What is PyTorch sequential?
In the sequential container, modules will be added to it in the request they be passed in the constructor. Then again, an OrderedDict of modules can be passed in. The forward() strategy for Sequential acknowledges any info and advances it to the principal module it contains. It then, at that point “chains” yields to inputs successively for each resulting module, at last returning the yield of the last module.
The worth a Sequential give over physically calling a grouping of modules is that it permits regarding the entire holder as a solitary module, with the end goal that playing out a change on the Sequential applies to each of the modules it stores (which are each an enlisted submodule of the Sequential).
What’s the contrast between a Sequential and a torch.nn.ModuleList? A ModuleList is actually what it seems like a rundown for putting away Modules! Then again, the layers in a Sequential are associated in a falling way.
How to use PyTorch sequential?
Now let’s see how we can use the sequential container as follows. For the implementation of the sequential class, we need to use different steps as follows.
First, we need to import the required packages that we want. After that, we need to create the dataset as per our requirements. After the creation of the dataset, we need to create the Model. Now, you might be pondering with regards to tasks like actuation works and pooling activities, and leveling activities. The majority of the tasks are in the neural organization practical API. Interestingly, PyTorch has wrapped itself within a neural organization module itself.
The successive class develops the forward strategy certainly by consecutively constructing network design. PyTorch successive model is a holder class or otherwise called a covering class that permits us to create the neural organization models. We can form any neural organization model together utilizing the Sequential model. This implies that we create layers to create organizations and we can even make different organizations together.
import torch. nn as nn
import torch.nn.functional as Fun
torch.nn.functional as fun permits us to make consecutive models and by being able to characterize our layers, our actuation capacities, our smooth tasks are pulling activities. The principal focal point here is that having these capacities wrapped up as neural organization models permit us to utilize the consecutive class to wrap different modules and create them together. We can form convolutions with max-pooling tasks with ReLU enactment capacities with straightened activities and what this does is permit us to assemble our models in consecutive order.
PyTorch sequential Model
Now let’s see how we can create the sequential Model as follows.
A sequential module is a compartment or covering class that expands the nn. Module base class and permits us to form modules together. We can make any nn.Module within some other nn.Module.
The sequential module is the AI model that info or yield groupings of information. Successive information incorporates text transfers, sound bites, video cuts, time-series information, and so on Intermittent Neural Networks (RNNs) are a famous calculation utilized in grouping models. The Sequential holder is utilized to chain a succession of PyTorch modules, for example, layers of a Neural Network (NN), into a succession (a rundown). The Sequential holder then advances changes to the whole succession of modules without composing extra code. For instance, to be applied on every module in Sequential, and a call to advance will be consequently anchored.
The sequential module is the AI model that informs or yields arrangements of information. Sequential information incorporates text transfers, sound bites, video cuts, time-series information and so forth Intermittent Neural Networks (RNNs) are a famous calculation utilized in succession models.
Creating the sequential network
Now let’s see how we can create the sequential network in PyTorch with an example as follows.
Example #1
Code:
import torch.nn as nn
input_layer = []
input_layer.append(nn.Linear(4, 2))
input_layer.append(nn.Sigmoid())
input_layer.append(nn.Linear(5, 3))
input_layer.append(nn.Sigmoid())
result = nn.Sequential(*input_layer)
print(result)
Explanation
In the above example, we try to implement the PyTorch sequential model, here first we import the torch as shown. After that, we take the input from the different sequential models in a chain manner with the append method and inside the append method, we pass the different arguments as shown. The final output of the above program we illustrated by using the following screenshot as follows.
Now let’s see another example as follows.
Example #2
Code:
import torch.nn as nn
input_layer = []
input_layer.append(nn.Linear(6, 1))
input_layer.append(nn.Sigmoid())
input_layer.append(nn.Linear(3, 5))
input_layer.append(nn.Sigmoid())
result = nn.Sequential(*input_layer)
print(result)
Explanation
In the above example, we try to implement the PyTorch sequential model, here first we import the torch as shown. After that, we take the input from the different sequential models in a chain manner with the append method and inside the append method, we pass the different arguments as shown. The final output of the above program we illustrated by using the following screenshot as follows.
In the above example, we implement the sequential model in a simple way as shown, but we can also implement this model by using a training dataset as per our requirement.
As per requirement, we can include the loss function into the sequential mode to find out the difference between predicted outcomes and actual outcomes.
Conclusion
We hope from this article you learn more about the PyTorch sequential. From the above article, we have taken in the essential idea of the PyTorch sequential and we also see the representation and example of PyTorch sequential. From this article, we learned how and when we sequential PyTorch.
Recommended Articles
We hope that this EDUCBA information on “PyTorch sequential” was beneficial to you. You can view EDUCBA’s recommended articles for more information.