Rate this Page

Sequential#

classtorch.nn.modules.container.Sequential(*args:Module)[source]#
classtorch.nn.modules.container.Sequential(arg:OrderedDict[str,Module])

A sequential container.

Modules will be added to it in the order they are passed in theconstructor. Alternatively, anOrderedDict of modules can bepassed in. Theforward() method ofSequential accepts anyinput and forwards it to the first module it contains. It then“chains” outputs to inputs sequentially for each subsequent module,finally returning the output of the last module.

The value aSequential provides over manually calling a sequenceof modules is that it allows treating the whole container as asingle module, such that performing a transformation on theSequential applies to each of the modules it stores (which areeach a registered submodule of theSequential).

What’s the difference between aSequential and atorch.nn.ModuleList? AModuleList is exactly what itsounds like–a list for storingModule s! On the other hand,the layers in aSequential are connected in a cascading way.

Example:

# Using Sequential to create a small model. When `model` is run,# input will first be passed to `Conv2d(1,20,5)`. The output of# `Conv2d(1,20,5)` will be used as the input to the first# `ReLU`; the output of the first `ReLU` will become the input# for `Conv2d(20,64,5)`. Finally, the output of# `Conv2d(20,64,5)` will be used as input to the second `ReLU`model=nn.Sequential(nn.Conv2d(1,20,5),nn.ReLU(),nn.Conv2d(20,64,5),nn.ReLU())# Using Sequential with OrderedDict. This is functionally the# same as the above codemodel=nn.Sequential(OrderedDict([("conv1",nn.Conv2d(1,20,5)),("relu1",nn.ReLU()),("conv2",nn.Conv2d(20,64,5)),("relu2",nn.ReLU()),]))
append(module)[source]#

Append a given module to the end.

Parameters

module (nn.Module) – module to append

Return type

Self

Example:

>>>importtorch.nnasnn>>>n=nn.Sequential(nn.Linear(1,2),nn.Linear(2,3))>>>n.append(nn.Linear(3,4))Sequential(    (0): Linear(in_features=1, out_features=2, bias=True)    (1): Linear(in_features=2, out_features=3, bias=True)    (2): Linear(in_features=3, out_features=4, bias=True))
extend(sequential)[source]#

Extends the current Sequential container with layers from another Sequential container.

Parameters

sequential (Sequential) – A Sequential container whose layers will be added to the current container.

Return type

Self

Example:

>>>importtorch.nnasnn>>>n=nn.Sequential(nn.Linear(1,2),nn.Linear(2,3))>>>other=nn.Sequential(nn.Linear(3,4),nn.Linear(4,5))>>>n.extend(other)# or `n + other`Sequential(    (0): Linear(in_features=1, out_features=2, bias=True)    (1): Linear(in_features=2, out_features=3, bias=True)    (2): Linear(in_features=3, out_features=4, bias=True)    (3): Linear(in_features=4, out_features=5, bias=True))
forward(input)[source]#

Runs the forward pass.

insert(index,module)[source]#

Inserts a module into the Sequential container at the specified index.

Parameters
  • index (int) – The index to insert the module.

  • module (Module) – The module to be inserted.

Return type

Self

Example:

>>>importtorch.nnasnn>>>n=nn.Sequential(nn.Linear(1,2),nn.Linear(2,3))>>>n.insert(0,nn.Linear(3,4))Sequential(    (0): Linear(in_features=3, out_features=4, bias=True)    (1): Linear(in_features=1, out_features=2, bias=True)    (2): Linear(in_features=2, out_features=3, bias=True))
pop(key)[source]#

Popkey from self.

Return type

Module