defines pytensor.shared — PyTensor dev documentation (original) (raw)

class pytensor.compile.sharedvalue.SharedVariable[source]#

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 shared_constructor()).

The user-friendly constructor is shared()

get_value(self, borrow=False, return_internal_type=False)[source]#

Parameters:

By default, return a copy of the data. If borrow=True (andreturn_internal_type=False), maybe it will return a copy. For tensor, it will always return an ndarray by 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 doborrow=True and 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)[source]#

Parameters:

The new value will be seen by all functions using this SharedVariable.

__init__(self, name, type, value, strict, container=None)[source]#

Parameters:

container[source]#

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)[source]#

Create a SharedVariable initialized with a copy or reference of value.

This function iterates over constructor functions to find a suitable SharedVariable subclass. The suitable one is the first constructor that accept the given value. See the documentation ofshared_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.shared is a shortcut to this function.

Notes

By passing kwargs, you effectively limit the set of potential constructors to those that can accept those kwargs.

Some shared variable have borrow as a kwarg.

SharedVariables of TensorType have broadcastable as a kwarg. As shared variable shapes can change, all dimensions default to not being broadcastable, even if value has a shape of 1 along some dimension. This parameter allows one to create for example a row or column tensor.

pytensor.compile.sharedvalue.shared_constructor(ctor)[source]#

Append ctor to the list of shared constructors (see shared()).

Each registered constructor ctor will be called like this:

ctor(value, name=name, strict=strict, **kwargs)

If it do not support given value, it must raise a TypeError.