How to combine pymc with neural network

I’m trying to use neural network as my model for finding input x by means of plain bayes for observation y,like this:


with pm.Model() as multi_objective_model:
    para1=pm.Uniform('para1',lower=[-10,-10,-10],upper=[10,10,10])
    para2=pm.Uniform('para2',lower=[-10,-10,-10],upper=[10,10,10])
    para3=pm.Uniform('para3',lower=[-10,-10,-10],upper=[10,10,10])
    sigmas=pm.HalfCauchy('sigmas',[1,1,1])
    y_observed=pm.Normal(
        "y_observed",
        mu=neural network(X,para1,para2,para3),
        sigma=sigmas,
        observed=Y,
    )
    
    prior = pm.sample_prior_predictive()
    posterior=pm.sample(draws=4000,tune=2000,target_accept=0.9,chains=4)

I don’t know how to combine neural network built by pytorch or tensorflow with pymc, after all, both tensorflow/pytorch and pytensor have strict shape requirements. Is there any similar example or interface? Is there any similar example or interface?

You cannot directly use external packages like pytorch or tf in a pymc model, any more than you could define a pytorch model using tensorflow layers.

Your options are:

  1. Wrap the NN in a Pytensor Op. This blog post shows how to do it with Jax/Flax.
  2. Write the NN yourself directly in pytensor. This example might be helpful.

If the architecture is simple, (2) is probably a much easier option.

1 Like

Thank you very much, I might try the second method, the neural network I am using is a specially designed fully connected neural network that is difficult to implement with pytensor. Any more examples of similar applications?