I'm learning how to save a keras model. I wrote a python script based on tensorflow tutorial: https://www.tensorflow.org/tutorials/quickstart/beginner
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10)
])
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile( optimizer='adam', loss=loss_fn, metrics=['accuracy'])
#model.save('model_untrained')
#model.save('model_untrained.keras')
model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test, verbose=2)
The training converges to accuracy better than 97%.
However, if I uncomment the line
model.save('model_untrained.keras')
the training no longer converges, accuracy stays at around 10% (which is completely random for a digit recognition task).
If I instead uncomment the other line, using new TensorFlow SavedModel format:
model.save('model_untrained')
the fit doesn't converge either, and I also get a warning:
Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
which may or may not be related issue (it seems like an unsolved bug: https://github.com/tensorflow/tensorflow/issues/34369).
Is it for some reason a mistake to try to save untrained model, or is it a bug?
Tensorflow 2.8.0, Python 3.10.4