look up `name` in `mod` and replace the layer with `new_layer`, return the updated `mod`. Args: mod: a pytorch module to be updated. name: a string representing the target module attribute. new_layer: a new module replacing the corresponding layer at ``mod.name``.
(mod, name: str, new_layer)
| 145 | |
| 146 | |
| 147 | def set_named_module(mod, name: str, new_layer): |
| 148 | """ |
| 149 | look up `name` in `mod` and replace the layer with `new_layer`, return the updated `mod`. |
| 150 | |
| 151 | Args: |
| 152 | mod: a pytorch module to be updated. |
| 153 | name: a string representing the target module attribute. |
| 154 | new_layer: a new module replacing the corresponding layer at ``mod.name``. |
| 155 | |
| 156 | Returns: |
| 157 | an updated ``mod`` |
| 158 | |
| 159 | See also: :py:func:`monai.networks.utils.look_up_named_module`. |
| 160 | """ |
| 161 | mods_attr = name.rsplit(".", 1) |
| 162 | submods, attr = mods_attr if len(mods_attr) == 2 else ("", name) |
| 163 | if not attr: |
| 164 | return new_layer |
| 165 | _mod = look_up_named_module(submods, mod) |
| 166 | setattr(_mod, attr, new_layer) |
| 167 | return mod |
| 168 | |
| 169 | |
| 170 | def one_hot(labels: torch.Tensor, num_classes: int, dtype: torch.dtype = torch.float, dim: int = 1) -> torch.Tensor: |
searching dependent graphs…