我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用keras.models.model_from_config()。
def load_model(config=None): assert config is not None, 'Missing input configuration' configs = config if isinstance(config, str): configs = load_json(config) assert len(configs) > 0, 'No configuration specified!' assert 'model' in configs, 'Missing model definition!' m = model_from_config(configs['model']) if m is None: raise Exception('Invalid model!') if 'weights' in configs: m.load_model(configs['weights']) return m
def clone_model(model, custom_objects={}): # Requires Keras 1.0.7 since get_config has breaking changes. config = { 'class_name': model.__class__.__name__, 'config': model.get_config(), } clone = model_from_config(config, custom_objects=custom_objects) clone.set_weights(model.get_weights()) return clone
def model_from_config(config): assert 'class_name' in config, 'Missing model class!' # fetch all members of module 'pydl.models' classes = dict(inspect.getmembers(sys.modules['pydl.models'], inspect.isclass)) return k_models.model_from_config(config, classes)
def clone_model(model, custom_objects=None): from keras.models import model_from_config custom_objects = custom_objects or {} config = { 'class_name': model.__class__.__name__, 'config': model.get_config(), } clone = model_from_config(config, custom_objects=custom_objects) clone.set_weights(model.get_weights()) return clone # clone a keras optimizer without file I/O