# gluonts.core.component module¶

class gluonts.core.component.BaseValidatedInitializerModel[source]

Bases: pydantic.main.BaseModel

Base Pydantic model for components with validated() initializers.

validated
Decorates an initializer methods with argument validation logic.
class Config[source]

Bases: pydantic.main.BaseConfig

Config for the Pydantic model inherited by all validated() initializers.

Allows the use of arbitrary type annotations in initializer parameters.

arbitrary_types_allowed = True
class gluonts.core.component.DType[source]

Bases: object

Defines custom data type validation for type instances.

Parameters annotated with DType can be bound to string arguments representing the fully-qualified type name. The validation logic defined here attempts to automatically load the type as part of the conversion process.

classmethod validate(v)[source]
class gluonts.core.component.MXContext[source]

Bases: object

Defines custom data type validation for the Context data type.

classmethod validate(v: Union[str, mxnet.context.Context]) → mxnet.context.Context[source]
gluonts.core.component.check_gpu_support() → None[source]

Emits a log line that indicates whether the currently installed MXNet version has GPU support.

gluonts.core.component.equals(this: Any, that: Any) → bool[source]

Structural equality check between two objects of arbitrary type.

By default, this function delegates to equals_default_impl().

In addition, the function dispatches to specialized implementations based on the type of the first argument, so the above conditions might be sticter for certain types.

Parameters: that (this,) – Objects to compare. A boolean value indicating whether this and that are structurally equal. bool

equals_default_impl()
Default semantics of a structural equality check between two objects of arbitrary type.
equals_representable_block()
Specialization for Gluon HybridBlock input arguments.
equals_parameter_dict()
Specialization for Gluon ParameterDict input arguments.
gluonts.core.component.equals_default_impl(this: Any, that: Any) → bool[source]

Default semantics of a structural equality check between two objects of arbitrary type.

Two objects this and that are defined to be structurally equal if and only if the following criteria are satisfied:

1. Their types match.
2. If their initializer are validated(), their initializer arguments are pairlise structurally equal.
3. If their initializer are not validated(), they are referentially equal (i.e. this == that).
Parameters: that (this,) – Objects to compare. A boolean value indicating whether this and that are structurally equal. bool
gluonts.core.component.equals_dict(this: dict, that: dict) → bool[source]
gluonts.core.component.equals_list(this: list, that: list) → bool[source]
gluonts.core.component.equals_ndarray(this: numpy.ndarray, that: numpy.ndarray) → bool[source]
gluonts.core.component.equals_parameter_dict(this: mxnet.gluon.parameter.ParameterDict, that: mxnet.gluon.parameter.ParameterDict) → bool[source]

Structural equality check between two ParameterDict objects.

Two parameter dictionaries this and that are considered structurally equal if the following conditions are satisfied:

1. They contain the same keys (modulo the key prefix which is stripped).
2. The data in the corresponding value pairs is equal, as defined by the almost_equal() function (in this case we call the function with equal_nan=True, that is, two aligned NaN values are always considered equal).

Specializes equals() for invocations where the first parameter is an instance of the ParameterDict class.

Parameters: that (this,) – Objects to compare. A boolean value indicating whether this and that are structurally equal. bool

equals()
Dispatching function.
gluonts.core.component.equals_representable_block(this: mxnet.gluon.block.HybridBlock, that: mxnet.gluon.block.HybridBlock) → bool[source]

Structural equality check between two HybridBlock objects with validated() initializers.

Two blocks this and that are considered structurally equal if all the conditions of equals() are met, and in addition their parameter dictionaries obtained with collect_params() are also structurally equal.

Specializes equals() for invocations where the first parameter is an instance of the HybridBlock class.

Parameters: that (this,) – Objects to compare. A boolean value indicating whether this and that are structurally equal. bool

equals()
Dispatching function.
equals_parameter_dict()
Specialization of equals() for Gluon ParameterDict input arguments.
gluonts.core.component.from_hyperparameters(cls: Type[A], **hyperparameters) → A[source]

Reflectively create an instance of a class with a validated() initializer.

Parameters: cls – The type A of the component to be instantiated. hyperparameters – A dictionary of key-value pairs to be used as parameters to the component initializer. An instance of the given class. A GluonTSHyperparametersError – Wraps a ValidationError thrown when validating the initializer parameters.
gluonts.core.component.get_mxnet_context() → mxnet.context.Context[source]

Returns either CPU or GPU context

gluonts.core.component.validated(base_model=None)[source]

Decorates an __init__ method with typed parameters with validation and auto-conversion logic.

>>> class ComplexNumber:
...     @validated()
...     def __init__(self, x: float = 0.0, y: float = 0.0) -> None:
...         self.x = x
...         self.y = y


Classes with decorated initializers can be instantiated using arguments of another type (e.g. an y argument of type str ). The decorator handles the type conversion logic.

>>> c = ComplexNumber(y='42')
>>> (c.x, c.y)
(0.0, 42.0)


If the bound argument cannot be converted, the decorator throws an error.

>>> c = ComplexNumber(y=None)
Traceback (most recent call last):
...
pydantic.error_wrappers.ValidationError: 1 validation error
y
value is not a valid float (type=type_error.float)


Internally, the decorator delegates all validation and conversion logic to a Pydantic model, which can be accessed through the Model attribute of the decorated initiazlier.

>>> ComplexNumber.__init__.Model
<class 'ComplexNumberModel'>


The Pydantic model is synthesized automatically from on the parameter names and types of the decorated initializer. In the ComplexNumber example, the synthesized Pydantic model corresponds to the following definition.

>>> class ComplexNumberModel(BaseValidatedInitializerModel):
...     x: float = 0.0
...     y: float = 0.0


Clients can optionally customize the base class of the synthesized Pydantic model using the base_model decorator parameter. The default behavior uses BaseValidatedInitializerModel and its model config.

BaseValidatedInitializerModel()