title: Can't save custom subclassed model
body: <p>Inspired by <a href="https://www.tensorflow.org/guide/keras#model_subclassing" rel="noreferrer">tf.keras.Model subclassing</a> I created custom model.<br>
  I can train it and get successfull results, but <strong>I can't save it</strong>.<br>
  I use python3.6 with tensorflow v1.10 (or v1.9)  </p>
  
  <p>Minimal complete code example here:</p>
  
  <pre><code>import tensorflow as tf
  from tensorflow.keras.datasets import mnist
  
  
  class Classifier(tf.keras.Model):
      def __init__(self):
          super().__init__(name="custom_model")
  
          self.batch_norm1 = tf.layers.BatchNormalization()
          self.conv1 = tf.layers.Conv2D(32, (7, 7))
          self.pool1 = tf.layers.MaxPooling2D((2, 2), (2, 2))
  
          self.batch_norm2 = tf.layers.BatchNormalization()
          self.conv2 = tf.layers.Conv2D(64, (5, 5))
          self.pool2 = tf.layers.MaxPooling2D((2, 2), (2, 2))
  
      def call(self, inputs, training=None, mask=None):
          x = self.batch_norm1(inputs)
          x = self.conv1(x)
          x = tf.nn.relu(x)
          x = self.pool1(x)
  
          x = self.batch_norm2(x)
          x = self.conv2(x)
          x = tf.nn.relu(x)
          x = self.pool2(x)
  
          return x
  
  
  if __name__ == '__main__':
      (x_train, y_train), (x_test, y_test) = mnist.load_data()
  
      x_train = x_train.reshape(*x_train.shape, 1)[:1000]
      y_train = y_train.reshape(*y_train.shape, 1)[:1000]
  
      x_test = x_test.reshape(*x_test.shape, 1)
      y_test = y_test.reshape(*y_test.shape, 1)
  
      y_train = tf.keras.utils.to_categorical(y_train)
      y_test = tf.keras.utils.to_categorical(y_test)
  
      model = Classifier()
  
      inputs = tf.keras.Input((28, 28, 1))
  
      x = model(inputs)
      x = tf.keras.layers.Flatten()(x)
      x = tf.keras.layers.Dense(10, activation="sigmoid")(x)
  
      model = tf.keras.Model(inputs=inputs, outputs=x)
      model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
      model.fit(x_train, y_train, epochs=1, shuffle=True)
  
      model.save("./my_model")
  </code></pre>
  
  <p>Error message:  </p>
  
  <pre><code>1000/1000 [==============================] - 1s 1ms/step - loss: 4.6037 - acc: 0.7025
  Traceback (most recent call last):
    File "/home/user/Data/test/python/mnist/mnist_run.py", line 62, in &lt;module&gt;
      model.save("./my_model")
    File "/home/user/miniconda3/envs/ml3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1278, in save
      save_model(self, filepath, overwrite, include_optimizer)
    File "/home/user/miniconda3/envs/ml3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/saving.py", line 101, in save_model
      'config': model.get_config()
    File "/home/user/miniconda3/envs/ml3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1049, in get_config
      layer_config = layer.get_config()
    File "/home/user/miniconda3/envs/ml3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1028, in get_config
      raise NotImplementedError
  NotImplementedError
  
  Process finished with exit code 1
  </code></pre>
  
  <p>I looked into the error line and found out that <strong>get_config</strong> method checks <strong>self._is_graph_network</strong></p>
  
  <p>Do anybody deal with this problem?</p>
  
  <p>Thanks!</p>
  
  <p><strong>Update 1:</strong><br>
  On the keras 2.2.2 (not tf.keras)<br>
  Found comment (for model saving)<br>
  file: keras/engine/network.py<br>
  Function: get_config  </p>
  
  <blockquote>
    <p># Subclassed networks are not serializable<br>
    # (unless serialization is implemented by<br>
    # the author of the subclassed network).  </p>
  </blockquote>
  
  <p>So, obviously it won't work...<br>
  I wonder, why don't they point it out in the <a href="https://www.tensorflow.org/guide/keras" rel="noreferrer">documentation</a> (Like: "Use subclassing without ability to save!")</p>
  
  <p><strong>Update 2:</strong><br>
  Found in <a href="https://keras.io/models/about-keras-models/" rel="noreferrer">keras documentation</a>:  </p>
  
  <blockquote>
    <p>In subclassed models, the model's topology is defined as Python code<br>
    (rather than as a static graph of layers). That means the model's<br>
    topology cannot be inspected or serialized. As a result, the following<br>
    methods and attributes are not available for subclassed models:  </p>
    
    <p>model.inputs        and model.outputs.<br>
    model.to_yaml()     and model.to_json()<br>
    model.get_config()  and model.save().  </p>
  </blockquote>
  
  <p><strong>So, there is no way to save model by using subclassing.</strong><br>
  It's possible to only use <code>Model.save_weights()</code></p>