Save fit model in pystan 2

Hi @ahartikainen

I’d like to understand if there are better ways to save model fits other than pickle files in order to use stan models in production. Pickle files often get corrupted and I’m recently getting some pandas errors like:

RecursionError: maximum recursion depth exceeded.

Can model fits get stored in json files with no loss of information?

Also are the save fit files linked somehow to the path were the stan model gets compiled? Can they be used outside the environment where the model was fit?

Thanks!

Hi

What do you do with your fit? Just access your draws?

You can create a dataframe and save that into json or use ArviZ InferenceData to save your draws e.g. in netCDF4.

Thank you for responding.

I’m trying to transfer the model + fit obtained in my local environment to a production environment.

I use the fit to make predictions on new data. You’re right - I only need the fit draws to make predictions so storing them in a json can work.

The predictions stan model I use next is also saved in a pickle file and I would want to move away from pickle files.

I guess the short answer is to re compile the stan file in production env. Is there any other way to access a pre compiled stan model object?

In this particular situation, you might want to try out using cmdstanpy – the CmdStanModel object can be instantiated directly from a compiled Stan binary.

Since you’re aiming for a production environment, it might make sense in the environment setup to compile the Stan model and have it sit in a predefined location, which you can just reference with cmdstandpy.

Alternatively, if you are using PyStan 3, then models are cached by default after they are first built, so if you were to run the model the same way every time, then it would pick up the cached model without having to recompile.

Seems like either would work.

Ok great, that makes sense.

Thank you both

I’m going to close this topic to new posts because it primarily concerns PyStan 2, which is no longer supported. I also updated the title.