Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras and Tensorflow: Saving non-sequential model weights

(I'm using Tensorflow 1.8.0 ...)

The documentation from Keras on how to save a model mentions no difference between saving a sequential model vs. one created from the functional API. But, all of the following blocks of code fail:

import tensorflow as tf
net = tf.keras.models.Model()
net.save('file')

or

import tensorflow as tf
net = tf.keras.models.Model()
print(net.to_json())

or

import tensorflow as tf
net = tf.keras.models.Model()
print(net.to_yaml())

or

import tensorflow as tf
net = tf.keras.models.Model()
print(net.get_config())

They raise a NotImplementedError. In the Keras module, the relevant lines are

if not self._is_graph_network:
  raise NotImplementedError

which shows up in .save and get_config (the latter is also called by to_json and to_yaml.

The only thing that DOES work is the following

import tensorflow as tf
net = tf.keras.models.Model()
net.save_weights('file')

in which case the weights are saved successfully and can be successfully loaded with net.load_weights.

However, replacing the second line of the above blocks of code, net = tf.keras.models.Model(), with net = tf.keras.models.Sequential(), making net a sequential model, allows everything above to work.

Is it really not possible to save the structure of a Keras model made with the functional API (using Model rather than Sequential)? Right now, can we only save weights?

like image 205
Jay Calamari Avatar asked Sep 15 '25 12:09

Jay Calamari


1 Answers

Of course its possible to save Model, all your examples have an empty Model, whcih makes no sense to save. Keras' author simply did not implement that case.

If you test with a non-empty Model you will see that saving works perfectly. We use it every day.

like image 184
Dr. Snoopy Avatar answered Sep 18 '25 08:09

Dr. Snoopy