giagrad.nn.Module#

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

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:

import giagrad.nn as nn

class Model(nn.Module):
    def __init__(self):
        super().__init__()
        self.l1 = nn.Linear(28*28, 100)
        self.l2 = nn.Linear(100, 10)

    def __call__(self, x):
        x = self.l1(x).relu()
        return self.l2(x).relu()

Submodules assigned in this way will be registered thanks to Model constructor in __odict__ variable in the same order as they are defined.

>>> m = Model()
>>> m.__odict__
OrderedDict([('l1', Layer(in=784, out=100, bias=True)),
             ('l2', Layer(in=100, out=10, bias=True))])

Note

For the example above, an __init__() call to the parent class must be made before assignment on the child if you want to have it in __odict__.

Variables:

training (bool, default: True) – Represents whether this module is in training or evaluation mode.

Methods

add_module

Adds a child module to the current module.

apply

Applies fn recursively to every submodule as well as self.

eval

Sets all submodules in evaluation mode including self.

parameters

Returns an iterator over all submodules and self parameters.

train

Sets all submodules in training mode including self.

zero_grad

Sets gradients of all tensors returned by parameters to zero.