xtensor – Module level operations#

pytensor.xtensor.broadcast(*args, exclude=None)[source]#

Broadcast any number of XTensorVariables against each other.

Parameters:
  • *args (XTensorVariable) – The tensors to broadcast against each other.

  • exclude (str or Sequence[str] or None, optional) –

pytensor.xtensor.concat(xtensors, dim)[source]#

Concatenate a sequence of XTensorVariables along a specified dimension.

Parameters:
  • xtensors (Sequence of XTensorVariable) – The tensors to concatenate.

  • dim (str) – The dimension along which to concatenate the tensors.

Return type:

XTensorVariable

Example

from pytensor.xtensor import as_xtensor, xtensor, concat

x = xtensor("x", shape=(2, 3), dims=("a", "b"))
zero = as_xtensor([0], dims=("a"))

out = concat([zero, x, zero], dim="a")
assert out.type.dims == ("a", "b")
assert out.type.shape == (4, 3)
pytensor.xtensor.dot(x, y, dim=None)[source]#

Matrix multiplication between two XTensorVariables.

This operation performs multiplication followed by summation for shared dimensions or simply summation for non-shared dimensions.

Parameters:
  • x (XTensorVariable) – First input tensor

  • y (XTensorVariable) – Second input tensor

  • dim (str, Iterable[AsDim], EllipsisType, or None, optional) – The dimensions to contract over. If None, will contract over all matching dimensions. If Ellipsis (…), will contract over all dimensions.

Return type:

XTensorVariable

Examples

from pytensor.xtensor import xtensor, dot

x = xtensor("x", dims=("a", "b"))
y = xtensor("y", dims=("b", "c"))

assert dot(x, y).dims == ("a", "c")  # Contract over shared `b` dimension
assert dot(x, y, dim=("a", "b")).dims == ("c",)  # Contract over 'a' and 'b'
assert dot(x, y, dim=...).dims == ()  # Contract over all dimensions
pytensor.xtensor.full_like(x, fill_value, dtype=None)[source]#

Create a new XTensorVariable with the same shape and dimensions, filled with a specified value.

Parameters:
  • x (XTensorVariable) – The tensor to fill.

  • fill_value (scalar or XTensorVariable) – The value to fill the new tensor with.

  • dtype (str or np.dtype, optional) – The data type of the new tensor. If None, uses the dtype of the input tensor.

Returns:

A new tensor with the same shape and dimensions as self, filled with fill_value.

Return type:

XTensorVariable

Examples

>>> from pytensor.xtensor import xtensor, full_like
>>> x = xtensor(dtype="float64", dims=("a", "b"), shape=(2, 3))
>>> y = full_like(x, 5.0)
>>> assert y.dims == ("a", "b")
>>> assert y.type.shape == (2, 3)
pytensor.xtensor.ones_like(x, dtype=None)[source]#

Create a new XTensorVariable with the same shape and dimensions, filled with ones.

Parameters:
  • x (XTensorVariable) – The tensor to fill.

  • dtype (str or np.dtype, optional) – The data type of the new tensor. If None, uses the dtype of the input tensor.

  • Returns

  • XTensorVariable – A new tensor with the same shape and dimensions as self, filled with ones.

Examples

>>> from pytensor.xtensor import xtensor, full_like
>>> x = xtensor(dtype="float64", dims=("a", "b"), shape=(2, 3))
>>> y = ones_like(x)
>>> assert y.dims == ("a", "b")
>>> assert y.type.shape == (2, 3)
pytensor.xtensor.zeros_like(x, dtype=None)[source]#

Create a new XTensorVariable with the same shape and dimensions, filled with zeros.

Parameters:
  • x (XTensorVariable) – The tensor to fill.

  • dtype (str or np.dtype, optional) – The data type of the new tensor. If None, uses the dtype of the input tensor.

  • Returns

  • XTensorVariable – A new tensor with the same shape and dimensions as self, filled with zeros.

Examples

>>> from pytensor.xtensor import xtensor, full_like
>>> x = xtensor(dtype="float64", dims=("a", "b"), shape=(2, 3))
>>> y = zeros_like(x)
>>> assert y.dims == ("a", "b")
>>> assert y.type.shape == (2, 3)