Download weather_model.py from Go4444Ward/w_MLP: direct link, hf CLI and curl.
- Browser
- Download file 1.98 kB
-
https://huggingface.co/Go4444Ward/w_MLP/resolve/main/weather_model.py
- Command line
-
hf download hf://Go4444Ward/w_MLP/weather_model.py
-
curl -L -o weather_model.py https://huggingface.co/Go4444Ward/w_MLP/resolve/main/weather_model.py
1.98 kB
| """Coordinate-conditioned weather regression network.""" | |
| import math | |
| import numpy as np | |
| import torch | |
| from torch import nn | |
| class CoordinateMLP(nn.Module): | |
| def __init__(self, center, scale, width=4096): | |
| super().__init__() | |
| rng = np.random.default_rng(2026) | |
| self.register_buffer( | |
| "projection", | |
| torch.tensor(rng.normal(size=(2, 64)) * 12, dtype=torch.float32), | |
| ) | |
| self.register_buffer( | |
| "frequencies", torch.tensor(np.geomspace(1, 4096, 64), dtype=torch.float32) | |
| ) | |
| self.register_buffer("location_center", torch.tensor([36.0, 128.0])) | |
| self.register_buffer("location_scale", torch.tensor([3.0, 4.0])) | |
| self.register_buffer("center", torch.tensor(center, dtype=torch.float32)) | |
| self.register_buffer("scale", torch.tensor(scale, dtype=torch.float32)) | |
| self.input = nn.Linear(24 + 3 + 128 + 128, width) | |
| self.blocks = nn.ModuleList([nn.Linear(width, width) for _ in range(4)]) | |
| self.output = nn.Linear(width, 9) | |
| nn.init.zeros_(self.output.weight) | |
| nn.init.zeros_(self.output.bias) | |
| def normalized(self, raw): | |
| location = (raw[:, 24:26] - self.location_center) / self.location_scale | |
| spatial = location @ self.projection * (2 * math.pi) | |
| temporal = raw[:, 26:27] / 8760 * self.frequencies[None] * (2 * math.pi) | |
| x = torch.cat( | |
| [ | |
| raw[:, :24], | |
| location, | |
| raw[:, 26:27] / (6 * 8760), | |
| spatial.sin(), | |
| spatial.cos(), | |
| temporal.sin(), | |
| temporal.cos(), | |
| ], | |
| dim=1, | |
| ) | |
| h = torch.nn.functional.silu(self.input(x)) | |
| for block in self.blocks: | |
| h = (h + torch.nn.functional.silu(block(h))) / math.sqrt(2) | |
| return self.output(h) | |
| def forward(self, raw): | |
| return self.normalized(raw).float() * self.scale + self.center | |