Exemplo n.º 1
0
    def __init__(self,
                 train_dataset: DeepARDataset,
                 predictor_filepath: str,
                 mean: bool = True,
                 num_samples: int = 100,
                 quantiles: List[float] = [],
                 nan_padding: bool = True):
        """ constructs DeepAR forecast object
        
            if mean False, will return median point estimates 
        """

        self.train_dataset = train_dataset
        self.train_frame = train_dataset.get_frame()
        self.predictor = GluonPredictor.deserialize(Path(predictor_filepath))
        self.mean = mean
        self.prediction_length = train_dataset.get_pred_length()
        self.context_length = train_dataset.get_context_length()
        self.num_samples = num_samples
        self.quantiles = quantiles
        self.nan_padding = nan_padding

        self.data = []
        self.series_idxs = []
        self.max_intervals = []
        self.pre_pad_lens = []
        self.total_in_samples = []
Exemplo n.º 2
0
    def __init__(
        self,
        train_dataset: NBEATSDataset,
        predictor_filepath: str,
        interpretable: bool = True,
        mean: bool = True,
        nan_padding: bool = True,
    ):
        """constructs NBEATS forecast object

        if mean False, will return median point estimates
        """

        self.train_dataset = train_dataset
        self.train_frame = train_dataset.get_frame()
        self.predictor = GluonPredictor.deserialize(Path(predictor_filepath))
        self.interpretable = interpretable
        if interpretable:
            self.mean = True
        else:
            self.mean = mean
        self.prediction_length = train_dataset.get_pred_length()
        self.context_length = train_dataset.get_context_length()
        self.nan_padding = nan_padding

        self.data = []
        self.series_idxs = []
        self.max_intervals = []
        self.pre_pad_lens = []
        self.total_in_samples = []
Exemplo n.º 3
0
def unserialize_all(
    base_path: str,
) -> List[GluonPredictor]:
    """ unserialize all predictors from directory
    
    Arguments:
        base_path {str} -- base dir name from which to load models

    Returns:
        List[GluonPredictor] -- list of unserialized GluonPredictor objects
    """
    
    return [
        GluonPredictor.deserialize(Path(base_dir)) 
        for base_dir in glob(base_path + "-fold*")
    ]
def evaluate_deepar(
    predictor: GluonPredictor,
    train_data: ListDataset,
    test_data: ListDataset,
    hierarchy_dict: Dict[int, List[int]],
    output_file: str = None,
    output_mean: bool = True,
    output_residuals: bool = True,
) -> Dict[Union[int, str], Dict[str, float]]:
    """ aggregates error metrics for each level of the hierarchy, optionally writes predictions/in-sample residuals 
        to output file
    
    Arguments:
        predictor {GluonPredictor} -- predictor
        train_data {ListDataset} -- train dataset
        test_data {ListDataset} -- test dataset
        hierarchy_dict {Dict[int, List[int]]} -- mapping from hierachy level to series prediction idxs included
            in that level of hierarchy
    
    Keyword Arguments:
        output_file {str} -- output_file to save predictions (default: {None})
        output_mean {bool} -- whether to output the mean (or median) predictions (default: {False})
        output_residuals {bool} -- whether to output the residuals of in-sample predictions. If True, 
            the in-sample residuals will be prepended to the out-of-sample predictions. Thus, 
            if the in-sample data contains 24 timeteps, and the out-of-sample data contains 6 timesteps,
            the output data frame will contain 30 rows (timesteps) (default: {True})

    Returns:
        Dict[Union[int, str], Dict[str, float]] -- mapping of hierarchy level (0-indexed) to dictionaries of aggregated metrics 
            for that level of the hierarchy
    """

    eval_forecasts = []
    output_forecasts = []
    with tqdm(predictor.predict(train_data),
              total=len(train_data),
              desc="Making Predictions") as it, np.errstate(invalid='ignore'):
        for forecast in it:
            output_forecasts.append(
                forecast.mean if output_mean else forecast.quantile(0.5))
            eval_forecasts.append(forecast)
        preds = np.array(output_forecasts)

    if output_file:
        if output_residuals:
            preds = np.concatenate(
                (predictor.prediction_net.residuals.asnumpy(), preds), axis=1)
        out_df = pd.DataFrame(preds).T
        out_df.to_csv(output_file, index=False)

    eval_forecasts = np.array(eval_forecasts)
    evaluator = Evaluator(quantiles=[0.5])
    evaluations = {
        level: evaluator([
            to_pandas(series)
            for series in np.array(list(test_data))[np.array(idxs)]
        ], eval_forecasts[np.array(idxs)])[0]
        for level, idxs in hierarchy_dict.items()
    }
    evaluations['all'] = evaluator(
        [to_pandas(series) for series in np.array(list(test_data))],
        eval_forecasts)[0]
    return evaluations