You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/utils.md
+26-2Lines changed: 26 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,16 +8,40 @@ title: Utilities
8
8
9
9
`param_groups_weight_decay` is adapted from [timm's optimizer factory methods](https://huggingface.co/docs/timm/reference/optimizers#timm.optim.create_optimizer).
10
10
11
-
### Example
11
+
### Examples
12
12
13
13
`param_groups_weight_decay` takes a model and returns two optimizer parameter group dictionaries. One with bias and normalization terms without weight decay and another dictionary with the rest of the model parameters with weight decay. The `weight_decay` passed to `param_groups_weight_decay` will override the optimizer's default weight decay.
`additional_layers` parameter allows you to specify additional layer names or name substrings that should be excluded from weight decay. This is useful for excluding specific layers like token embeddings which also benefit from not having weight decay applied.
22
+
23
+
The parameter accepts an iterable of strings, where each string is matched as a substring against the full parameter name (as returned by `model.named_parameters()`).
24
+
25
+
```python
26
+
classMiniLM(nn.Module):
27
+
def__init__(self):
28
+
super().__init__()
29
+
self.tok_embeddings = nn.Embedding(1000, 20)
30
+
self.pos_embeddings = nn.Embedding(100, 20)
31
+
self.norm = nn.LayerNorm(20)
32
+
self.layer1 = nn.Linear(20, 30)
33
+
self.layer2 = nn.Linear(30, 1000)
34
+
35
+
model = MiniLM()
36
+
37
+
# Exclude token embeddings from weight decay in addition to bias and normalization layers
0 commit comments