Initialize the weights of a module based on the depth of the model.
(module: nn.Module, depth: int)
| 339 | |
| 340 | |
| 341 | def _init_by_depth(module: nn.Module, depth: int) -> None: |
| 342 | """Initialize the weights of a module based on the depth of the model.""" |
| 343 | if isinstance(module, nn.Linear): |
| 344 | fan_in = module.weight.size(-1) |
| 345 | std = 1 / math.sqrt(2 * fan_in * depth) |
| 346 | nn.init.trunc_normal_(module.weight, mean=0.0, std=std, a=-3 * std, b=3 * std) |
| 347 | if module.bias is not None: |
| 348 | nn.init.zeros_(module.bias) |
no outgoing calls
no test coverage detected