def trainer(snapshot_manager: SnapshotManager, model: t.nn.Module, training_set: Iterator, timeseries_frequency: int, loss_name: str, iterations: int, learning_rate: float = 0.001): model = model.to(default_device()) optimizer = optim.Adam(model.parameters(), lr=learning_rate) training_loss_fn = __loss_fn(loss_name) lr_decay_step = iterations // 3 if lr_decay_step == 0: lr_decay_step = 1 iteration = snapshot_manager.restore(model, optimizer) # # Training Loop # snapshot_manager.enable_time_tracking() for i in range(iteration + 1, iterations + 1): model.train() x, x_mask, y, y_mask = map(to_tensor, next(training_set)) optimizer.zero_grad() forecast = model(x, x_mask) training_loss = training_loss_fn(x, timeseries_frequency, forecast, y, y_mask) if np.isnan(float(training_loss)): break training_loss.backward() t.nn.utils.clip_grad_norm_(model.parameters(), 1.0) optimizer.step() for param_group in optimizer.param_groups: param_group["lr"] = learning_rate * 0.5**(i // lr_decay_step) snapshot_manager.register(iteration=i, training_loss=float(training_loss), validation_loss=np.nan, model=model, optimizer=optimizer) return model
def instance(self, repeat: int, lookback: int, loss: str, history_size: Dict[str, float], iterations: Dict[str, int], model_type: str): dataset = M4Dataset.load(training=True) forecasts = [] for seasonal_pattern in M4Meta.seasonal_patterns: history_size_in_horizons = history_size[seasonal_pattern] horizon = M4Meta.horizons_map[seasonal_pattern] input_size = lookback * horizon # Training Set training_values = group_values(dataset.values, dataset.groups, seasonal_pattern) training_set = TimeseriesSampler( timeseries=training_values, insample_size=input_size, outsample_size=horizon, window_sampling_limit=int(history_size_in_horizons * horizon)) if model_type == 'interpretable': model = interpretable(input_size=input_size, output_size=horizon) elif model_type == 'generic': model = generic(input_size=input_size, output_size=horizon) else: raise Exception(f'Unknown model type {model_type}') # Train model snapshot_manager = SnapshotManager( snapshot_dir=os.path.join(self.root, 'snapshots', seasonal_pattern), total_iterations=iterations[seasonal_pattern]) model = trainer( snapshot_manager=snapshot_manager, model=model, training_set=iter(training_set), timeseries_frequency=M4Meta.frequency_map[seasonal_pattern], loss_name=loss, iterations=iterations[seasonal_pattern]) # Build forecasts x, x_mask = map(to_tensor, training_set.last_insample_window()) model.eval() with t.no_grad(): forecasts.extend(model(x, x_mask).cpu().detach().numpy()) forecasts_df = pd.DataFrame( forecasts, columns=[f'V{i + 1}' for i in range(np.max(M4Meta.horizons))]) forecasts_df.index = dataset.ids forecasts_df.index.name = 'id' forecasts_df.to_csv(os.path.join(self.root, 'forecast.csv'))
def instance(self, repeat: int, lookback: int, loss: str, history_size: int, iterations: Dict[str, int], model_type: str): dataset = TrafficDataset.load() for split_name, split_date in splits.items(): horizon = TrafficMeta.horizon input_size = lookback * horizon # Training Set training_set, test_set = dataset.split_by_date(split_date) training_values = training_set.values test_values = test_set.values training_set = TimeseriesSampler( timeseries=training_values, insample_size=input_size, outsample_size=horizon, window_sampling_limit=history_size * horizon) if model_type == 'interpretable': model = interpretable(input_size=input_size, output_size=horizon) elif model_type == 'generic': model = generic(input_size=input_size, output_size=horizon) else: raise Exception(f'Unknown model type {model_type}') # Train model snapshot_manager = SnapshotManager( snapshot_dir=os.path.join(self.root, 'snapshots', split_name), total_iterations=iterations[split_name]) model = trainer(snapshot_manager=snapshot_manager, model=model, training_set=iter(training_set), timeseries_frequency=TrafficMeta.frequency, loss_name=loss, iterations=iterations[split_name]) # Build forecasts forecasts = [] model.eval() with t.no_grad(): for i in range(test_windows): window_input_set = np.concatenate([ training_values, test_values[:, :i * TrafficMeta.horizon] ], axis=1) input_set = TimeseriesSampler(timeseries=window_input_set, insample_size=input_size, outsample_size=horizon, window_sampling_limit=int( history_size * horizon)) x, x_mask = map(to_tensor, input_set.last_insample_window()) window_forecast = model(x, x_mask).cpu().detach().numpy() forecasts = window_forecast if len( forecasts) == 0 else np.concatenate( [forecasts, window_forecast], axis=1) forecasts_df = pd.DataFrame(forecasts, columns=[ f'V{i + 1}' for i in range(test_windows * TrafficMeta.horizon) ]) forecasts_df.index.name = 'id' forecasts_df.to_csv( os.path.join(self.root, f'forecast_{split_name}.csv'))