How can I get test_value in PyMC(PyMC4)?

I am a newbie in Bayesian and Probabilistic inference, and sorry for this basic question. Recently I am following some examples in Bayesian Methods. And, the examples require me to use “tag.test_value.” However, I am trying to use PyMC rather than PyMC3, so there is an error using the sentence. Although I tried to use others such as init_value, initial_value, it does not work…

Could you kindly let me know alternatives for that sentence to check the initial value in PyMC (that was originally test value in PyMC3)?

a = pm.Uniform(“b”, 0, 50)
print(a.tag.test_value)

AttributeError: ‘ValidatingScratchpad’ object has no attribute 'test_value`

You’re looking for pm.Model.initial_point.

But, I am still getting an error when I used pm.model.initial_point.

with model:
a = pm.Uniform(‘c’, 0, 50)
print(a.initial_point)

AttributeError: ‘TensorVariable’ object has no attribute ‘initial_point’

model.initial_point, not a.initial_point.

1 Like

I am sorry to bother you. But, in my case, it does not work. To be more specific, I was facing this kind of problem.

I want to get initial values from my each model (however, model.initial_point gives me an address of initial values.)

Even though I use the initial_values

import pymc as pm
import aesara.tensor as at
with pm.Model() as model:

p1 = pm.Uniform(‘p’, 0, 1)
p2 = 1 - p1
p = at.stack([p1, p2]) # Vectorize p1 and p2.
print(type(p))
print(np.shape(p))

assignment = pm.Categorical(“assignment”,
p,
shape = data.shape[0],
testval = np.random.randint(0,2,data.shape[0]))

print(“prior assignment, with p = %.2f:” % p1.tag.test_value)
print(assignment.tag.test_value[:10])

A result is:

AttributeError: ‘ValidatingScratchpad’ object has no attribute ‘test_value’

Even though I add some lines,

print(model.initial_point)
print(model.initial_values)

<bound method Model.initial_point of <pymc.model.Model object at 0x14e9e2410>>
{p: None, assignment: array([0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1,
1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1,
0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1,
1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1,
0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0,
0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1,
1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1,
0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0,
0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1,
1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1,
1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0,
0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1,
0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1])}

Since I need to get a single real number (0.5) from p1.tag.test_value in PyMC3, model.initial_point or model.initial_values do not work in this case…

How can I exactly get a test value from a distribution?

This is what I get:

In [33]: model.initial_point()
Out[33]: 
{'p_interval__': array(0.),
 'assignment': array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])}

This provides the test value of p1 (or p, you have 2 names for that parameter) in the transformed space (it’s transformed because uniform-distributed parameters are bound).

1 Like

Given your question I think @cluhmann 's last post may be the correct approach, but maybe what your asking for is how to evaluate the aesara tensors? Maybe something like this:

import pymc as pm
import aesara.tensor as at

with pm.Model() as model:
    p1 = pm.Uniform("p", 0, 1)
    p2 = 1 - p1
    p = at.stack([p1, p2]) # Vectorize p1 and p2.
    
p.eval()
Out[4]: array([0.212052, 0.787948])

p1.eval()
Out[5]: array(0.212052)

I couldn’t get it quite clear form your last post, but is this what you had in mind?

2 Likes

Oh, maybe I missed () for model.initial_point(). Now it works when I access to the test values in this way.

model.initial_point()[“assignment”][:10]

array([1, 1, 1, 0, 0, 0, 0, 1, 0, 1])

Thank you so much.

@cluhmann was right. I missed () in my line. I am sorry that I am totally a newbie in this area. But, eval() might be different from what I looked for. Thanks for your comment :slight_smile:

1 Like

I think I accidentally omitted them (the parentheses) above. Glad you got it worked out!

1 Like