Skip to content

Module

toydl.core.module.Module

Module()

Modules form a tree that stores parameters and other submodules. They make up the basis of neural network stacks.

Source code in toydl/core/module.py
47
48
49
50
def __init__(self):
    self._modules: dict[str, "Module"] = {}
    self._parameters: dict[str, Parameter] = {}
    self.training = True

add_parameter

add_parameter(k: str, v: Scalar)

Manually add a parameter. Useful helper for scalar parameters.

Parameters:

Name Type Description Default
k str

Local name of the parameter.

required
v Scalar

Value for the parameter.

required

Returns:

Type Description

Newly created parameter.

Source code in toydl/core/module.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def add_parameter(self, k: str, v: Scalar):
    """
    Manually add a parameter. Useful helper for scalar parameters.

    :param k: Local name of the parameter.
    :param v: Value for the parameter.
    :return parameter: Newly created parameter.
    """
    val = Parameter(v, k)
    self.__dict__["_parameters"][k] = val
    return val

eval

eval()

Set the mode of this module and all descendant modules to eval.

Source code in toydl/core/module.py
62
63
64
65
66
def eval(self):
    """Set the mode of this module and all descendant modules to `eval`."""
    self.training = False
    for m in self.modules():
        m.training = False

modules

modules()

Return the direct child modules of this module.

Source code in toydl/core/module.py
52
53
54
def modules(self):
    """Return the direct child modules of this module."""
    return self.__dict__["_modules"].values()

named_parameters

named_parameters() -> list[tuple[str, Parameter]]

Collect all the parameters of this module and its descendants.

Returns:

Type Description
list[tuple[str, Parameter]]

Contains the name and :class:Parameter of each ancestor parameter.

Source code in toydl/core/module.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def named_parameters(self) -> list[tuple[str, Parameter]]:
    """
    Collect all the parameters of this module and its descendants.


    :return list of pairs: Contains the name and :class:`Parameter` of each ancestor parameter.
    """
    named_params: list[tuple[str, Parameter]] = []
    # the module params
    for name, param in self._parameters.items():
        named_params.append((name, param))
    # descendants params
    for module_name, module in self._modules.items():
        for param_name, param in module.named_parameters():
            named_params.append((f"{module_name}.{param_name}", param))
    return named_params

parameters

parameters()

Enumerate over all the parameters of this module and its descendants.

Source code in toydl/core/module.py
85
86
87
88
def parameters(self):
    """Enumerate over all the parameters of this module and its descendants."""
    params = [param for _, param in self.named_parameters()]
    return params

train

train()

Set the mode of this module and all descendant modules to train.

Source code in toydl/core/module.py
56
57
58
59
60
def train(self):
    """Set the mode of this module and all descendant modules to `train`."""
    self.training = True
    for m in self.modules():
        m.training = True

toydl.core.module.Parameter

Parameter(value: Scalar, name: str | None = None)

A Parameter is a special container stored in a :class:Module.

It is designed to hold a :class:Variable, but we allow it to hold any value for testing.

Parameters:

Name Type Description Default
value Scalar

the value of parameter

required
name str | None

the name of parameter

None
Source code in toydl/core/module.py
14
15
16
17
18
19
20
21
22
23
24
def __init__(self, value: Scalar, name: str | None = None):
    """
    :param value: the value of parameter
    :param name: the name of parameter
    """
    self.value = value
    self.name = name
    # Set `requires_grad_` to True, indicating that these parameters are trainable (can accumulate gradients)
    self.value.requires_grad_(True)
    if self.name:
        self.value.name = self.name

update

update(x: Scalar) -> None

Update the parameter value.

Parameters:

Name Type Description Default
x Scalar

the parameter's new value

required
Source code in toydl/core/module.py
26
27
28
29
30
31
def update(self, x: Scalar) -> None:
    r"""Update the parameter value.

    :param x: the parameter's new value
    """
    self.value.data = x.data