titu1994 / keras-neural-alu

A Keras implementation of Neural Arithmatic and Logical Unit
MIT License
27 stars 8 forks source link

Loading a model #1

Open lynochka opened 6 years ago

lynochka commented 6 years ago

Hei, thanks for the implementation! Tried to load a trained model with

load_model(modelfile, custom_objects={'NALU': NALU}) but got the following error:

Getting data from Blob Storage:
 - Working on best_cumsum_filming_20180814.hdf5 ... no local file found, downloading ... reading finished in 0.26 sec!
TypeError: __init__() got an unexpected keyword argument 'trainable'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<command-2520505459566318> in <module>()
      1 get_blobs_to_local(bbs, [model_saved_in_file], container="models", blobfolder="live", force_download=True)
      2 
----> 3 model = load_model(model_saved_in_file, custom_objects={'NALU': NALU})

/databricks/python/lib/python3.5/site-packages/keras/engine/saving.py in load_model(filepath, custom_objects, compile)
    258             raise ValueError('No model found in config file.')
    259         model_config = json.loads(model_config.decode('utf-8'))
--> 260         model = model_from_config(model_config, custom_objects=custom_objects)
    261 
    262         # set weights

/databricks/python/lib/python3.5/site-packages/keras/engine/saving.py in model_from_config(config, custom_objects)
    332                         '`Sequential.from_config(config)`?')
    333     from ..layers import deserialize
--> 334     return deserialize(config, custom_objects=custom_objects)
    335 
    336 

/databricks/python/lib/python3.5/site-packages/keras/layers/__init__.py in deserialize(config, custom_objects)
     53                                     module_objects=globs,
     54                                     custom_objects=custom_objects,
---> 55                                     printable_module_name='layer')

/databricks/python/lib/python3.5/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    143                     config['config'],
    144                     custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 145                                         list(custom_objects.items())))
    146             with CustomObjectScope(custom_objects):
    147                 return cls.from_config(config['config'])

/databricks/python/lib/python3.5/site-packages/keras/engine/network.py in from_config(cls, config, custom_objects)
   1015         # First, we create all layers and enqueue nodes to be processed
   1016         for layer_data in config['layers']:
-> 1017             process_layer(layer_data)
   1018         # Then we process nodes in order of layer depth.
   1019         # Nodes that cannot yet be processed (if the inbound node

/databricks/python/lib/python3.5/site-packages/keras/engine/network.py in process_layer(layer_data)
   1001 
   1002             layer = deserialize_layer(layer_data,
-> 1003                                       custom_objects=custom_objects)
   1004             created_layers[layer_name] = layer
   1005 

/databricks/python/lib/python3.5/site-packages/keras/layers/__init__.py in deserialize(config, custom_objects)
     53                                     module_objects=globs,
     54                                     custom_objects=custom_objects,
---> 55                                     printable_module_name='layer')

/databricks/python/lib/python3.5/site-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    145                                         list(custom_objects.items())))
    146             with CustomObjectScope(custom_objects):
--> 147                 return cls.from_config(config['config'])
    148         else:
    149             # Then `cls` may be a function returning a class.

/databricks/python/lib/python3.5/site-packages/keras/engine/base_layer.py in from_config(cls, config)
   1107             A layer instance.
   1108         """
-> 1109         return cls(**config)
   1110 
   1111     def count_params(self):

TypeError: __init__() got an unexpected keyword argument 'trainable'

Should I have saved it with only weights or is there a way around? Thanks!

mattb46 commented 6 years ago

I had a similar error when I went to save and reload a model using NAC or NALU, however my error was "TypeError: init() got an unexpected keyword argument 'name'". By adding **kwargs as an argument at the end in init I was able to train a model, save it, reload it and predict with the loaded model.

Like so for NALU def __init__(self, units, use_gating=True, kernel_W_initializer='glorot_uniform', kernel_M_initializer='glorot_uniform', gate_initializer='glorot_uniform', kernel_W_regularizer=None, kernel_M_regularizer=None, gate_regularizer=None, kernel_W_constraint=None, kernel_M_constraint=None, gate_constraint=None, epsilon=1e-7, **kwargs):

and for NAC

def __init__(self, units, kernel_W_initializer='glorot_uniform', kernel_M_initializer='glorot_uniform', kernel_W_regularizer=None, kernel_M_regularizer=None, kernel_W_constraint=None, kernel_M_constraint=None, **kwargs):

I also created a pull request to add this, hope this helps!