type – Interface for types of variables#
Reference#
- class pytensor.graph.type.Type[source]#
Interface specification for variable type instances.
A Type instance is mainly responsible for two things:
creating
Variableinstances (conventionally,__call__does this), andfiltering a value assigned to a
Variableso that the value conforms to restrictions imposed by the type (also known as casting, this is done byfilter).
- clone(*args, **kwargs)[source]#
Clone a copy of this type with the given arguments/keyword values, if any.
- constant_type[source]#
The
Typethat will be created by a call toType.make_constant.alias of
Constant
- convert_variable(var)[source]#
Produce a
Variablethat’s compatible with bothselfandvar.type, if possible.A compatible
Variableis aVariablewith aTypethat’s the “narrower” ofselfandvar.type.If a compatible
Typecannot be found, this method will returnNone.
- abstract filter(data, strict=False, allow_downcast=None)[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
strictisFalse, andallow_downcastisTrue, the data may be cast to an appropriate type. Ifallow_downcastisFalse, it may only be up-cast and not lose precision. Ifallow_downcastisNone(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, storage, strict=False, allow_downcast=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, allow_convert=True)[source]#
Convert a
otherinto aVariablewith aTypethat’s compatible withself.If the involved
Types are not compatible, aTypeErrorwill be raised.
- in_same_class(otype)[source]#
Determine if another
Typerepresents a subset from the same “class” of types represented byself.A “class” of types could be something like “float64 tensors with four dimensions”. One
Typecould 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
Typeto 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)[source]#
Determine if
selfis a supertype ofotype.This method effectively implements the type relation
>.In general,
t1.is_super(t2) == Trueimplies thatt1can be replaced witht2.See
Type.in_same_class.- Return type:
Noneif the type relation cannot be applied/determined.
- is_valid_value(data, strict=True)[source]#
Return
Truefor any python object that would be a legal value for aVariableof thisType.
- make_constant(value, name=None)[source]#
Return a new
Constantinstance of thisType.- Parameters:
value (array-like) – The constant value.
name (None or str) – A pretty string for printing and debugging.
- make_variable(name=None)[source]#
Return a new
Variableinstance of thisType.- Parameters:
name (None or str) – A pretty string for printing and debugging.
- classmethod values_eq(a, b)[source]#
Return
Trueifaandbcan be considered exactly equal.aandbare assumed to be valid values of thisType.
- classmethod values_eq_approx(a, b)[source]#
Return
Trueifaandbcan 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.
- variable_type[source]#
The
Typethat will be created by a call toType.make_variable.alias of
Variable