shared - defines pytensor.shared#
- class pytensor.compile.sharedvalue.SharedVariable#
Variable with storage that is shared between the compiled functions that it appears in. These variables are meant to be created by registered shared constructors (see
The user-friendly constructor is
- get_value(self, borrow=False, return_internal_type=False)#
borrow (bool) – True to permit returning of an object aliased to internal memory.
return_internal_type (bool) – True to permit the returning of an arbitrary type object used internally to store the shared variable.
By default, return a copy of the data. If
return_internal_type=False), maybe it will return a copy. For tensor, it will always return an
ndarrayby default, so if the data is on another device, it will return a copy, but if the data is on the CPU, it will return the original data. If you do
return_internal_type=True, it will always return the original data, not a copy, but this can be a non-
ndarraytype of object.
- set_value(self, new_value, borrow=False)#
new_value (A compatible type for this shared variable.) – The new value.
borrow (bool) – True to use the new_value directly, potentially creating problems related to aliased memory.
The new value will be seen by all functions using this SharedVariable.
- __init__(self, name, type, value, strict, container=None)#
name (None or str) – The name for this variable.
type – The Type for this Variable.
value – A value to associate with this variable (a new container will be created).
strict – True -> assignments to
self.valuewill not be casted or copied, so they must have the correct type or an exception will be raised.
container – The container to use for this variable. This should instead of the
valueparameter. Using both is an error.
A container to use for this SharedVariable when it is an implicit function parameter.
- pytensor.compile.sharedvalue.shared(value, name=None, strict=False, allow_downcast=None, **kwargs)#
SharedVariableinitialized with a copy or reference of
This function iterates over constructor functions to find a suitable
SharedVariablesubclass. The suitable one is the first constructor that accept the given value. See the documentation of
shared_constructor()for the definition of a constructor function.
This function is meant as a convenient default. If you want to use a specific constructor, consider calling it directly.
pytensor.sharedis a shortcut to this function.
By passing kwargs, you effectively limit the set of potential constructors to those that can accept those kwargs.
Some shared variable have
borrowas a kwarg.
broadcastableas a kwarg. As shared variable shapes can change, all dimensions default to not being broadcastable, even if
valuehas a shape of 1 along some dimension. This parameter allows one to create for example a row or column tensor.