3

I'm trying to run a baysian calibration of a dynamic model with PYMC3.

My model function includes a large time loop, thus when the model is compiled with Theano, the RAM memory exceeds the capacity. For this problem I found how to call the function as a blackbox. However is there any other way to handle this problem without using the blackbox method ?

here an illustration :


with pm.Model():

    # DECLARE PRIOR
    var1 = pm.Uniform('var1', lower=0.5, upper=1.5)
    var2 = pm.Uniform('var2', lower=0.5, upper=1.5)
    var3 = pm.Uniform('var3', lower=0.5, upper=1.5)
    var4 = pm.Uniform('var4', lower=0.5, upper=1.5)

    theta = tt.as_tensor_variable([var1, var2, var3,var4])

    # DECLARE OBJECTIVE FUNCTION
    mu = pm.Deterministic('mu', tt.as_tensor_variable(my_model(theta,x)))
    sigma = pm.HalfNormal('sigma', 0.15)
    y_obs = pm.Normal('y_obs', mu=mu, sd=sigma, observed=data)

    step = pm.Metropolis()
    trace = pm.sample(ndraws, tune=nburn, njobs=njobs, step=step)
```

my_model is a code using mainly numpy and pandas and including large time loops such : 
for t in range(1,1000):
      x=...

Marty
  • 53
  • 4
  • 1
    Hi @Marty, I think it will be hard to address your question without seeing the code specifically because you are asking how to optimize a `for` loop to work efficiently within a model. Generally, `for` loops in a model cause issues with creating too many nodes in the compute graph, because each operation that is applied to a `TensorVariable` gets added as a new node in the compute graph. E.g., possibly related question: https://stackoverflow.com/q/53670475/570918. – merv Apr 17 '19 at 15:55
  • 1
    The other thing that might be important to add to your question is what exactly you would like to capture from `for` loop section. Is there something specific you want to retain from this section that is essential for downstream analysis? That would help to clarify why the blackbox approach is insufficient. – merv Apr 17 '19 at 16:25
  • hi, thank you for your comments, I had a piece of code to illustrate the model, I whish it will make it more clear. – Marty Apr 19 '19 at 08:26
  • sorry for the delay. I don't see how I could help further. You may want to look into custom Theano ops or using a `pm.Potential` function, but I'm not totally sure that would solve it. The [PyMC Discourse site](https://discourse.pymc.io/) might get you better answers. – merv Apr 25 '19 at 01:25

0 Answers0