Layer normalized with optional bias. This is based on PyTorch's :class:`~torch.nn.LayerNorm` module but is needed because PyTorch's version does not support disabling the bias. :param shape: shape of the input, following an arbitrary number of batch dimensions; that is, the inp
| 247 | |
| 248 | |
| 249 | class LayerNorm(nn.Module): |
| 250 | """Layer normalized with optional bias. |
| 251 | |
| 252 | This is based on PyTorch's :class:`~torch.nn.LayerNorm` module but is needed because |
| 253 | PyTorch's version does not support disabling the bias. |
| 254 | |
| 255 | :param shape: shape of the input, following an arbitrary number of batch dimensions; |
| 256 | that is, the input has dimensions `[d1, ..., dk, shape[0], ..., shape[-1]]` |
| 257 | :param eps: value added to the denominator for numerical stability |
| 258 | :param bias: whether to include a bias term |
| 259 | :param dtype: data type to use for the parameters |
| 260 | """ |
| 261 | |
| 262 | normalized_shape: Tuple[int, ...] |
| 263 | eps: float |
| 264 | |
| 265 | def __init__( |
| 266 | self, |
| 267 | shape: Union[int, Tuple[int, ...], torch.Size], |
| 268 | eps: float = 1e-5, |
| 269 | bias: bool = True, |
| 270 | dtype=None, |
| 271 | ): |
| 272 | super().__init__() |
| 273 | |
| 274 | self.eps = eps |
| 275 | if isinstance(shape, numbers.Integral): |
| 276 | self.normalized_shape = (shape,) |
| 277 | else: |
| 278 | self.normalized_shape = tuple(shape) |
| 279 | |
| 280 | self.weight = nn.Parameter(torch.empty(shape)) |
| 281 | self.bias = nn.Parameter(torch.empty(shape)) if bias else None |
| 282 | |
| 283 | self.reset_parameters() |
| 284 | |
| 285 | def reset_parameters(self): |
| 286 | torch.nn.init.ones_(self.weight) |
| 287 | if self.bias is not None: |
| 288 | torch.nn.init.zeros_(self.bias) |
| 289 | |
| 290 | def forward(self, input): |
| 291 | return F.layer_norm( |
| 292 | input, self.normalized_shape, self.weight, self.bias, self.eps |
| 293 | ) |
| 294 | |
| 295 | |
| 296 | class TiedLinear(nn.Module): |