Module
Base class for neural-network modules.
Subclass it, register children/parameters by attribute assignment in
__init__ (after super().__init__()), and define forward.
__init__
__init__(self) -> 'None'
Initialize self. See help(type(self)) for accurate signature.
add_module
add_module(self, name: 'str', module: "'Module'") -> 'None'
File module as a child (what Module assignment does).
apply
apply(self, fn: "Callable[['Module'], None]") -> "'Module'"
Run fn over every module in the tree (children first).
buffers
buffers(self, recurse: 'bool' = True) -> 'Iterator[Tensor]'
children
children(self) -> "Iterator['Module']"
eval
eval(self) -> "'Module'"
extra_repr
extra_repr(self) -> 'str'
One line of per-class detail for :meth:__repr__ (a layer
prints its geometry here).
forward
forward(self, *args: 'Any', **kwargs: 'Any') -> 'Any'
Subclasses define the computation here; call the module itself
(m(x)), not forward directly.
initialize
initialize(self) -> "'Module'"
Warm the whole tree up eagerly (pack bound weights now instead of on the first forward).
load_state_dict
load_state_dict(self, state: 'dict[str, Tensor]', strict: 'bool' = True) -> 'LoadResult'
Bind a flat dict[str, Tensor] checkpoint onto the tree by
dotted names. strict=True raises when keys are missing or
unexpected; either way the accounting is returned.
modules
modules(self) -> "Iterator['Module']"
named_buffers
named_buffers(self, recurse: 'bool' = True) -> 'Iterator[tuple[str, Tensor]]'
named_children
named_children(self) -> "Iterator[tuple[str, 'Module']]"
named_modules
named_modules(self, prefix: 'str' = '', _memo: 'set[int] | None' = None) -> "Iterator[tuple[str, 'Module']]"
(dotted name, module) pairs over the whole tree, self first. A module reachable twice is yielded once (first path wins).
named_parameters
named_parameters(self, recurse: 'bool' = True) -> 'Iterator[tuple[str, Tensor]]'
(dotted name, tensor) pairs: loose parameters and the wrapped
layers' tensors alike. A :class:Parameter shared between two
attributes yields once (first name wins).
parameters
parameters(self, recurse: 'bool' = True) -> 'Iterator[Tensor]'
register_buffer
register_buffer(self, name: 'str', tensor: 'Tensor', persistent: 'bool' = True) -> 'None'
File tensor as a buffer: module state that is not a learned
parameter (a mask, a precomputed table). Persistent buffers appear
in :meth:state_dict; persistent=False keeps one out.
register_parameter
register_parameter(self, name: 'str', param: 'Parameter') -> 'None'
File param under name (what Parameter assignment does).
state_dict
state_dict(self, *, prefix: 'str' = '') -> 'dict[str, Tensor]'
The module tree's named tensors as one flat dict[str, Tensor]
with dotted keys (persistent buffers included).
The export reflects what the tree currently HOLDS: once a layer has
packed its weights for serving, the packed entries no longer appear
here. Loading is the round-trip contract: build the module, then
:meth:load_state_dict a checkpoint into it.
to
to(self, target: 'ToTarget') -> "'Module'"
Move the tree to a device (Device or a string like
"cpu") or cast it to a dtype (DataType). Refusals raise;
they are never swallowed.
train
train(self, mode: 'bool' = True) -> "'Module'"
Flip the tree's training flag (modules may branch on it).