How can I save this model(s) and why is the use for "with tf.Graph().as_default()"

Viewed 189

I have been trying to train and then compile this RL algo. My problem comes when I want to save the three models.

Here is how the neural networks are defined:

def build_networks(state_shape, action_size, learning_rate, critic_weight,
               hidden_neurons, entropy):

state_input = layers.Input(state_shape, name='frames')
advantages = layers.Input((1,), name='advantages')  # PG, A instead of G

# PG
actor_1 = layers.LSTM(input_shape = (1,4),units=hidden_neurons, return_sequences = True)(state_input)
actor_2 = layers.LSTM(input_shape = (1,4), units=hidden_neurons, return_sequences = False)(actor_1)
probabilities = layers.Dense(action_size, activation='softmax')(actor_2)

# DQN
critic_1 = layers.LSTM(input_shape = (1,4), units = hidden_neurons, return_sequences = True)(state_input)
critic_2 = layers.LSTM(input_shape = (1,4), units=hidden_neurons, return_sequences = False)(critic_1)
values = layers.Dense(1, activation='linear')(critic_2)

def actor_loss(y_true, y_pred):  # PG
    #cusom loss-do stuff

# Train both actor and critic at the same time.
actor = models.Model(
    inputs=[state_input, advantages], outputs=[probabilities, values])
actor.compile(
    loss=[actor_loss, 'mean_squared_error'],  # [PG, DQN]
    loss_weights=[1, critic_weight],  # [PG, DQN]
    optimizer=tf.keras.optimizers.Adam(lr=learning_rate))

critic = models.Model(inputs=[state_input], outputs=[values])
policy = models.Model(inputs=[state_input], outputs=[probabilities])
return actor, critic, policy

As you can see, the only NN that uses compile is the actor. How can I save the three NN for later use? Do I need to save them separate or I need to save the three NN as one? Also, when I tried to save them, I used this line:

with tf.Graph().as_default():
    test_agent.actor.save_weights("Test_agent_actor.h5")
    test_agent.critic.save_weights("Test_agent_critic.h5")
    test_agent.policy.save_weights("Test_agent_policy.h5")

But when I try to load any of them, it says that:

WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.
WARNING:tensorflow:Output dense missing from loss dictionary. We assume this was done on purpose. The fit and evaluate APIs will not be expecting any data to be passed to dense.

Even when I try to use the NN in validation/test dataset, I get an error that tells me that:

InvalidArgumentError: Tensor frames:0, specified in either feed_devices or fetch_devices was not found in the Graph

Or when I try to save again the NN, it tells me something about "ragged" and the file size goes from 25MB to 1kb. Why is this? What I'm doing wrong? Should I use .hdf5 instead of .h5?

And also, I'm training all this with this part of code:

with tf.Graph().as_default():
     #do some stuff

Why is this for? Why should I use this and also use import tensorflow.keras instead of import keras?

Thank you in advance!

0 Answers
Related