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

Function generate_param_groups

monai/optimizers/utils.py:23–103  ·  view source on GitHub ↗

Utility function to generate parameter groups with different LR values for optimizer. The output parameter groups have the same order as `layer_match` functions. Args: network: source network to generate parameter groups from. layer_matches: a list of callable functions

(
    network: torch.nn.Module,
    layer_matches: Sequence[Callable],
    match_types: Sequence[str],
    lr_values: Sequence[float],
    include_others: bool = True,
)

Source from the content-addressed store, hash-verified

21
22
23def generate_param_groups(
24 network: torch.nn.Module,
25 layer_matches: Sequence[Callable],
26 match_types: Sequence[str],
27 lr_values: Sequence[float],
28 include_others: bool = True,
29) -> list[dict]:
30 """
31 Utility function to generate parameter groups with different LR values for optimizer.
32 The output parameter groups have the same order as `layer_match` functions.
33
34 Args:
35 network: source network to generate parameter groups from.
36 layer_matches: a list of callable functions to select or filter out network layer groups,
37 for "select" type, the input will be the `network`, for "filter" type,
38 the input will be every item of `network.named_parameters()`.
39 for "select", the parameters will be
40 `select_func(network).parameters()`.
41 for "filter", the parameters will be
42 `(x[1] for x in filter(f, network.named_parameters()))`
43 match_types: a list of tags to identify the matching type corresponding to the `layer_matches` functions,
44 can be "select" or "filter".
45 lr_values: a list of LR values corresponding to the `layer_matches` functions.
46 include_others: whether to include the rest layers as the last group, default to True.
47
48 It's mainly used to set different LR values for different network elements, for example:
49
50 .. code-block:: python
51
52 net = Unet(spatial_dims=3, in_channels=1, out_channels=3, channels=[2, 2, 2], strides=[1, 1, 1])
53 print(net) # print out network components to select expected items
54 print(net.named_parameters()) # print out all the named parameters to filter out expected items
55 params = generate_param_groups(
56 network=net,
57 layer_matches=[lambda x: x.model[0], lambda x: "2.0.conv" in x[0]],
58 match_types=["select", "filter"],
59 lr_values=[1e-2, 1e-3],
60 )
61 # the groups will be a list of dictionaries:
62 # [{'params': <generator object Module.parameters at 0x7f9090a70bf8>, 'lr': 0.01},
63 # {'params': <filter object at 0x7f9088fd0dd8>, 'lr': 0.001},
64 # {'params': <filter object at 0x7f9088fd0da0>}]
65 optimizer = torch.optim.Adam(params, 1e-4)
66
67 """
68 layer_matches = ensure_tuple(layer_matches)
69 match_types = ensure_tuple_rep(match_types, len(layer_matches))
70 lr_values = ensure_tuple_rep(lr_values, len(layer_matches))
71
72 def _get_select(f):
73
74 def _select():
75 return f(network).parameters()
76
77 return _select
78
79 def _get_filter(f):
80

Callers 2

test_lr_valuesMethod · 0.90
test_wrongMethod · 0.90

Calls 6

ensure_tupleFunction · 0.90
ensure_tuple_repFunction · 0.90
_get_selectFunction · 0.85
_get_filterFunction · 0.85
extendMethod · 0.80
appendMethod · 0.45

Tested by 2

test_lr_valuesMethod · 0.72
test_wrongMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…