Save a meta-model for future use

Viewed 189

I am using openMDAO to construct a co-kriging metamodel that I would like to export and then import in another python code.

I've found a message on the old forum (http://openmdao.org/forum/questions/444/how-can-i-save-the-metamodel-for-later-use?sort=votes) in which someone used pickle to save a meta-model. I have also read about the recorders however I didn't succeed in the different tests I performed.

Is there a way to save the meta-model and use it in a future code?


EDIT: I think I found a kind of solution using 'pickle'. I succeded to do this with a kriging meta-model but i assume I would work the same with the co-kriging.

Like in the post on the 'old' forum of openMDAO, I saved the trained meta-model in a file and then reuse it in another python script. I joined here the part of the code saving the trained meta-model:

cok = MultiFiCoKrigingSurrogate()

prob = Problem(Simulation(cok, nfi=2))
prob.setup(check=False)

prob['mm.train:x1']     = DATA_HF_dim
prob['mm.train:x1_fi2'] = DATA_LF_dim

prob['mm.train:y1']     = rastri_e
prob['mm.train:y1_fi2'] = rastri_c

prob.run()

import pickle
f = open('meta_model_info.p','wb')
pickle.dump(prob,f)
f.close

Once the trained meta-model is saved in the file meta_model_info.p, I can load it in another script, skipping the learning phase. Part of the code of the second script is here:

class Simulation(Group):

    def __init__(self, surrogate, nfi):
        super(Simulation, self).__init__()
        self.surrogate = surrogate

        mm = self.add("mm", MultiFiMetaModel(nfi=nfi))
        mm.add_param('x1', val=0.)

        mm.add_output('y1', val=(0.,0.), surrogate=surrogate)

cok = MultiFiCoKrigingSurrogate()

prob = Problem(Simulation(cok, nfi=2))
prob.setup(check=False)

import pickle

f = open('meta_model_info.p','rb')
clf = pickle.load(f)

pred_cok_clf = []
for x in inputs:
    clf['mm.x1'] = x
    clf.run()
    pred_cok_clf.append(clf['mm.y1'])

pred_mu_clf    = np.array([float(p[0]) for p in pred_cok_clf])
pred_sigma_clf = np.array([float(p[1]) for p in pred_cok_clf])

However I was forced to redefine the class of the problem and to setup the problem either in this second script.

I don't know if it is a proper use of 'pickle' or if there is another way to do this, if you have any suggestion :)

1 Answers
Related