Len works on numpy
from numpy import array
vector = array([1, 2, 3])
len(vector) # 3
matrix = array([
[1, 2],
[3, 2],
[5, 4],
])
len(matrix) # 3
But not with pt.as_tensor_variable
from pytensor.tensor import as_tensor_variable as array
# Same as above
which results in:
[ins] In [100,000,000,000,000]: matrix
Out[100,000,000,000,000]: TensorConstant(TensorType(int64, shape=(3, 2)), data=array([[1, ... [5, 4]]))
[ins] In [100,000,000,000,001]: len(matrix)
----------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[100,000,000,000,001], line 1
----> 1 len(matrix)
TypeError: object of type 'TensorConstant' has no len()
My usecase
import numpy as np
import pytensor.tensor as pt
import pymc as pm
def build_model(y) -> pm.Model:
coords = {
"idx": np.arange(len(y))
}
model = pm.Model(coords=coords)
with model:
...
return model
model = build_model([1, 2, 3])
model = build_model(np.array([1, 2, 3]))
# Failure as above
model = build_model(pt.as_tensor_variable([1, 2, 3]))
Len works on numpy
But not with pt.as_tensor_variable
which results in:
My usecase