示例#1
0
    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'))
示例#2
0
文件: main.py 项目: valeman/N-BEATS-1
    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'))