tensor.utils – Tensor Utils#


Convert x to a list if it is an iterable; otherwise, wrap it in a list.

pytensor.tensor.utils.broadcast_static_dim_lengths(dim_lengths: Sequence[int | None]) int | None[source]#

Apply static broadcast given static dim length of inputs (obtained from var.type.shape).


ValueError – When static dim lengths are incompatible


Return a hash from an ndarray.

It takes care of the data, shapes, strides and dtype.

pytensor.tensor.utils.shape_of_variables(fgraph, input_shapes)[source]#

Compute the numeric shape of all intermediate variables given input shapes.

  • fgraph – The FunctionGraph in question.

  • input_shapes (dict) – A dict mapping input to shape.


  • shapes (dict) – A dict mapping variable to shape

  • .. warning:: This modifies the fgraph. Not pure.


>>> import pytensor
>>> x = pytensor.tensor.matrix('x')
>>> y = x[512:]; y.name = 'y'
>>> fgraph = FunctionGraph([x], [y], clone=False)
>>> d = shape_of_variables(fgraph, {x: (1024, 1024)})
>>> d[y]
(array(512), array(1024))
>>> d[x]
(array(1024), array(1024))