<h3 dir="auto">System information</h3>
<ul dir="auto">
<li><strong>Have I written custom code (as opposed to using a stock example script provided in TensorFlow)</strong>: Yes</li>
<li><strong>OS Platform and Distribution (e.g., Linux Ubuntu 16.04)</strong>: Linux Ubuntu 16.04</li>
<li><strong>TensorFlow installed from (source or binary)</strong>: from binary(pip install)</li>
<li><strong>TensorFlow version (use command below)</strong>:1.9.0</li>
<li><strong>Python version</strong>: 3.6.4</li>
<li><strong>Bazel version (if compiling from source)</strong>: None</li>
<li><strong>GCC/Compiler version (if compiling from source)</strong>: None</li>
<li><strong>CUDA/cuDNN version</strong>:  CUDA-8.0</li>
<li><strong>GPU model and memory</strong>: GTX1080Ti, 12G</li>
<li><strong>Exact command to reproduce</strong>: just as the code below</li>
</ul>
<h3 dir="auto">Describe the problem</h3>
<p dir="auto">As the official document <a href="https://www.tensorflow.org/guide/keras" rel="nofollow">here</a>, tf.keras.models could be saved with <code class="notranslate">model.save('my_model.h5')</code>, namely, "The entire model can be saved to a file that contains the weight values, the model's configuration, and even the optimizer's configuration". However, when I load the model with <code class="notranslate">model = tf.keras.models.load_model('lenet_mnist.h5')</code>, it throws an error: <strong>"You are trying to load a weight file containing 5 layers into a model with 0 layers."</strong></p>
<h3 dir="auto">Source code / logs</h3>
<h4 dir="auto">source code(lenet_mnist.py)</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import tensorflow as tf
import numpy as np
def train_test_data():
    (x_train, y_train),(x_test, y_test) = tf.keras.datasets.mnist.load_data()
    x_train, x_test = x_train / 255.0, x_test / 255.0
    x_train = np.expand_dims(x_train, -1)
    x_test = np.expand_dims(x_test, -1)
    return x_train, y_train, x_test, y_test

def train_fn():
    x_train, y_train, x_test, y_test = train_test_data()
    model = tf.keras.models.Sequential([
        tf.keras.layers.Conv2D(6, [5, 5], strides=1, padding=&quot;same&quot;, use_bias=True),
        tf.keras.layers.AvgPool2D([2, 2], 2, padding=&quot;valid&quot;),   
        tf.keras.layers.LeakyReLU(0),
        tf.keras.layers.Conv2D(16, [5, 5], strides=1, padding=&quot;valid&quot;, use_bias=True),
        tf.keras.layers.AvgPool2D([2, 2], 2, padding=&quot;valid&quot;),
        tf.keras.layers.LeakyReLU(0),
        tf.keras.layers.Conv2D(120, [5, 5], strides=1, padding=&quot;valid&quot;, use_bias=True),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(84, activation=tf.nn.relu),
        tf.keras.layers.Dense(10, activation=tf.nn.softmax),
    ])
    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
    model.fit(x_train, y_train, epochs=5, batch_size=300)
    loss, test_acc = model.evaluate(x_test, y_test, verbose=1)
    print(&quot;test_acc: &quot; + str(test_acc))
    model.save('lenet_mnist.h5')

def test_fn():
    x_train, y_train, x_test, y_test = train_test_data()
    model = tf.keras.models.load_model('lenet_mnist.h5')
    loss, test_acc = model.evaluate(x_test, y_test, verbose=1)
    print(&quot;test_acc: &quot; + str(test_acc))

def main(unused):
    train_fn()
    test_fn()

if __name__ == '__main__':
    tf.app.run()"><pre class="notranslate"><code class="notranslate">import tensorflow as tf
import numpy as np
def train_test_data():
    (x_train, y_train),(x_test, y_test) = tf.keras.datasets.mnist.load_data()
    x_train, x_test = x_train / 255.0, x_test / 255.0
    x_train = np.expand_dims(x_train, -1)
    x_test = np.expand_dims(x_test, -1)
    return x_train, y_train, x_test, y_test

def train_fn():
    x_train, y_train, x_test, y_test = train_test_data()
    model = tf.keras.models.Sequential([
        tf.keras.layers.Conv2D(6, [5, 5], strides=1, padding="same", use_bias=True),
        tf.keras.layers.AvgPool2D([2, 2], 2, padding="valid"),   
        tf.keras.layers.LeakyReLU(0),
        tf.keras.layers.Conv2D(16, [5, 5], strides=1, padding="valid", use_bias=True),
        tf.keras.layers.AvgPool2D([2, 2], 2, padding="valid"),
        tf.keras.layers.LeakyReLU(0),
        tf.keras.layers.Conv2D(120, [5, 5], strides=1, padding="valid", use_bias=True),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(84, activation=tf.nn.relu),
        tf.keras.layers.Dense(10, activation=tf.nn.softmax),
    ])
    model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
    model.fit(x_train, y_train, epochs=5, batch_size=300)
    loss, test_acc = model.evaluate(x_test, y_test, verbose=1)
    print("test_acc: " + str(test_acc))
    model.save('lenet_mnist.h5')

def test_fn():
    x_train, y_train, x_test, y_test = train_test_data()
    model = tf.keras.models.load_model('lenet_mnist.h5')
    loss, test_acc = model.evaluate(x_test, y_test, verbose=1)
    print("test_acc: " + str(test_acc))

def main(unused):
    train_fn()
    test_fn()

if __name__ == '__main__':
    tf.app.run()
</code></pre></div>
<h4 dir="auto">log</h4>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;lenet_mnist.py&quot;, line 60, in &lt;module&gt;
    tf.app.run()
  File &quot;/home/wangwenxiao/anaconda3/lib/python3.6/site-packages/tensorflow/python/platform/app.py&quot;, line 125, in run
    _sys.exit(main(argv))
  File &quot;lenet_mnist.py&quot;, line 57, in main
    test_fn()
  File &quot;lenet_mnist.py&quot;, line 49, in test_fn
    model = tf.keras.models.load_model('lenet_mnist.h5')
  File &quot;/home/wangwenxiao/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/saving.py&quot;, line 232, in load_model
    load_weights_from_hdf5_group(f['model_weights'], model.layers)
  File &quot;/home/wangwenxiao/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/saving.py&quot;, line 732, in load_weights_from_hdf5_group
    ' layers.')
ValueError: You are trying to load a weight file containing 5 layers into a model with 0 layers."><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "lenet_mnist.py", line 60, in &lt;module&gt;
    tf.app.run()
  File "/home/wangwenxiao/anaconda3/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "lenet_mnist.py", line 57, in main
    test_fn()
  File "lenet_mnist.py", line 49, in test_fn
    model = tf.keras.models.load_model('lenet_mnist.h5')
  File "/home/wangwenxiao/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/saving.py", line 232, in load_model
    load_weights_from_hdf5_group(f['model_weights'], model.layers)
  File "/home/wangwenxiao/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/saving.py", line 732, in load_weights_from_hdf5_group
    ' layers.')
ValueError: You are trying to load a weight file containing 5 layers into a model with 0 layers.
</code></pre></div>
<p dir="auto">Bug.  Compiled Tensorflow 1.8 and 1.9rc0 both fail. Linux Mint 18.2.  Python-3.5.2  CUDA 9.0/CuDNN 7.0</p>
<p dir="auto">Have I written custom code: I've attached a file<br>
OS Platform and Distribution: Linux Mint 18.2<br>
TensorFlow installed from: source<br>
TensorFlow version: 1.8 and 1.9rc0<br>
Bazel version: 0.13.1<br>
CUDA/cuDNN version: 9.0/7.0<br>
GPU model and memory: Nvidia 1080Ti<br>
Exact command to reproduce: run the attached script.</p>
<p dir="auto">When using tf.keras.SequentialModel to build a network, and using tf.keras.callbacks.ModelCheckpoint to save the model/weights during training, the saved hdf5 file cannot be loaded into the same model.</p>
<p dir="auto">I believe the same problem was observed in keras-1.0<br>
See here: <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="147802910" data-permission-text="Title is private" data-url="https://github.com/keras-team/keras/issues/2281" data-hovercard-type="issue" data-hovercard-url="/keras-team/keras/issues/2281/hovercard" href="https://github.com/keras-team/keras/issues/2281">keras-team/keras#2281</a><br>
And reportedly fixed here: <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/keras-team/keras/commit/1206120d1084cbe45dc2876f002cb572a97e3844/hovercard" href="https://github.com/keras-team/keras/commit/1206120d1084cbe45dc2876f002cb572a97e3844">keras-team/keras@<tt>1206120</tt></a></p>
<p dir="auto">I've attached a minimal script which will readily reproduce the problem here: <a href="https://github.com/tensorflow/tensorflow/files/2107894/save-bug.txt">save-bug.txt</a></p>
<p dir="auto">The output error I receive when attempting to load the model weights into the same model which I saved them from is:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;./save-bug.py&quot;, line 38, in &lt;module&gt;
    predict()
  File &quot;./save-bug.py&quot;, line 31, in predict
    model.load_weights('test.hdf5');
  File &quot;/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/_impl/keras/engine/network.py&quot;, line 1190, in load_weights
    saving.load_weights_from_hdf5_group(f, self.layers)
  File &quot;/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/_impl/keras/engine/saving.py&quot;, line 697, in load_weights_from_hdf5_group
    ' layers.')
ValueError: You are trying to load a weight file containing 1 layers into a model with 0 layers."><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "./save-bug.py", line 38, in &lt;module&gt;
    predict()
  File "./save-bug.py", line 31, in predict
    model.load_weights('test.hdf5');
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/_impl/keras/engine/network.py", line 1190, in load_weights
    saving.load_weights_from_hdf5_group(f, self.layers)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/keras/_impl/keras/engine/saving.py", line 697, in load_weights_from_hdf5_group
    ' layers.')
ValueError: You are trying to load a weight file containing 1 layers into a model with 0 layers.
</code></pre></div>