MCPcopy Create free account
hub / github.com/Oneflow-Inc/oneflow / named_modules

Method named_modules

python/oneflow/nn/modules/module.py:714–756  ·  view source on GitHub ↗

r""" named_modules(memo=None, prefix="") Returns an iterator over all modules in the network, yielding both the name of the module as well as the module itself. Args: memo: a memo to store the set of modules already added to the result

(self, memo: Optional[Set["Module"]] = None, prefix: str = "")

Source from the content-addressed store, hash-verified

712 yield module
713
714 def named_modules(self, memo: Optional[Set["Module"]] = None, prefix: str = ""):
715 r"""
716 named_modules(memo=None, prefix="")
717
718 Returns an iterator over all modules in the network, yielding
719 both the name of the module as well as the module itself.
720
721 Args:
722 memo: a memo to store the set of modules already added to the result
723 prefix: a prefix that will be added to the name of the module
724
725 Yields:
726 (string, Module): Tuple of name and module
727
728 Note:
729 Duplicate modules are returned only once. In the following
730 example, ``l`` will be returned only once.
731
732 Example::
733
734 >>> import oneflow.nn as nn
735 >>> l = nn.Linear(2, 2)
736 >>> net = nn.Sequential(l, l)
737 >>> for idx, m in enumerate(net.named_modules()):
738 ... print(idx, '->', m)
739 0 -> ('', Sequential(
740 (0): Linear(in_features=2, out_features=2, bias=True)
741 (1): Linear(in_features=2, out_features=2, bias=True)
742 ))
743 1 -> ('0', Linear(in_features=2, out_features=2, bias=True))
744
745 """
746 if memo is None:
747 memo = set()
748 if self not in memo:
749 memo.add(self)
750 yield (prefix, self)
751 for (name, module) in self._modules.items():
752 if module is None:
753 continue
754 submodule_prefix = prefix + ("." if prefix else "") + name
755 for m in module.named_modules(memo, submodule_prefix):
756 yield m
757
758 def train(self: T, mode: bool = True) -> T:
759 r"""

Callers 7

_named_membersMethod · 0.95
modulesMethod · 0.95
to_of_transformFunction · 0.80
state_dict_hookMethod · 0.80
_fx_fuseFunction · 0.80
is_prunedFunction · 0.80

Calls 2

addMethod · 0.45
itemsMethod · 0.45

Tested by 2

state_dict_hookMethod · 0.64
_fx_fuseFunction · 0.64