Created
February 16, 2020 00:34
-
-
Save yiyuezhuo/7541dd6e629ba3ffc1fab5314b36c123 to your computer and use it in GitHub Desktop.
colab gists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pystan | |
import pickle | |
from hashlib import md5 | |
def StanModel_cache(model_code, model_name=None, **kwargs): | |
"""Use just as you would `stan`""" | |
code_hash = md5(model_code.encode('ascii')).hexdigest() | |
if model_name is None: | |
cache_fn = 'cached-model-{}.pkl'.format(code_hash) | |
else: | |
cache_fn = 'cached-{}-{}.pkl'.format(model_name, code_hash) | |
try: | |
sm = pickle.load(open(cache_fn, 'rb')) | |
except: | |
sm = pystan.StanModel(model_code=model_code) | |
with open(cache_fn, 'wb') as f: | |
pickle.dump(sm, f) | |
else: | |
print("Using cached StanModel") | |
return sm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment