1 parent d01391b commit 5eea426Copy full SHA for 5eea426
1 file changed
model2vec/train/classifier.py
@@ -80,10 +80,16 @@ def construct_head(self) -> nn.Sequential:
80
# We always have a layer mapping from hidden to out.
81
modules.append(nn.Linear(self.hidden_dim, self.out_dim))
82
83
- for module in modules:
84
- if isinstance(module, nn.Linear):
85
- nn.init.kaiming_uniform_(module.weight)
86
- nn.init.zeros_(module.bias)
+ linear_modules = [module for module in modules if isinstance(module, nn.Linear)]
+ if linear_modules:
+ *initial, last = linear_modules
+ for module in initial:
87
+ if isinstance(module, nn.Linear):
88
+ nn.init.kaiming_uniform_(module.weight, nonlinearity="relu")
89
+ nn.init.zeros_(module.bias)
90
+ # Final layer does not kaiming
91
+ nn.init.xavier_uniform_(last.weight)
92
+ nn.init.zeros_(last.bias)
93
94
return nn.Sequential(*modules)
95
0 commit comments