From 502f6d6c267519ffd5865c685e2b293989d04fd5 Mon Sep 17 00:00:00 2001 From: Vahe Hakobyan Date: Wed, 18 Mar 2020 16:49:39 +0100 Subject: [PATCH] Model serialization (#8) * wip: serialization ran successfully * wip: deserialization ran successfully * wip: deepar serialization --- pts/feature/time_feature.py | 3 ++- pts/transform/convert.py | 21 ++++++++++----------- pts/transform/feature.py | 10 +++++----- pts/transform/field.py | 12 +++++++----- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/pts/feature/time_feature.py b/pts/feature/time_feature.py index 7d4f656..816a8a0 100644 --- a/pts/feature/time_feature.py +++ b/pts/feature/time_feature.py @@ -4,11 +4,12 @@ from typing import List import numpy as np import pandas as pd from pandas.tseries.frequencies import to_offset - +from pts.core.component import validated from .utils import get_granularity class TimeFeature(ABC): + @validated() def __init__(self, normalized: bool = True): self.normalized = normalized diff --git a/pts/transform/convert.py b/pts/transform/convert.py index 6df4ac4..2803600 100644 --- a/pts/transform/convert.py +++ b/pts/transform/convert.py @@ -19,8 +19,7 @@ from scipy.special import erf, erfinv import torch from pts.exception import assert_pts -from pts.dataset import DataEntry - +from pts.core.component import validated from pts.dataset import DataEntry from .transform import ( @@ -42,7 +41,7 @@ class AsNumpyArray(SimpleTransformation): dtype numpy dtype to use. """ - + @validated() def __init__( self, field: str, expected_ndim: int, dtype: np.dtype = np.float32 ) -> None: @@ -86,7 +85,7 @@ class ExpandDimArray(SimpleTransformation): axis Axis to expand (see np.expand_dims for details) """ - + @validated() def __init__(self, field: str, axis: Optional[int] = None) -> None: self.field = field self.axis = axis @@ -112,7 +111,7 @@ class VstackFeatures(SimpleTransformation): drop_inputs If set to true the input fields will be dropped. """ - + @validated() def __init__( self, output_field: str, input_fields: List[str], drop_inputs: bool = True, ) -> None: @@ -148,7 +147,7 @@ class ConcatFeatures(SimpleTransformation): drop_inputs If set to true the input fields will be dropped. """ - + @validated() def __init__( self, output_field: str, input_fields: List[str], drop_inputs: bool = True, ) -> None: @@ -180,7 +179,7 @@ class SwapAxes(SimpleTransformation): axes Axes to use """ - + @validated() def __init__(self, input_fields: List[str], axes: Tuple[int, int]) -> None: self.input_fields = input_fields self.axis1, self.axis2 = axes @@ -215,7 +214,7 @@ class ListFeatures(SimpleTransformation): drop_inputs If true the input fields will be removed from the result. """ - + @validated() def __init__( self, output_field: str, input_fields: List[str], drop_inputs: bool = True, ) -> None: @@ -238,7 +237,7 @@ class TargetDimIndicator(SimpleTransformation): """ Label-encoding of the target dimensions. """ - + @validated() def __init__(self, field_name: str, target_field: str) -> None: self.field_name = field_name self.target_field = target_field @@ -252,7 +251,7 @@ class SampleTargetDim(FlatMapTransformation): """ Samples random dimensions from the target at training time. """ - + @validated() def __init__( self, field_name: str, @@ -304,7 +303,7 @@ class CDFtoGaussianTransform(MapTransformation): Note that this transformation is currently intended for multivariate targets only. """ - + @validated() def __init__( self, target_dim: int, diff --git a/pts/transform/feature.py b/pts/transform/feature.py index 5f6d3b9..34d2b3d 100644 --- a/pts/transform/feature.py +++ b/pts/transform/feature.py @@ -18,7 +18,7 @@ import pandas as pd from pts.dataset import DataEntry from pts.feature import TimeFeature - +from pts.core.component import validated from .transform import SimpleTransformation, MapTransformation from .split import shift_timestamp @@ -49,7 +49,7 @@ class AddObservedValuesIndicator(SimpleTransformation): they will not be replaced. In any case the indicator is included in the result. """ - + @validated() def __init__( self, target_field: str, @@ -101,7 +101,7 @@ class AddConstFeature(MapTransformation): dtype Numpy dtype to use for resulting array. """ - + @validated() def __init__( self, output_field: str, @@ -146,7 +146,7 @@ class AddTimeFeatures(MapTransformation): pred_length Prediction length """ - + @validated() def __init__( self, start_field: str, @@ -226,7 +226,7 @@ class AddAgeFeature(MapTransformation): If set to true the age feature grows logarithmically otherwise linearly over time. """ - + @validated() def __init__( self, target_field: str, diff --git a/pts/transform/field.py b/pts/transform/field.py index 06a36ba..502afe1 100644 --- a/pts/transform/field.py +++ b/pts/transform/field.py @@ -15,7 +15,7 @@ from collections import Counter from typing import Any, Dict, List from pts.dataset import DataEntry - +from pts.core.component import validated from .transform import SimpleTransformation, MapTransformation @@ -28,7 +28,7 @@ class RenameFields(SimpleTransformation): mapping Name mapping `input_name -> output_name` """ - + @validated() def __init__(self, mapping: Dict[str, str]) -> None: self.mapping = mapping values_count = Counter(mapping.values()) @@ -46,6 +46,8 @@ class RenameFields(SimpleTransformation): class RemoveFields(SimpleTransformation): + + @validated() def __init__(self, field_names: List[str]) -> None: self.field_names = field_names @@ -67,7 +69,7 @@ class SetField(SimpleTransformation): value Value to be set """ - + @validated() def __init__(self, output_field: str, value: Any) -> None: self.output_field = output_field self.value = value @@ -88,7 +90,7 @@ class SetFieldIfNotPresent(SimpleTransformation): value Value to be set """ - + @validated() def __init__(self, field: str, value: Any) -> None: self.output_field = field self.value = value @@ -108,7 +110,7 @@ class SelectFields(MapTransformation): input_fields List of fields to keep. """ - + @validated() def __init__(self, input_fields: List[str]) -> None: self.input_fields = input_fields