Issue 1:
System information

Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes

OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 16.04

TensorFlow installed from (source or binary): from binary(pip install)

TensorFlow version (use command below):1.9.0

Python version: 3.6.4

Bazel version (if compiling from source): None

GCC/Compiler version (if compiling from source): None

CUDA/cuDNN version: CUDA-8.0

GPU model and memory: GTX1080Ti, 12G

Exact command to reproduce: just as the code below

Describe the problem

As the official document here, tf.keras.models could be saved with model.save('my_model.h5'), 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 model = tf.keras.models.load_model('lenet_mnist.h5'), it throws an error: "You are trying to load a weight file containing 5 layers into a model with 0 layers."

Source code / logs

source code(lenet_mnist.py)

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()

log

Traceback (most recent call last):
File "lenet_mnist.py", line 60, in <module>
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.

Issue 2:
Bug. Compiled Tensorflow 1.8 and 1.9rc0 both fail. Linux Mint 18.2. Python-3.5.2 CUDA 9.0/CuDNN 7.0

Have I written custom code: I've attached a file

OS Platform and Distribution: Linux Mint 18.2

TensorFlow installed from: source

TensorFlow version: 1.8 and 1.9rc0

Bazel version: 0.13.1

CUDA/cuDNN version: 9.0/7.0

GPU model and memory: Nvidia 1080Ti

Exact command to reproduce: run the attached script.

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.

I believe the same problem was observed in keras-1.0

See here: keras-team/keras#2281

And reportedly fixed here: keras-team/keras@1206120

I've attached a minimal script which will readily reproduce the problem here: save-bug.txt

The output error I receive when attempting to load the model weights into the same model which I saved them from is:

Traceback (most recent call last):
File "./save-bug.py", line 38, in <module>
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.