title: Saving Keras models with Custom Layers
body: <p>I am trying to save a Keras model in a H5 file. The Keras model has a <strong>custom layer</strong>.
  When I try to <strong>restore the model</strong>, I get the following error:</p>
  
  <pre class="lang-py prettyprint-override"><code>---------------------------------------------------------------------------
  ValueError                                Traceback (most recent call last)
  &lt;ipython-input-5-0fbff9b56a9d&gt; in &lt;module&gt;()
        1 model.save('model.h5')
        2 del model
  ----&gt; 3 model = tf.keras.models.load_model('model.h5')
  
  8 frames
  /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/generic_utils.py in class_and_config_for_serialized_keras_object(config, module_objects, custom_objects, printable_module_name)
      319   cls = get_registered_object(class_name, custom_objects, module_objects)
      320   if cls is None:
  --&gt; 321     raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
      322 
      323   cls_config = config['config']
  
  ValueError: Unknown layer: CustomLayer
  </code></pre>
  
  <p>Could you please tell me how I am supposed to save and load weights of all the custom Keras layers too? (Also, there was no warning when saving, will it be possible to load models from H5 files which I have already saved but can't load back now?)</p>
  
  <p>Here is the minimal working code sample (MCVE) for this error, as well as the full expanded message: <a href="https://colab.research.google.com/drive/18r5TDlVZR8SC1WBOXuYKk9f17edZN6y6?usp=sharing" rel="noreferrer">Google Colab Notebook</a></p>
  
  <p>Just for completeness, this is the code I used to make my custom layer.
  <code>get_config</code> and <code>from_config</code> are both working fine.</p>
  
  <pre class="lang-py prettyprint-override"><code>class CustomLayer(tf.keras.layers.Layer):
      def __init__(self, k, name=None):
          super(CustomLayer, self).__init__(name=name)
          self.k = k
  
      def get_config(self):
          return {'k': self.k}
  
      def call(self, input):
          return tf.multiply(input, 2)
  
  model = tf.keras.models.Sequential([
      tf.keras.Input(name='input_layer', shape=(10,)),
      CustomLayer(10, name='custom_layer'),
      tf.keras.layers.Dense(1, activation='sigmoid', name='output_layer')
  ])
  model.save('model.h5')
  model = tf.keras.models.load_model('model.h5')
  </code></pre>