AttributeError: 'numpy.dtype' object has no attribute 'base_dtype'

Dear all, just a very small question:

The following code gives an arror AttributeError: 'numpy.dtype' object has no attribute 'base_dtype', but because I am new in TF, I am not sure about the mistake. Maybe you could point it out fast.

t = np.array(np.arange(1,13,1),dtype='int64')
@pm.model
def lt_model():
    Δ = yield pm.Laplace(loc=0, scale=4, batch_stack=5, name='Δ')
    g = tf.math.scalar_mul(Δ,t)
    
trace = pm.inference.sampling.sample(
        lt_model(), num_chains=2, num_samples=10, burn_in=10, step_size=1., xla=True)

as I understood the shape parameter is now given by batch_stack (or by plate)?

Thank you in advance

Hi @aakhmetz
You can convert the numpy.ndarray to Tensor before passing to scalar_mul function.

t = np.array(np.arange(1,13,1),dtype='int64')
t = tf.convert_to_tensor(value=t, dtype='float32')
@pm.model
def lt_model():
    Δ = yield pm.Laplace(loc=0, scale=4, batch_stack=5, name='Δ')
    g = tf.math.scalar_mul(Δ,t)

I think dtype should also be changed to float32 as the distributions in tfp are more compatible with floats.

1 Like

@Sayam753’s answer makes the base_dtype error go but I doubt the shapes are compatible.

\Delta has a shape (5, ) while the tf.math.scalar_mul only accepts scalar values. Are you sure you want to do a scalar multiplication? If not, you will still face an error because \Delta and t don’t have broadcastable shapes. Maybe, you should consider resolving that too! I hope this helps…

1 Like