5

I created a Sequential model using tf.keras as follows:

model = tf.keras.Sequential() model.add(tf.keras.layers.Dense(8, input_dim=4)) model.add(tf.keras.layers.Dense(3, activation=tf.nn.softmax)) opt = tf.train.AdamOptimizer(learning_rate=0.001) model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=["accuracy"]) model.summary() 

After that, I created a training process using train_on_batch:

EPOCHS=50 for epoch in range(EPOCHS): for metrics, labels in dataset: # Calculate training loss and accuracy tr_loss, tr_accuracy = model.train_on_batch(metrics, labels) 

When I try to save the model, I receive a warning. I can't understand why, because I included the optimizer as part of the model.compile:

tf.keras.models.save_model( model, "./model/iris_model.h5", overwrite=True, include_optimizer=True ) 

WARNING:tensorflow:TensorFlow optimizers do not make it possible to access optimizer attributes or optimizer state after instantiation. As a result, we cannot save the optimizer as part of the model save file.You will have to compile your model again after loading it. Prefer using a Keras optimizer instead (see keras.io/optimizers).

The TF version I used is 1.9.0-rc2.

2 Answers 2

12

As the warning says the Tensorflow optimizers cannot be saved when saving the model. Instead, use the optimizers provided by Keras:

opt = tf.keras.optimizers.Adam(lr=0.001) 
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, @today you are right I changed the optimizer to allow the eager execution :(
1

Optimizer like tf.keras.optimizers.Adam() will be saved, and tf.train.AdamOptimizer() will not be saved on model.save().

As the time as writing this some official tutorials on TensorFlow use tf.train.* optimizer while I strongly believe selecting the tf.keras.optimizers.* is the best way to go.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.