Release experimental Transition Foundation Model v0.1 with reproducible source and validation
1baf215 verified Download tfm/data/windows.py from maxffarrell/transition-foundation-model: direct link, hf CLI and curl.
- Browser
- Download file 5.07 kB
-
https://huggingface.co/maxffarrell/transition-foundation-model/resolve/main/tfm/data/windows.py
- Command line
-
hf download hf://maxffarrell/transition-foundation-model/tfm/data/windows.py
-
curl -L -o windows.py https://huggingface.co/maxffarrell/transition-foundation-model/resolve/main/tfm/data/windows.py
5.07 kB
| """Leakage-safe historical inputs and horizon-specific normalized state deltas. | |
| History ends at current state t. Action/exogenous row s describes transition | |
| s -> s+1, so historical actions stop at t-1. Planned controls start at t. | |
| """ | |
| import numpy as np | |
| import torch | |
| from torch.utils.data import Dataset | |
| from .schema import FIELD_INDEX, HORIZONS, MAX_STATE_FIELDS, NUM_FIELDS, ROLE_IDS, UNIT_IDS, role_fields | |
| class WindowDataset(Dataset): | |
| def __init__(self, episodes, normalization, history_steps=24, future_steps=8, samples=10000, seed=0): | |
| self.episodes = list(episodes) | |
| self.normalization = normalization | |
| self.history_steps, self.future_steps = int(history_steps), int(future_steps) | |
| if history_steps < 1 or future_steps < max(HORIZONS): | |
| raise ValueError("History must be positive and future_steps must cover horizon 8") | |
| available = [] | |
| for i, episode in enumerate(self.episodes): | |
| available.extend((i, t) for t in range(history_steps - 1, len(episode.values) - future_steps)) | |
| if not available: | |
| raise ValueError("No episodes long enough for requested window") | |
| if samples is None: | |
| self.indices = available | |
| else: | |
| if int(samples) <= 0: | |
| raise ValueError("samples must be positive") | |
| rng = np.random.default_rng(seed) | |
| self.indices = [available[i] for i in rng.integers(0, len(available), int(samples))] | |
| self.seed = seed | |
| def __len__(self): | |
| return len(self.indices) | |
| def __getitem__(self, index): | |
| episode_index, t = self.indices[index] | |
| ep = self.episodes[episode_index] | |
| h, k = self.history_steps, self.future_steps | |
| history_values = ep.values[t - h + 1 : t + 1].copy() | |
| history_mask = ep.observation_mask[t - h + 1 : t + 1].copy() | |
| # Explicit transition arrays are authoritative for historical controls. | |
| past_actions = (ROLE_IDS == 1) & ep.field_mask | |
| past_exogenous = (ROLE_IDS == 2) & ep.field_mask | |
| history_values[:-1, past_actions] = ep.actions[t - h + 1 : t, past_actions] | |
| history_values[:-1, past_exogenous] = ep.exogenous[t - h + 1 : t, past_exogenous] | |
| # Exclude controls and known forecast inputs applied AFTER the anchor state. | |
| history_mask[-1, ROLE_IDS != 0] = False | |
| history_values[-1, ROLE_IDS != 0] = 0 | |
| current_mask = history_mask[-1] | |
| norm = self.normalization | |
| history = norm.transform(history_values, history_mask) | |
| future_actions = np.zeros((k, NUM_FIELDS)) | |
| future_exogenous = np.zeros((k, NUM_FIELDS)) | |
| action_slots, exogenous_slots = (ROLE_IDS == 1) & ep.field_mask, (ROLE_IDS == 2) & ep.field_mask | |
| future_actions[:, action_slots] = norm.transform(ep.actions[t : t + k])[:, action_slots] | |
| future_exogenous[:, exogenous_slots] = norm.transform(ep.exogenous[t : t + k])[:, exogenous_slots] | |
| query_fields = np.zeros(MAX_STATE_FIELDS * len(HORIZONS), dtype=np.int64) | |
| query_horizons = np.zeros_like(query_fields, dtype=float) | |
| target = np.zeros_like(query_fields, dtype=float) | |
| target_mask = np.zeros_like(query_fields, dtype=bool) | |
| q = 0 | |
| for name in role_fields(ep.schema, "state"): | |
| field = FIELD_INDEX[name] | |
| for horizon in HORIZONS: | |
| query_fields[q] = field | |
| query_horizons[q] = ep.timestamps[t + horizon] - ep.timestamps[t] | |
| target_mask[q] = ep.observation_mask[t, field] and ep.observation_mask[t + horizon, field] | |
| if target_mask[q]: | |
| target[q] = (ep.values[t + horizon, field] - ep.values[t, field]) / norm.scale[field] | |
| q += 1 | |
| arrays = { | |
| "history": history, | |
| "history_mask": history_mask.astype(float), | |
| "history_dt": ep.timestamps[t - h + 1 : t + 1] - ep.timestamps[t], | |
| "current": history[-1], | |
| "current_mask": current_mask.astype(float), | |
| "future_actions": future_actions, | |
| "future_exogenous": future_exogenous, | |
| "future_dt": ep.timestamps[t + 1 : t + k + 1] - ep.timestamps[t], | |
| "field_mask": ep.field_mask, | |
| "field_ids": np.arange(NUM_FIELDS, dtype=np.int64), | |
| "unit_ids": UNIT_IDS.copy(), | |
| "role_ids": ROLE_IDS.copy(), | |
| "query_fields": query_fields, | |
| "query_horizons": query_horizons, | |
| "target": target, | |
| "target_mask": target_mask, | |
| } | |
| return { | |
| name: torch.from_numpy( | |
| np.asarray( | |
| value, | |
| dtype=( | |
| np.int64 | |
| if name.endswith("ids") or name == "query_fields" | |
| else bool | |
| if name in ("field_mask", "target_mask") | |
| else np.float32 | |
| ), | |
| ).copy() | |
| ) | |
| for name, value in arrays.items() | |
| } | |