pytensor
pytensor copied to clipboard
Sketch of dim-ed tensors
import numpy as np
from pytensor import function
from pytensor.xtensor.basic import add, exp
from pytensor.xtensor.type import xtensor
x = xtensor("x", dims=("city",), shape=(None,))
y = xtensor("y", dims=("country",), shape=(4,))
z = add(exp(x), exp(y))
assert z.type.dims == ("city", "country")
assert z.type.shape == (None, 4)
fn = function([x, y], z)
fn.dprint(print_type=True)
# XTensorFromTensor{dims=('city', 'country')} [id A] <XTensorType{dtype='float64', shape=(None, 4), dims=('city', 'country')}> 7
# └─ Add [id B] <Matrix(float64, shape=(?, 4))> 6
# ├─ Exp [id C] <Matrix(float64, shape=(?, 1))> 5
# │ └─ ExpandDims{axis=1} [id D] <Matrix(float64, shape=(?, 1))> 3
# │ └─ TensorFromXTensor [id E] <Vector(float64, shape=(?,))> 1
# │ └─ x [id F] <XTensorType{dtype='float64', shape=(None,), dims=('city',)}>
# └─ Exp [id G] <Matrix(float64, shape=(1, 4))> 4
# └─ ExpandDims{axis=0} [id H] <Matrix(float64, shape=(1, 4))> 2
# └─ TensorFromXTensor [id I] <Vector(float64, shape=(4,))> 0
# └─ y [id J] <XTensorType{dtype='float64', shape=(4,), dims=('country',)}>
np.testing.assert_allclose(
fn(x=np.zeros(3), y=np.zeros(4)),
np.full((3, 4), 2.0),
)