Reputation: 31
I'm using AutoKeras 1.0 and I'm having trouble understanding how I'm supposed to save and reload a trained model (plus weights, etc).
I can train a model easily, using code similar to:
num_data = 500
train_x = common.generate_structured_data(num_data)
train_y = common.generate_one_hot_labels(num_instances=num_data, num_classes=3)
clf = ak.StructuredDataClassifier(
column_names=common.COLUMN_NAMES_FROM_NUMPY,
max_trials=1,
seed=common.SEED)
clf.fit(train_x, train_y, epochs=4, validation_data=(train_x, train_y))
loss = clf.evaluate(train_x, train_y)
print(loss)
However, I'm not able to tell from the docs how to save this model and reuse it in another program later. I've tried finding the "best" model and saving it, like so:
preprocess_graph, best_model = clf.tuner.get_best_model()
best_model.save("testmodel.h5")
However, when I try to load this model again, I get the following:
new_model = load_model("testmodel.h5")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-bd01053bfeda> in <module>
----> 1 new_model = load_model("testmodel.h5")
/opt/conda/lib/python3.7/site-packages/keras/engine/saving.py in load_wrapper(*args, **kwargs)
490 os.remove(tmp_filepath)
491 return res
--> 492 return load_function(*args, **kwargs)
493
494 return load_wrapper
/opt/conda/lib/python3.7/site-packages/keras/engine/saving.py in load_model(filepath, custom_objects, compile)
582 if H5Dict.is_supported_type(filepath):
583 with H5Dict(filepath, mode='r') as h5dict:
--> 584 model = _deserialize_model(h5dict, custom_objects, compile)
585 elif hasattr(filepath, 'write') and callable(filepath.write):
586 def load_function(h5file):
/opt/conda/lib/python3.7/site-packages/keras/engine/saving.py in _deserialize_model(h5dict, custom_objects, compile)
272 raise ValueError('No model found in config.')
273 model_config = json.loads(model_config.decode('utf-8'))
--> 274 model = model_from_config(model_config, custom_objects=custom_objects)
275 model_weights_group = h5dict['model_weights']
276
/opt/conda/lib/python3.7/site-packages/keras/engine/saving.py in model_from_config(config, custom_objects)
625 '`Sequential.from_config(config)`?')
626 from ..layers import deserialize
--> 627 return deserialize(config, custom_objects=custom_objects)
628
629
/opt/conda/lib/python3.7/site-packages/keras/layers/__init__.py in deserialize(config, custom_objects)
166 module_objects=globs,
167 custom_objects=custom_objects,
--> 168 printable_module_name='layer')
/opt/conda/lib/python3.7/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
145 config['config'],
146 custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 147 list(custom_objects.items())))
148 with CustomObjectScope(custom_objects):
149 return cls.from_config(config['config'])
/opt/conda/lib/python3.7/site-packages/keras/engine/network.py in from_config(cls, config, custom_objects)
1054 # First, we create all layers and enqueue nodes to be processed
1055 for layer_data in config['layers']:
-> 1056 process_layer(layer_data)
1057
1058 # Then we process nodes in order of layer depth.
/opt/conda/lib/python3.7/site-packages/keras/engine/network.py in process_layer(layer_data)
1040
1041 layer = deserialize_layer(layer_data,
-> 1042 custom_objects=custom_objects)
1043 created_layers[layer_name] = layer
1044
/opt/conda/lib/python3.7/site-packages/keras/layers/__init__.py in deserialize(config, custom_objects)
166 module_objects=globs,
167 custom_objects=custom_objects,
--> 168 printable_module_name='layer')
/opt/conda/lib/python3.7/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
147 list(custom_objects.items())))
148 with CustomObjectScope(custom_objects):
--> 149 return cls.from_config(config['config'])
150 else:
151 # Then `cls` may be a function returning a class.
/opt/conda/lib/python3.7/site-packages/keras/engine/base_layer.py in from_config(cls, config)
1177 A layer instance.
1178 """
-> 1179 return cls(**config)
1180
1181 def count_params(self):
/opt/conda/lib/python3.7/site-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name + '` call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
TypeError: __init__() got an unexpected keyword argument 'ragged'
Am I doing this wrong or is there a better way?
Upvotes: 3
Views: 3551
Reputation: 3285
With AutoKeras 1.0.2 this seems to work:
best_model = clf.tuner.get_best_model()
best_model.save("testmodel.h5")
model = tf.keras.models.load_model("testmodel.h5")
Upvotes: 0
Reputation: 31
You can try this for loading saved model:
import tensorflow as tf
new_model = tf.keras.models.load_model('testmodel.h5')
Upvotes: 0