giagrad.nn.Sequential#

class giagrad.nn.Sequential(*args, **kwargs)[source]#

A sequential container.

Based on PyTorch’s Sequential documentation.

Modules will be added to it in the order they are passed in the constructor. Alternatively, an OrderedDict of modules can be passed in. The forward() method of Sequential accepts any input 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.

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 code
model = nn.Sequential(OrderedDict([
          ('conv1', nn.Conv2D(1,20,5)),
          ('relu1', nn.ReLU()),
          ('conv2', nn.Conv2D(20,64,5)),
          ('relu2', nn.ReLU())
        ]))

Inherits from: Module.

Sequentials can be added together or in-place, and iterated too.

>>> model = nn.Sequential(
            nn.Linear(500),
            nn.ReLU(),
            nn.Linear(10)
        )
>>> model += model
>>> for key, subModule in model:
...     print(key, subModule)
module0 Layer(out_features=500, bias=True)
module1 ReLU
module2 Layer(out_features=10, bias=True)
module3 Layer(out_features=500, bias=True)
module4 ReLU
module5 Layer(out_features=10, bias=True)

Methods

append

Appends a new module to self.