Created
January 8, 2026 02:37
-
-
Save cjams/fa490c245bc00bb239970ecfd6737591 to your computer and use it in GitHub Desktop.
LayerNorm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class LayerNorm(): | |
| def __init__(self, device, num_features): | |
| self.out = None | |
| self.gamma = torch.ones(num_features, device=device) | |
| self.bias = torch.zeros(num_features, device=device) | |
| def __call__(self, x: torch.Tensor): | |
| assert x.ndim == 2 | |
| H = x.shape[1] | |
| avg = x.mean(dim=1, keepdim=True) | |
| std = torch.sqrt(1 / H * ((x - avg)**2).sum(dim=1, keepdim=True)) | |
| self.out = (x - avg) / std * self.gamma + self.bias | |
| return self.out | |
| def params(self): | |
| return [self.gamma, self.bias] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment