Dawid Cimoch
Dawid Cimoch

Reputation: 32

Loading model tensorflow

I have a problem with loading my trained model to another python file. Here's the code I use to save it:

input_size = 16
output_size = 2
hidden_layer_size = 50

model = tf.keras.Sequential([
    tf.keras.layers.Dense(hidden_layer_size, 
                          activation='relu'), # 1st hidden layer
    tf.keras.layers.Dense(hidden_layer_size, 
                          activation='relu'), # 2nd hidden layer
    tf.keras.layers.Dense(output_size, 
                          activation='softmax') # output layer
])

model.compile(optimizer='Adam', 
              loss='sparse_categorical_crossentropy', 
              metrics=['accuracy'])

batch_size = 100
max_epochs = 20
early_stopping=tf.keras.callbacks.EarlyStopping()

model.fit(train_inputs, # train inputs
          train_targets, # train targets
          batch_size=batch_size, # batch size
          epochs=max_epochs, # epochs that we will train for (assuming early stopping doesn't kick in)
          callbacks=[early_stopping],
          validation_data=(validation_inputs, validation_targets), # validation data
          verbose = 1 # making sure we get enough information about the training process
          )  
saver = tf.train.Saver()
sess = tf.compat.v1.keras.backend.get_session()
saver.save(sess,r'C:\Users\User\Desktop\tensorflow\model\tf_keras_session\session.ckpt' )

model.save(r'C:\Users\User\Desktop\tensorflow\model\tensorflow_model_3')

And here is the code I use to load it:

model = tf.keras.models.load_model('./data/tensorflow_model_3')
saver = tf.compat.v1.train.Saver()
sess = K.get_session()
saver.restore(sess, './data/tf_keras_session/session.ckpt')

Finally, I got an error like that (the problem is with define "saver"):

RuntimeError: When eager execution is enabled, `var_list` must specify a list or dict of variables to save

Upvotes: 1

Views: 779

Answers (1)

Innat
Innat

Reputation: 17219

Disable the eager execution mode. Set it the beginning of the tf import.

tf.compat.v1.disable_eager_execution()

Upvotes: 1

Related Questions