Save and reuse model with shared variables

Let’s say I have a simple model with shared variables to make out-of-sample predictions:

N=5000
p=2
batch_size=100
X=np.random.normal(0,1,(N,p))
b=np.random.uniform(0,0.1,p)
y=np.random.poisson(np.exp(X.dot(b)),N)

model = pm.Model()
with model:

x0_s=shared(X[:,0])
x1_s=shared(X[:,1])

b0=pm.Normal('b0',mu=0,sd=.1)
b1=pm.Normal('b1',mu=0,sd=.1)
mu=pm.math.exp(b0*x0_s+b1*x1_s)
y_=pm.Poisson('Y_obs',mu=mu,observed=y)

approx=pm.fit(10000,method='ADVI')
trace=approx.sample(1000)

I need to save the fitted model and then to make prediction in a separate session.
I can easily pickle the trace and the model but how can I manage the shared variables?
One solution would be to redefine the model in the new session so that I have access to the shared variables but I would like to avoid that.
Any suggestions?

Thank you.

Did you try pickling them?

Save them with the approx variable and set the values before you call approx.sample(). For me this works just fine.

x0_s.set_value(newX0)
x1_s.set_value(newX1)
trace = approx.sample(1000)
1 Like

You are right! The easiest solution is the one that works :slight_smile:

Thank you.