aloctavodia / BAP

Bayesian Analysis with Python (Second Edition)
https://www.amazon.com/dp/B07HHBCR9G
MIT License
646 stars 250 forks source link

Softmax function in aesara #86

Closed rm10075 closed 1 year ago

rm10075 commented 1 year ago

Hi,

I was trying to run the code for the Softmax regression example in Chapter 4 using the Aesara (rather than Theano) Softmax function. It looked like the aesara.tensor.nnet subpackage had been removed but I was hoping this might do the trick but was getting a "NotImplementedError: Cannot convert μ to a tensor variable." upon running the following code:

with pm.Model() as model_s: α = pm.Normal('α', mu=0, sigma=5, shape=3) β = pm.Normal('β', mu=0, sigma=5, shape=(4,3)) μ = pm.Deterministic('μ', α + pm.math.dot(x_s, β)) θ = aesara.tensor.special.softmax(μ) yl = pm.Categorical('yl', p=θ, observed=y_s) idata_s = pm.sample(2000, target_accept=0.9, return_inferencedata=True)

Any suggestions greatly appreciated.

Thanks,

Rich

rm10075 commented 1 year ago

Please ignore, fixed by using PyTensor instead of theano:

with pm.Model() as model_s: α = pm.Normal('α', mu=0, sigma=5, shape=3) β = pm.Normal('β', mu=0, sigma=5, shape=(4,3)) μ = pm.Deterministic('μ', α + pm.math.dot(x_s, β)) θ = pytensor.tensor.special.softmax(μ) yl = pm.Categorical('yl', p=θ, observed=y_s) idata_s = pm.sample(2000, target_accept=0.9, return_inferencedata=True)

aloctavodia commented 1 year ago

glad you solve it!