tensorflow: model.save() returning a 'NotImplementedError'

Please go to Stack Overflow for help and support:

https://stackoverflow.com/questions/tagged/tensorflow

If you open a GitHub issue, here is our policy:

  1. It must be a bug, a feature request, or a significant problem with documentation (for small docs fixes please send a PR instead).
  2. The form below must be filled out.
  3. It shouldn’t be a TensorBoard issue. Those go here.

Here’s why we have that policy: TensorFlow developers respond to issues. We want to focus on work that benefits the whole community, e.g., fixing bugs and adding features. Support only helps individuals. GitHub also notifies thousands of people when issues are filed. We want them to see you communicating an interesting problem, rather than being redirected to Stack Overflow.


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): Windows 10 1803
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
  • TensorFlow installed from (source or binary): From pip
  • TensorFlow version (use command below): 1.11.0
  • Python version: 3.6.6
  • Bazel version (if compiling from source):
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version:
  • GPU model and memory:
  • Exact command to reproduce:

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(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=3)

model.save('epic_num_reader.model')

You can collect some of this information using our environment capture script:

https://github.com/tensorflow/tensorflow/tree/master/tools/tf_env_collect.sh

You can obtain the TensorFlow version with

python -c “import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)”

Describe the problem

I’m receiving a NotImplementedError when trying to save a new model. The code above written above creates the error. This code is from a tutorial i’ve been following here Python Programming (Sentdex).

It’s on a fresh machine with a fresh install of python and tensorflow.

Talking with some others on a Discord chat, i’m not the first person who’s encountered this recently.

Source code / logs

Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached. Try to provide a reproducible test case that is the bare minimum necessary to generate the problem.

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-11-7b7b789f82a6> in <module>
----> 1 model.save('epic_num_reader.model')

c:\users\james\appdata\local\programs\python\python36\lib\site-packages\tensorflow\python\keras\engine\network.py in save(self, filepath, overwrite, include_optimizer)
   1356     """
   1357     if not self._is_graph_network:
-> 1358       raise NotImplementedError
   1359 
   1360     from tensorflow.python.keras.models import save_model  # pylint: disable=g-import-not-at-top

NotImplementedError:

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 7
  • Comments: 17 (6 by maintainers)

Most upvoted comments

@muhammadmw I found that my problem was caused by subclassing Keras.Model. A model created by subclassing Keras.Model cannot be serialized by using the save_model function. I switched over to the Keras functional Api and I no longer have the problem.

Add save_weights_only = True for ModelCheckpoint or use model.save_weights()

Working on a fix for this, in the meantime a workaround is to provide input_shape to the first Layer or your Model:

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=...))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(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=3)

model.save('epic_num_reader.model')

Just a heads up. I was able to execute the @jam3sn 's code successfully in TF 1.10 but it failed in TF 1.11

I am also getting the same problem in version 1.12.0


TypeError Traceback (most recent call last) <ipython-input-18-b9d746b1e27b> in <module> ----> 1 tf.keras.models.save_model(‘epic_num_reader.model’)

TypeError: save_model() missing 1 required positional argument: ‘filepath’