Skip to content

Module

toydl.core.module.Module

Module()

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

Source code in toydl/core/module.py
13
14
15
16
def __init__(self):
    self._modules = {}
    self._parameters = {}
    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
56
57
58
59
60
61
62
63
64
65
66
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
28
29
30
31
32
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
18
19
20
def modules(self):
    """Return the direct child modules of this module."""
    return self.__dict__["_modules"].values()

named_parameters

named_parameters()

Collect all the parameters of this module and its descendants.

Returns:

Type Description

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

Source code in toydl/core/module.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def named_parameters(self):
    """
    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 = []
    # 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
51
52
53
54
def parameters(self):
    """Enumerate over all the parameters of this module and its descendants."""
    params = [param for name, 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
22
23
24
25
26
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: Optional[str] = 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 Optional[str]

the name of parameter

None
Source code in toydl/core/module.py
 98
 99
100
101
102
103
104
105
106
107
108
def __init__(self, value: Scalar, name: Optional[str] = None):
    """
    :param value: the value of parameter
    :param name: the name of parameter
    """
    self.value = value
    self.name = name
    # 这里设置`requires_grad_`为True可以将当前参数的值,即对应的Scalar instance
    self.value.requires_grad_(True)
    if self.name:
        self.value.name = self.name

update

update(x: Any)

Update the parameter value.

Parameters:

Name Type Description Default
x Any

the parameter's new value

required
Source code in toydl/core/module.py
110
111
112
113
114
115
def update(self, x: Any):
    r"""Update the parameter value.

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