|
| 1 | +import torch |
| 2 | +from .condition_interface import ConditionInterface |
| 3 | +from ..loss import TimeWeightingInterface, ConstantTimeWeighting |
| 4 | +from ..utils import check_consistency |
| 5 | + |
| 6 | + |
| 7 | +class AutoregressiveCondition(ConditionInterface): |
| 8 | + """ |
| 9 | + A specialized condition for autoregressive tasks. |
| 10 | + It generates input/unroll pairs from a single time-series tensor. |
| 11 | + """ |
| 12 | + |
| 13 | + __slots__ = ["input", "unroll"] |
| 14 | + |
| 15 | + def __init__( |
| 16 | + self, |
| 17 | + data, |
| 18 | + unroll_length, |
| 19 | + num_unrolls=None, |
| 20 | + randomize=True, |
| 21 | + time_weighting=None, |
| 22 | + ): |
| 23 | + """ |
| 24 | + Create an AutoregressiveCondition. |
| 25 | + """ |
| 26 | + super().__init__() |
| 27 | + |
| 28 | + self._n_timesteps, n_features = data.shape |
| 29 | + self._unroll_length = unroll_length |
| 30 | + self._requested_num_unrolls = num_unrolls |
| 31 | + self._randomize = randomize |
| 32 | + |
| 33 | + # time weighting: weight the loss differently along the unroll |
| 34 | + if time_weighting is None: |
| 35 | + self._time_weighting = ConstantTimeWeighting() |
| 36 | + else: |
| 37 | + check_consistency(time_weighting, TimeWeightingInterface) |
| 38 | + self._time_weighting = time_weighting |
| 39 | + |
| 40 | + # windows creation |
| 41 | + initial_data = [] |
| 42 | + unroll_data = [] |
| 43 | + |
| 44 | + for starting_index in self.starting_indices: |
| 45 | + initial_data.append(data[starting_index]) |
| 46 | + target_start = starting_index + 1 |
| 47 | + unroll_data.append( |
| 48 | + data[target_start : target_start + self._unroll_length, :] |
| 49 | + ) |
| 50 | + |
| 51 | + self.input = torch.stack(initial_data) # [num_unrolls, features] |
| 52 | + self.unroll = torch.stack( |
| 53 | + unroll_data |
| 54 | + ) # [num_unrolls, unroll_length, features] |
| 55 | + |
| 56 | + @property |
| 57 | + def unroll_length(self): |
| 58 | + return self._unroll_length |
| 59 | + |
| 60 | + @property |
| 61 | + def time_weighting(self): |
| 62 | + return self._time_weighting |
| 63 | + |
| 64 | + @property |
| 65 | + def max_start_idx(self): |
| 66 | + max_start_idx = self._n_timesteps - self._unroll_length |
| 67 | + assert max_start_idx > 0, "Provided data sequence too short" |
| 68 | + return max_start_idx |
| 69 | + |
| 70 | + @property |
| 71 | + def num_unrolls(self): |
| 72 | + if self._requested_num_unrolls is None: |
| 73 | + return self.max_start_idx |
| 74 | + else: |
| 75 | + assert ( |
| 76 | + self._requested_num_unrolls < self.max_start_idx |
| 77 | + ), "too many samples requested" |
| 78 | + return self._requested_num_unrolls |
| 79 | + |
| 80 | + @property |
| 81 | + def starting_indices(self): |
| 82 | + all_starting_indices = torch.arange(self.max_start_idx) |
| 83 | + |
| 84 | + if self._randomize: |
| 85 | + perm = torch.randperm(len(all_starting_indices)) |
| 86 | + return all_starting_indices[perm[: self.num_unrolls]] |
| 87 | + else: |
| 88 | + selected_indices = torch.linspace( |
| 89 | + 0, len(all_starting_indices) - 1, self.num_unrolls |
| 90 | + ).long() |
| 91 | + return all_starting_indices[selected_indices] |
0 commit comments