r"""Return an iterator over all modules in the network. Yields: Module: a module in the network Note: Duplicate modules are returned only once. In the following example, ``l`` will be returned only once. Example:: >>> l = nn
(self)
| 2304 | yield name, module |
| 2305 | |
| 2306 | def modules(self) -> Iterator['Module']: |
| 2307 | r"""Return an iterator over all modules in the network. |
| 2308 | |
| 2309 | Yields: |
| 2310 | Module: a module in the network |
| 2311 | |
| 2312 | Note: |
| 2313 | Duplicate modules are returned only once. In the following |
| 2314 | example, ``l`` will be returned only once. |
| 2315 | |
| 2316 | Example:: |
| 2317 | |
| 2318 | >>> l = nn.Linear(2, 2) |
| 2319 | >>> net = nn.Sequential(l, l) |
| 2320 | >>> for idx, m in enumerate(net.modules()): |
| 2321 | ... print(idx, '->', m) |
| 2322 | |
| 2323 | 0 -> Sequential( |
| 2324 | (0): Linear(in_features=2, out_features=2, bias=True) |
| 2325 | (1): Linear(in_features=2, out_features=2, bias=True) |
| 2326 | ) |
| 2327 | 1 -> Linear(in_features=2, out_features=2, bias=True) |
| 2328 | |
| 2329 | """ |
| 2330 | for _, module in self.named_modules(): |
| 2331 | yield module |
| 2332 | |
| 2333 | def named_modules(self, memo: Optional[Set['Module']] = None, prefix: str = '', remove_duplicate: bool = True): |
| 2334 | r"""Return an iterator over all modules in the network, yielding both the name of the module as well as the module itself. |