Skip to the content.

Detailed Specification for Module Methods: Container

In the mini-torch framework, a container like Sequential is a specialized subclass of Module that implements the Composite Design Pattern. It holds a list of other Module instances (like Linear layers or activation functions) and chains them together. Because it inherits from Module, it shares the exact same interface as a single layer.

Unlike a single layer, the Sequential container does not define its own mathematical weights or perform its own gradient math. Instead, it delegates these responsibilities by iterating through its child modules.

Here are the detailed specifications for the Sequential container methods:

__init__()

The constructor is responsible for receiving and storing the child layers that make up the sequence.

Method Signature

def __init__(self, modules):

Specification

Example Implementation

def __init__(self, modules):
    self.modules = modules

forward()

The forward method passes the input data sequentially through every layer in the container.

Method Signature

def forward(self, x):

Specification

Example Implementation

def forward(self, x):
    for module in self.modules:
        x = module.forward(x)
    return x

backward()

The backward method implements the chain rule of calculus by propagating the error gradients backwards through the sequence of layers.

Method Signature

def backward(self, grad_output):

Specification

Example Implementation

def backward(self, grad_output):
    # Iterate through layers from last to first
    for module in reversed(self.modules):
        grad_output = module.backward(grad_output)
    return grad_output

parameters() and grads()

To allow the Optimizer to update the entire network seamlessly, the container must aggregate the state of all its child modules into flat lists.

Method Signatures

def parameters(self):
def grads(self):

Specification

Example Implementation

def parameters(self):
    params = []
    for module in self.modules:
        params.extend(module.parameters())
    return params

def grads(self):
    gradients = []
    for module in self.modules:
        gradients.extend(module.grads())
    return gradients