added initial artifical const dataset

This commit is contained in:
Dr. Kashif Rasul
2019-11-19 16:45:05 +01:00
parent d7aef183ce
commit eb67bf0b9a
5 changed files with 160 additions and 1 deletions
+1
View File
@@ -11,3 +11,4 @@ from .sampler import (
from .process import ProcessStartField, ProcessDataEntry
from .utils import to_pandas
from .stat import ScaleHistogram, calculate_dataset_statistics
from .artificial import constant_dataset
+70
View File
@@ -0,0 +1,70 @@
from typing import Callable, List, NamedTuple, Optional, Tuple, Union
from .common import MetaData, CategoricalFeatureInfo, BasicFeatureInfo, FieldName, Dataset
from .list_dataset import ListDataset
from .stat import DatasetStatistics, calculate_dataset_statistics
class DatasetInfo(NamedTuple):
"""
Information stored on a dataset. When downloading from the repository, the
dataset repository checks that the obtained version matches the one
declared in dataset_info/dataset_name.json.
"""
name: str
metadata: MetaData
prediction_length: int
train_statistics: DatasetStatistics
test_statistics: DatasetStatistics
def constant_dataset() -> Tuple[DatasetInfo, Dataset, Dataset]:
metadata = MetaData(
freq="1H",
feat_static_cat=[
CategoricalFeatureInfo(
name="feat_static_cat_000", cardinality="10"
)
],
feat_static_real=[BasicFeatureInfo(name="feat_static_real_000")],
)
start_date = "2000-01-01 00:00:00"
train_ds = ListDataset(
data_iter=[
{
FieldName.ITEM_ID: str(i),
FieldName.START: start_date,
FieldName.TARGET: [float(i)] * 24,
FieldName.FEAT_STATIC_CAT: [i],
FieldName.FEAT_STATIC_REAL: [float(i)],
}
for i in range(10)
],
freq=metadata.freq,
)
test_ds = ListDataset(
data_iter=[
{
FieldName.ITEM_ID: str(i),
FieldName.START: start_date,
FieldName.TARGET: [float(i)] * 30,
FieldName.FEAT_STATIC_CAT: [i],
FieldName.FEAT_STATIC_REAL: [float(i)],
}
for i in range(10)
],
freq=metadata.freq,
)
info = DatasetInfo(
name="constant_dataset",
metadata=metadata,
prediction_length=2,
train_statistics=calculate_dataset_statistics(train_ds),
test_statistics=calculate_dataset_statistics(test_ds),
)
return info, train_ds, test_ds
+19 -1
View File
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import Any, Dict, Iterable, NamedTuple, Sized
from typing import Any, Dict, Iterable, NamedTuple, Sized, List, Optional
DataEntry = Dict[str, Any]
@@ -42,3 +42,21 @@ class Dataset(Sized, Iterable[DataEntry], ABC):
@abstractmethod
def __len__(self):
pass
class CategoricalFeatureInfo():
name: str
cardinality: str
class BasicFeatureInfo():
name: str
class MetaData():
freq: str = None
target: Optional[BasicFeatureInfo] = None
feat_static_cat: List[CategoricalFeatureInfo] = []
feat_static_real: List[BasicFeatureInfo] = []
feat_dynamic_real: List[BasicFeatureInfo] = []
feat_dynamic_cat: List[CategoricalFeatureInfo] = []
prediction_length: Optional[int] = None
+1
View File
@@ -2,3 +2,4 @@ from .estimator import Estimator, PTSEstimator
from .forecast import Forecast
from .predictor import Predictor
from .quantile import Quantile
from .utils import get_module_forward_input_names
@@ -0,0 +1,69 @@
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
from itertools import islice
import torch
from pts.modules import StudentTOutput
from pts.dataset import constant_dataset, TrainDataLoader
from pts.model.deepar import DeepAREstimator
from pts.model import get_module_forward_input_names
from pts import Trainer
ds_info, train_ds, test_ds = constant_dataset()
freq = ds_info.metadata.freq
prediction_length = ds_info.prediction_length
def test_distribution():
"""
Makes sure additional tensors can be accessed and have expected shapes
"""
prediction_length = ds_info.prediction_length
estimator = DeepAREstimator(
freq=freq,
prediction_length=prediction_length,
trainer=Trainer(epochs=1, num_batches_per_epoch=1),
distr_output=StudentTOutput(),
)
train_output = estimator.train_model(train_ds)
# todo adapt loader to anomaly detection use-case
batch_size = 2
num_samples = 3
training_data_loader = TrainDataLoader(
dataset=train_ds,
transform=train_output.transformation,
batch_size=batch_size,
num_batches_per_epoch=estimator.trainer.num_batches_per_epoch,
device=torch.device("cpu")
)
seq_len = 2 * ds_info.prediction_length
for data_entry in islice(training_data_loader, 1):
input_names = get_module_forward_input_names(train_output.trained_net)
distr = train_output.trained_net.distribution(
*[data_entry[k] for k in input_names]
)
assert distr.sample(num_samples).shape == (
num_samples,
batch_size,
seq_len,
)