type – Interface for types of variables#

Reference#

class pytensor.graph.type.HasDataType[source]#

A mixin for a type that has a dtype attribute.

class pytensor.graph.type.HasShape[source]#

A mixin for a type that has shape and ndim attributes.

class pytensor.graph.type.Type[source]#

Interface specification for variable type instances.

A Type instance is mainly responsible for two things:

  • creating Variable instances (conventionally, __call__ does this), and

  • filtering a value assigned to a Variable so that the value conforms to restrictions imposed by the type (also known as casting, this is done by filter).

clone(*args, **kwargs) Type[source]#

Clone a copy of this type with the given arguments/keyword values, if any.

constant_type[source]#

The Type that will be created by a call to Type.make_constant.

alias of Constant

convert_variable(var: Variable) pytensor.graph.basic.Variable | None[source]#

Produce a Variable that’s compatible with both self and var.type, if possible.

A compatible Variable is a Variable with a Type that’s the “narrower” of self and var.type.

If a compatible Type cannot be found, this method will return None.

abstract filter(data: Any, strict: bool = False, allow_downcast: Optional[bool] = None) D[source]#

Return data or an appropriately wrapped/converted data.

Subclass implementations should raise a TypeError exception if the data is not of an acceptable type.

Parameters:
  • data (array-like) – The data to be filtered/converted.

  • strict (bool (optional)) – If True, the data returned must be the same as the data passed as an argument.

  • allow_downcast (bool (optional)) – If strict is False, and allow_downcast is True, the data may be cast to an appropriate type. If allow_downcast is False, it may only be up-cast and not lose precision. If allow_downcast is None (default), the behaviour can be type-dependent, but for now it means only Python floats can be down-casted, and only to floatX scalars.

filter_inplace(value: Any, storage: Any, strict: bool = False, allow_downcast: Optional[bool] = None)[source]#

Return data or an appropriately wrapped/converted data by converting it in-place.

This method allows one to reuse old allocated memory. If this method is implemented, it will be called instead of Type.filter.

As of now, this method is not implemented and was previously used for transferring memory to and from GPU.

Parameters:
  • value (array-like) –

  • storage (array-like) – The old value (e.g. the old NumPy array)

  • strict (bool) –

  • allow_downcast (bool (optional)) –

Raises:

NotImplementedError

filter_variable(other: Union[Variable, D], allow_convert: bool = True) Variable[source]#

Convert a other into a Variable with a Type that’s compatible with self.

If the involved Types are not compatible, a TypeError will be raised.

in_same_class(otype: Type) bool | None[source]#

Determine if another Type represents a subset from the same “class” of types represented by self.

A “class” of types could be something like “float64 tensors with four dimensions”. One Type could represent a set containing only a type for “float64 tensors with shape (1, 2, 3, 4)” and another the set of “float64 tensors with shape (1, x, x, x)” for all suitable “x”.

It’s up to each subclass of Type to determine to which “classes” of types this method applies.

The default implementation assumes that all “classes” have only one unique element (i.e. it uses self.__eq__).

is_super(otype: Type) bool | None[source]#

Determine if self is a supertype of otype.

This method effectively implements the type relation >.

In general, t1.is_super(t2) == True implies that t1 can be replaced with t2.

See Type.in_same_class.

Return type:

None if the type relation cannot be applied/determined.

is_valid_value(data: D, strict: bool = True) bool[source]#

Return True for any python object that would be a legal value for a Variable of this Type.

make_constant(value: D, name: Optional[str] = None) Constant[source]#

Return a new Constant instance of this Type.

Parameters:
  • value (array-like) – The constant value.

  • name (None or str) – A pretty string for printing and debugging.

make_variable(name: Optional[str] = None) Variable[source]#

Return a new Variable instance of this Type.

Parameters:

name (None or str) – A pretty string for printing and debugging.

classmethod values_eq(a: D, b: D) bool[source]#

Return True if a and b can be considered exactly equal.

a and b are assumed to be valid values of this Type.

classmethod values_eq_approx(a: D, b: D) bool[source]#

Return True if a and b can be considered approximately equal.

This function is used by PyTensor debugging tools to decide whether two values are equivalent, admitting a certain amount of numerical instability. For example, for floating-point numbers this function should be an approximate comparison.

By default, this does an exact comparison.

Parameters:
  • a (array-like) – A potential value for a Variable of this Type.

  • b (array-like) – A potential value for a Variable of this Type.

Return type:

bool

variable_type[source]#

The Type that will be created by a call to Type.make_variable.

alias of Variable