def model_from_dict_w_opt(model_dict, custom_objects=None):
"""Builds a sklearn model from a serialized model using `to_dict_w_opt`
Args:
model_dict(dict): a serialized sklearn model
custom_objects(dict, optionnal): a dictionnary mapping custom objects
names to custom objects (callables, etc.)
Returns:
A new sklearn.BaseEstimator (in SUPPORTED) instance. The attributes
are not loaded.
"""
if custom_objects is None:
custom_objects = dict()
# custom_objects = {k: deserialize(k, custom_objects[k])
# for k in custom_objects}
# safety check
if model_dict['config'] not in keyval:
raise NotImplementedError("sklearn model not supported.")
# load the metrics
if 'metrics' in model_dict:
metrics = model_dict.pop('metrics')
else:
metrics = None
# create a new instance of the appropriate model type
model = copy.deepcopy(keyval[model_dict['config']])
# load the parameters
for k, v in model_dict.items():
if isinstance(v, list): # pragma: no cover
setattr(model, k, np.array(v))
else:
setattr(model, k, v)
return model, metrics
评论列表
文章目录