简体   繁体   中英

Tensorflow / Keras Tutorial Save / Load Model not work

I am unable to save a simple keras Sequential model as per the tutorial using Tensorflow 1.11.0. I am able to convert the model configuration to a json string and I am able to save the weights, but not the model in its entirety.

The error comes from the first line of code in Model.save which checks that self._is_graph_network is True . I feel like there is something basic that I am missing, but I cannot figure it out. I believe my code is analogous to the tutorial code found here: Keras Save and Restore in the Save Entire Model section.

Python 3.6.6 (default, Jun 28 2018, 05:43:53)
[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> import tensorflow.keras as tk
>>> mnist = tk.datasets.mnist
>>> (xtrain, ytrain), (xtest, ytest) = mnist.load_data()
>>> xtrain, xtest = xtrain/255.0, xtest/255.0
>>> model = tk.models.Sequential([tk.layers.Flatten(), tk.layers.Dense(64, activation=tf.nn.relu), \
tk.layers.Dense(10, activation=tf.nn.softmax)])
>>> model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
>>> model.fit(xtrain, ytrain, epochs=5)
Epoch 1/5
60000/60000 [==============================] - 2s 39us/step - loss: 0.3086 - acc: 0.9126
...
Epoch 5/5
60000/60000 [==============================] - 2s 37us/step - loss: 0.0715 - acc: 0.9787
<tensorflow.python.keras.callbacks.History object at 0x129819470>
>>> model.evaluate(xtest, ytest)
10000/10000 [==============================] - 0s 17us/step
[0.09259423367818817, 0.9711]
>>> model.save('m0x.h5')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mmelton/Library/Python/3.6/lib/python/site-packages/tensorflow/python/keras/engine/n\
etwork.py", line 1358, in save
    raise NotImplementedError
NotImplementedError
>>> model._is_graph_network
False
>>> model.save_weights('w0.h5', save_format='h5')
>>>

>>> tf.__version__
'1.11.0'
>>> tk.__version__
'2.1.6-tf'
>>>

What am I missing?

model = tk.models.Sequential([
    tk.layers.Flatten(), 
    tk.layers.Dense(64, activation=tf.nn.relu), 
    tk.layers.Dense(10, activation=tf.nn.softmax)
])

Your network has no inputs - Flatten(input_shape=(784,)) . Also, having Flatten() as your first layer is a little funny but go for it if you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM