File size: 1,975 Bytes
2b6ba7f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | """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
|