コード例 #1
0
    def deserialize(
            cls,
            path: Path,
            ctx: Optional[mx.Context] = None) -> "SymbolBlockPredictor":
        ctx = ctx if ctx is not None else get_mxnet_context()

        with mx.Context(ctx):
            # deserialize constructor parameters
            with (path / "parameters.json").open("r") as fp:
                parameters = load_json(fp.read())

            parameters["ctx"] = ctx

            # deserialize transformation chain
            with (path / "input_transform.json").open("r") as fp:
                transform = load_json(fp.read())

            # deserialize prediction network
            num_inputs = len(parameters["input_names"])
            prediction_net = import_symb_block(num_inputs, path,
                                               "prediction_net")

            return SymbolBlockPredictor(
                input_transform=transform,
                prediction_net=prediction_net,
                **parameters,
            )
コード例 #2
0
    def deserialize(
            cls,
            path: Path,
            ctx: Optional[mx.Context] = None) -> "RepresentableBlockPredictor":
        ctx = ctx if ctx is not None else get_mxnet_context()

        with mx.Context(ctx):
            # deserialize constructor parameters
            with (path / "parameters.json").open("r") as fp:
                parameters = load_json(fp.read())

            # deserialize transformation chain
            with (path / "input_transform.json").open("r") as fp:
                transform = load_json(fp.read())

            # deserialize prediction network
            prediction_net = import_repr_block(path, "prediction_net")

            # input_names is derived from the prediction_net
            if "input_names" in parameters:
                del parameters["input_names"]

            parameters["ctx"] = ctx

            return RepresentableBlockPredictor(
                input_transform=transform,
                prediction_net=prediction_net,
                **parameters,
            )
コード例 #3
0
    def _get_estimator(self):
        if get_mxnet_context() != mx.context.cpu():
            self.params["hybridize"] = False

        with warning_filter():
            return self.gluonts_estimator_class.from_hyperparameters(
                **self._get_estimator_init_args())
コード例 #4
0
    def _get_estimator(self):
        # TODO: temporarily disabling hybridization on GPU due to mxnet issue
        # TODO: fixed in mxnet v2.0
        if get_mxnet_context() != mx.context.cpu():
            self.params["hybridize"] = False

        with warning_filter():
            return self.gluonts_estimator_class.from_hyperparameters(
                **self._get_estimator_init_args())
コード例 #5
0
ファイル: _base.py プロジェクト: wl935/gluon-ts
    def __init__(
        self,
        ctx: Optional[mx.Context] = None,
        epochs: int = 100,
        batch_size: int = 32,
        num_batches_per_epoch: int = 50,
        learning_rate: float = 1e-3,
        learning_rate_decay_factor: float = 0.5,
        patience: int = 10,
        minimum_learning_rate: float = 5e-5,
        clip_gradient: float = 10.0,
        weight_decay: float = 1e-8,
        init: Union[str, mx.initializer.Initializer] = "xavier",
        hybridize: bool = True,
        avg_strategy: Union[
            AveragingStrategy, IterationAveragingStrategy
        ] = SelectNBestMean(num_models=1),
        post_initialize_cb: Optional[Callable[[mx.gluon.Block], None]] = None,
    ) -> None:

        assert (
            0 <= epochs < float("inf")
        ), "The value of `epochs` should be >= 0"
        assert 0 < batch_size, "The value of `batch_size` should be > 0"
        assert (
            0 < num_batches_per_epoch
        ), "The value of `num_batches_per_epoch` should be > 0"
        assert (
            0 < learning_rate < float("inf")
        ), "The value of `learning_rate` should be > 0"
        assert (
            0 <= learning_rate_decay_factor < 1
        ), "The value of `learning_rate_decay_factor` should be in the [0, 1) range"
        assert 0 <= patience, "The value of `patience` should be >= 0"
        assert (
            0 <= minimum_learning_rate
        ), "The value of `minimum_learning_rate` should be >= 0"
        assert 0 < clip_gradient, "The value of `clip_gradient` should be > 0"
        assert 0 <= weight_decay, "The value of `weight_decay` should be => 0"

        self.epochs = epochs
        self.batch_size = batch_size
        self.num_batches_per_epoch = num_batches_per_epoch
        self.learning_rate = learning_rate
        self.learning_rate_decay_factor = learning_rate_decay_factor
        self.patience = patience
        self.minimum_learning_rate = minimum_learning_rate
        self.clip_gradient = clip_gradient
        self.weight_decay = weight_decay
        self.init = init
        self.hybridize = hybridize
        self.avg_strategy = avg_strategy
        self.ctx = ctx if ctx is not None else get_mxnet_context()
        self.halt = False
        self.post_initialize_cb = post_initialize_cb
コード例 #6
0
 def initialize_from_dataset(self,
                             input_dataset: Dataset,
                             ctx: mx.Context = get_mxnet_context()):
     # Rescale all time series in training set.
     train_target_sequence: np.ndarray = np.array([])
     for train_entry in input_dataset:
         train_entry_target = train_entry["target"]
         train_tar_mean = np.mean(train_entry_target)
         train_entry_target /= train_tar_mean
         train_target_sequence = np.concatenate(
             [train_target_sequence, train_entry_target])
     self.initialize_from_array(train_target_sequence, ctx)
コード例 #7
0
    def initialize_from_array(self,
                              input_array: np.ndarray,
                              ctx: mx.Context = get_mxnet_context()):
        r"""
        Initialize the representation based on a numpy array.

        Parameters
        ----------
        input_array
            Numpy array.
        ctx
            MXNet context.
        """
        pass
コード例 #8
0
    def initialize_from_dataset(self,
                                input_dataset: Dataset,
                                ctx: mx.Context = get_mxnet_context()):
        r"""
        Initialize the representation based on an entire dataset.

        Parameters
        ----------
        input_dataset
            GluonTS dataset.
        ctx
            MXNet context.
        """
        pass
コード例 #9
0
    def __init__(
            self,
            units: int,
            in_units: int,
            coeff: float = 0.9,
            activation: Optional[str] = None,
            use_bias: bool = True,
            flatten: bool = True,
            weight_initializer: init.Initializer = init.Orthogonal(scale=0.9),
            bias_initializer="zeros",
            dtype="float32",
            num_power_iter: int = 1,
            ctx: Optional[mx.Context] = None,
            **kwargs):
        super().__init__(**kwargs)
        self._coeff = coeff
        self._flatten = flatten
        self._ctx = ctx if ctx is not None else get_mxnet_context()
        self._num_power_iter = num_power_iter
        with self.name_scope():
            self._units = units
            self._in_units = in_units
            self._weight = self.params.get(
                "weight",
                shape=(units, in_units),
                init=weight_initializer,
                dtype=dtype,
            )
            self._u = self.params.get("u",
                                      init=mx.init.Normal(),
                                      shape=(1, units))

            if use_bias:
                self._bias = self.params.get("bias",
                                             shape=(units, ),
                                             init=bias_initializer,
                                             dtype=dtype)
            else:
                self._bias = None

            if activation is not None:
                self._act = get_activation(activation, prefix=activation + "_")
            else:
                self._act = None
コード例 #10
0
    def initialize_from_array(self,
                              input_array: np.ndarray,
                              ctx: mx.Context = get_mxnet_context()):
        # Calculate bin centers and bin edges using linear or quantile binning..
        if self.is_quantile:
            bin_centers = np.quantile(
                input_array,
                np.linspace(0, self.quantile_scaling_limit, self.num_bins),
            )
            bin_centers = ensure_binning_monotonicity(bin_centers)
        else:
            has_negative_data = np.any(input_array < 0)
            low = -self.linear_scaling_limit if has_negative_data else 0
            high = self.linear_scaling_limit
            bin_centers = np.linspace(low, high, self.num_bins)
        bin_edges = bin_edges_from_bin_centers(bin_centers)

        # Store bin centers and edges since their are globally applicable to all time series.
        with ctx:
            self.bin_edges.initialize()
            self.bin_centers.initialize()
        self.bin_edges.set_data(mx.nd.array(bin_edges))
        self.bin_centers.set_data(mx.nd.array(bin_centers))
コード例 #11
0
 def initialize_from_array(self,
                           input_array: np.ndarray,
                           ctx: mx.Context = get_mxnet_context()):
     for representation in self.representations:
         representation.initialize_from_array(input_array, ctx)
コード例 #12
0
 def initialize_from_dataset(self,
                             input_dataset: Dataset,
                             ctx: mx.Context = get_mxnet_context()):
     for representation in self.representations:
         representation.initialize_from_dataset(input_dataset, ctx)
コード例 #13
0
ファイル: _base.py プロジェクト: vishalbelsare/gluon-ts
    def __init__(
        self,
        ctx: Optional[mx.Context] = None,
        epochs: int = 100,
        batch_size: Optional[int] = None,
        num_batches_per_epoch: int = 50,
        learning_rate: float = 1e-3,
        learning_rate_decay_factor: float = 0.5,
        patience: int = 10,
        minimum_learning_rate: float = 5e-5,
        clip_gradient: float = 10.0,
        weight_decay: float = 1e-8,
        init: Union[str, mx.initializer.Initializer] = "xavier",
        hybridize: bool = True,
        callbacks: Optional[List[Callback]] = None,
        add_default_callbacks: bool = True,
    ) -> None:

        if batch_size is not None:
            warnings.warn(
                "batch_size argument is deprecated",
                DeprecationWarning,
                stacklevel=2,
            )
        else:
            batch_size = 32

        assert isinstance(batch_size, int)

        # TODO param disable_default_callbacks to get backwards compatibility
        # deprecation warnings, in the future, the following callbacks should be
        # controlled by altering callbacks:
        if learning_rate_decay_factor is not None:
            warnings.warn(
                'Trainer argument "learning_rate_decay_factor" is deprecated. Use callbacks instead.',
                DeprecationWarning,
            )
            assert (
                0 <= learning_rate_decay_factor < 1
            ), "The value of `learning_rate_decay_factor` should be in the [0, 1) range"
        if patience is not None:
            warnings.warn(
                'Trainer argument "patience" is deprecated. Use callbacks instead.',
                DeprecationWarning,
            )
            assert 0 <= patience, "The value of `patience` should be >= 0"
        if minimum_learning_rate:
            warnings.warn(
                'Trainer argument "minimum_learning_rate" is deprecated. Use callbacks instead.',
                DeprecationWarning,
            )
            assert (0 <= minimum_learning_rate
                    ), "The value of `minimum_learning_rate` should be >= 0"

        assert (0 <= epochs <
                float("inf")), "The value of `epochs` should be >= 0"
        assert 0 < batch_size, "The value of `batch_size` should be > 0"
        assert (0 < num_batches_per_epoch
                ), "The value of `num_batches_per_epoch` should be > 0"
        assert (0 < learning_rate <
                float("inf")), "The value of `learning_rate` should be > 0"

        assert 0 < clip_gradient, "The value of `clip_gradient` should be > 0"
        assert 0 <= weight_decay, "The value of `weight_decay` should be => 0"

        self.epochs = epochs
        self.batch_size = batch_size
        self.num_batches_per_epoch = num_batches_per_epoch
        self.learning_rate = learning_rate
        self.learning_rate_decay_factor = learning_rate_decay_factor
        self.patience = patience
        self.minimum_learning_rate = minimum_learning_rate
        self.clip_gradient = clip_gradient
        self.weight_decay = weight_decay
        self.init = init
        self.hybridize = hybridize
        self.ctx = ctx if ctx is not None else get_mxnet_context()
        self.halt = False

        # Make sure callbacks is list -- they are assigned to `self.callbacks`
        # below
        callbacks = callbacks or []

        # TODO the following is done for backwards compatibility. For future
        # versions, add the default callbacks as default arg
        if add_default_callbacks:
            default_callbacks = [
                ModelAveraging(avg_strategy=SelectNBestMean(num_models=1)),
                LearningRateReduction(
                    base_lr=learning_rate,
                    decay_factor=learning_rate_decay_factor,
                    patience=patience,
                    min_lr=minimum_learning_rate,
                    objective="min",
                ),
            ]
            self.callbacks = CallbackList(callbacks + default_callbacks)
        else:
            self.callbacks = CallbackList(callbacks)
コード例 #14
0
 def initialize_from_array(
     self, input_array: np.ndarray, ctx: mx.Context = get_mxnet_context()
 ):
     with ctx:
         self.bin_edges.initialize()
         self.bin_centers.initialize()
コード例 #15
0
 def initialize_from_dataset(
     self, input_dataset: Dataset, ctx: mx.Context = get_mxnet_context()
 ):
     self.initialize_from_array(np.array([]), ctx)