pytorch Official website torch.nn.Sequential Is described as follows .
Usage mode :
# Writing a
net = nn.Sequential(
nn.Linear(num_inputs, 1)
# You can also pass in other layers here
)
# Write two
net = nn.Sequential()
net.add_module('linear', nn.Linear(num_inputs, 1))
# net.add_module ......
# Write three
from collections import OrderedDict
net = nn.Sequential(OrderedDict([
('linear', nn.Linear(num_inputs, 1))
# ......
]))
Mode one :
This is a container in sequence , The specific neural network modules are added to the calculation diagram in the order of the incoming constructors to execute .
Mode two :
We can also use the ordered dictionary with the specific neural network module as the element (OrderedDict) Pass in for parameter .
Mode three :
You can also use add_module Function to insert a specific neural network module into the calculation diagram .add_module Function is the basic class of neural network module (torch.nn.Module) The method in , The following description is used to add submodules to existing modules .
Let's take a look at the initialization function __init__, In the initialization function , First of all if conditional , If the parameter passed in is 1 individual , And the type is OrderedDict, Use by dictionary index add_module Function to add a submodule to an existing module , otherwise , adopt for Loop traversal parameters , Add all submodules to the existing .
Because every neural network module is inherited from nn.Module, So it will all come true __call__
And forward
function , therefore forward Function through for The loop in turn calls the submodules added to the existing modules , Finally, the output of all the neural network layer results .
reference :
https://blog.csdn.net/dss_dssssd/article/details/82980222