Spaces:
Sleeping
Sleeping
Download build/lib/src/data/normalization.py from ServandoTorres/Relu: direct link, hf CLI and curl.
- Browser
- Download file 796 Bytes
-
https://huggingface.co/spaces/ServandoTorres/Relu/resolve/main/build/lib/src/data/normalization.py
- Command line
-
hf download hf://spaces/ServandoTorres/Relu/build/lib/src/data/normalization.py
-
curl -L -o normalization.py https://huggingface.co/spaces/ServandoTorres/Relu/resolve/main/build/lib/src/data/normalization.py
796 Bytes
| import torch | |
| import torch.nn as nn | |
| class DatasetNormalization(nn.Module): | |
| """ | |
| Just add this layer as the first layer of your network | |
| and it will normalize your raw features. | |
| But do not forget to take log of counters-like features by yourself. | |
| """ | |
| def init(self, x, nonorm_suffix_size=0): | |
| super().init() | |
| mu = torch.mean(x, dim=(-2,)) | |
| std = torch.clamp(torch.std(x, dim=(-2,)), min=0.001) | |
| if nonorm_suffix_size > 0: | |
| mu = torch.cat((mu, torch.zeros(nonorm_suffix_size))) | |
| std = torch.cat((std, torch.ones(nonorm_suffix_size))) | |
| self.p_mu = nn.Parameter(mu, requires_grad=False) | |
| self.p_std = nn.Parameter(std, requires_grad=False) | |
| def forward(self, x): | |
| return (x - self.p_mu) / self.p_std |