Deletes the given submodule from ``self``. The module will not be deleted if ``target`` is not a valid target. Args: target: The fully-qualified string name of the new submodule (See example in ``nn.Module.get_submodule`` for how to
(self, target: str)
| 594 | |
| 595 | @compatibility(is_backward_compatible=True) |
| 596 | def delete_submodule(self, target: str) -> bool: |
| 597 | """ |
| 598 | Deletes the given submodule from ``self``. |
| 599 | |
| 600 | The module will not be deleted if ``target`` is not a valid |
| 601 | target. |
| 602 | |
| 603 | Args: |
| 604 | target: The fully-qualified string name of the new submodule |
| 605 | (See example in ``nn.Module.get_submodule`` for how to |
| 606 | specify a fully-qualified string.) |
| 607 | |
| 608 | Returns: |
| 609 | bool: Whether or not the target string referenced a |
| 610 | submodule we want to delete. A return value of ``False`` |
| 611 | means that the ``target`` was not a valid reference to |
| 612 | a submodule. |
| 613 | """ |
| 614 | atoms = target.split(".") |
| 615 | path, target_submod = atoms[:-1], atoms[-1] |
| 616 | mod: torch.nn.Module = self |
| 617 | |
| 618 | # Get the parent module |
| 619 | for item in path: |
| 620 | |
| 621 | if not hasattr(mod, item): |
| 622 | return False |
| 623 | |
| 624 | mod = getattr(mod, item) |
| 625 | |
| 626 | if not isinstance(mod, torch.nn.Module): |
| 627 | return False |
| 628 | |
| 629 | if not hasattr(mod, target_submod): |
| 630 | return False |
| 631 | |
| 632 | if not isinstance(getattr(mod, target_submod), torch.nn.Module): |
| 633 | return False |
| 634 | |
| 635 | delattr(mod, target_submod) |
| 636 | return True |
| 637 | |
| 638 | @compatibility(is_backward_compatible=True) |
| 639 | def delete_all_unused_submodules(self) -> None: |