MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / set_named_module

Function set_named_module

monai/networks/utils.py:147–167  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

145
146
147def 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
170def one_hot(labels: torch.Tensor, num_classes: int, dtype: torch.dtype = torch.float, dim: int = 1) -> torch.Tensor:

Callers 2

__init__Method · 0.90
test_get_moduleMethod · 0.90

Calls 1

look_up_named_moduleFunction · 0.85

Tested by 1

test_get_moduleMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…