I tried saving and loading a tensorflow model on the same device and got the following warning:
UserWarning: Skipping variable loading for optimizer 'adam', because it has 14 variables whereas the saved optimizer has 2 variables.
The model loaded did not behave the same way as when being trained and had a much higher error rate.
Here is the model class:
class DQNAgent:
def __init__(self, state_size, action_size):
self.state_size = state_size
self.action_size = action_size
self.memory = deque(maxlen = 20000)
#Other parameters...
self.model = self._build_model()
def _build_model(self):
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation="relu", input_shape=(self.state_size,)),
tf.keras.layers.Dense(64, activation="relu"),
tf.keras.layers.Dense(self.action_size, activation=None)
])
model.compile(optimizer = tf.keras.optimizers.Adam(learning_rate = self.alpha), loss = 'mse')
return model
...
def save(self, filepath):
self.model.save(filepath)
def load(self, filepath):
self.model = tf.keras.models.load_model(filepath)
I initialised, then saved the model as such:
agent = DQNAgent(state_size=5, action_size=2)
agent.save("agent.keras")
And loaded it as such:
agent = DQNAgent(state_size=5, action_size=2)
agent.load("agent.keras")
agent.epsilon = 0