add the function of saving all the parameters into a HDF5 file

This commit is contained in:
Zhenwen Dai 2014-11-11 15:41:35 +00:00
parent 9b2e49c949
commit 5736caa8fd

View file

@ -1009,3 +1009,28 @@ class Parameterizable(OptimizationHandlable):
updates get passed through. See :py:function:``GPy.core.param.Observable.add_observer``
"""
pass
def save_params_H5(self, filename):
"""
Save all the model parameters into a HDF5 file.
"""
from . import Param
from ...util.misc import param_to_array
def gather_params(self, plist):
if isinstance(self,Param):
plist.append(self)
plist = []
self.traverse(gather_params, plist)
names = self.parameter_names(adjust_for_printing=True)
try:
import h5py
f = h5py.File(filename,'w')
for p,n in zip(plist,names):
n = n.replace('.','_')
p = param_to_array(p)
d = f.create_dataset(n,p.shape,dtype=p.dtype)
d[:] = p
f.close()
except:
raise 'Fails to write the parameters into a HDF5 file!'