gluonts.core.component module

class gluonts.core.component.BaseValidatedInitializerModel[source]

Bases: pydantic.main.BaseModel

Base Pydantic model for components with validated() initializers.

See also

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]
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.

Returns

A boolean value indicating whether this and that are structurally equal.

Return type

bool

See also

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.

Returns

A boolean value indicating whether this and that are structurally equal.

Return type

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.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.

Returns

An instance of the given class.

Return type

A

Raises

GluonTSHyperparametersError – Wraps a ValidationError thrown when validating the initializer parameters.

gluonts.core.component.skip_encoding(v: Any) → bool[source]

Tells whether the input value v should be encoded using the encode() function.

This is used by validated() to determine which values need to be skipped when recording the initializer arguments for later serialization.

This is the fallback implementation, and can be specialized for specific types by registering handler functions.

gluonts.core.component.tensor_to_numpy(tensor) → numpy.ndarray[source]
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 for ComplexNumberModel
y
  none is not an allowed value (type=type_error.none.not_allowed)

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

>>> ComplexNumber.__init__.Model
<class 'pydantic.main.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.

See also

BaseValidatedInitializerModel()

Default base class for all synthesized Pydantic models.