Update modeling_super_linear.py
Browse files- modeling_super_linear.py +68 -58
modeling_super_linear.py
CHANGED
|
@@ -192,71 +192,81 @@ class NLinear(nn.Module):
|
|
| 192 |
|
| 193 |
|
| 194 |
class RLinear(nn.Module):
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
self.
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
|
|
|
| 228 |
|
| 229 |
-
|
|
|
|
| 230 |
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
self.transform_model(x.shape[1],3)
|
| 239 |
|
| 240 |
-
|
| 241 |
-
#
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
#x = x * (512/x.shape[1])
|
| 246 |
-
return x
|
| 247 |
|
| 248 |
-
|
| 249 |
-
if len(x_shape) == 2:
|
| 250 |
-
x = x.unsqueeze(-1)
|
| 251 |
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
|
|
|
|
|
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
|
| 262 |
"-------------------------------------------------------------------------------------------------------------------"
|
|
|
|
| 192 |
|
| 193 |
|
| 194 |
class RLinear(nn.Module):
|
| 195 |
+
"""
|
| 196 |
+
Resizable linear projection from variable input length L to fixed horizon.
|
| 197 |
+
Each channel is projected independently (no mixing across channels).
|
| 198 |
+
"""
|
| 199 |
+
def __init__(self, input_len: int, output_len: int):
|
| 200 |
+
super().__init__()
|
| 201 |
+
self.seq_len = input_len # “design” length
|
| 202 |
+
self.horizon = output_len
|
| 203 |
+
# plain weight + bias – we will *interpolate* weight when L ≠ seq_len
|
| 204 |
+
self.linear = nn.Linear(input_len, output_len, bias=True)
|
| 205 |
+
|
| 206 |
+
# RevIN — your own implementation; keep it stateless for safety
|
| 207 |
+
self.revin = RevIN(num_features=None, affine=False,
|
| 208 |
+
norm_type=None, subtract_last=False)
|
| 209 |
+
|
| 210 |
+
def _resize_weight(self, weight: torch.Tensor, new_in: int) -> torch.Tensor:
|
| 211 |
+
"""
|
| 212 |
+
Bilinearly interpolate the *columns* of `weight` so that
|
| 213 |
+
weight.shape becomes (horizon, new_in).
|
| 214 |
+
"""
|
| 215 |
+
if new_in == weight.shape[1]:
|
| 216 |
+
return weight # no resizing needed
|
| 217 |
+
|
| 218 |
+
# weight: (out, in) → (1,1,out,in) so we can use interpolate
|
| 219 |
+
w4d = weight.unsqueeze(0).unsqueeze(0)
|
| 220 |
+
w_resized = F.interpolate(
|
| 221 |
+
w4d, size=(self.horizon, new_in), mode="bilinear",
|
| 222 |
+
align_corners=False
|
| 223 |
+
)[0, 0] # back to (out, new_in)
|
| 224 |
+
return w_resized
|
| 225 |
+
|
| 226 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 227 |
+
"""
|
| 228 |
+
x: (B, L, C) or (B, L) ⇒ (B, horizon, C) or (B, horizon)
|
| 229 |
+
"""
|
| 230 |
+
# make sure x is 3-D
|
| 231 |
+
squeeze_last_dim = False
|
| 232 |
+
if x.dim() == 2: # (B,L)
|
| 233 |
+
x = x.unsqueeze(-1) # (B,L,1)
|
| 234 |
+
squeeze_last_dim = True
|
| 235 |
|
| 236 |
+
B, L, C = x.shape
|
| 237 |
|
| 238 |
+
# RevIN normalisation (over time dimension)
|
| 239 |
+
x = self.revin(x, "norm")
|
| 240 |
|
| 241 |
+
if L == self.seq_len:
|
| 242 |
+
# fast path — built-in linear works
|
| 243 |
+
# reshape so that each channel is treated independently
|
| 244 |
+
x = x.permute(0, 2, 1) # (B,C,L)
|
| 245 |
+
x = self.linear(x) # (B,C,horizon)
|
| 246 |
+
x = x.permute(0, 2, 1) # (B,horizon,C)
|
|
|
|
|
|
|
| 247 |
|
| 248 |
+
else:
|
| 249 |
+
# slow path — resize the weight to match L
|
| 250 |
+
# freeze current weight & bias
|
| 251 |
+
W = self.linear.weight.detach() # (out,in)
|
| 252 |
+
b = self.linear.bias.detach() # (out)
|
|
|
|
|
|
|
| 253 |
|
| 254 |
+
W_resized = self._resize_weight(W, L) # (out,L)
|
|
|
|
|
|
|
| 255 |
|
| 256 |
+
# apply per channel
|
| 257 |
+
# x: (B,L,C) → (B,C,L) so that last dim is L
|
| 258 |
+
x = x.permute(0, 2, 1)
|
| 259 |
+
# out = x @ W_resized.T + b
|
| 260 |
+
out = torch.einsum("bcl,ol->bco", x, W_resized) + b
|
| 261 |
+
x = out.permute(0, 2, 1) # back to (B,horizon,C)
|
| 262 |
|
| 263 |
+
# RevIN denorm
|
| 264 |
+
x = self.revin(x, "denorm")
|
| 265 |
+
|
| 266 |
+
if squeeze_last_dim:
|
| 267 |
+
x = x.squeeze(-1) # (B,horizon)
|
| 268 |
+
|
| 269 |
+
return x
|
| 270 |
|
| 271 |
|
| 272 |
"-------------------------------------------------------------------------------------------------------------------"
|