2

If yes, then, how did they do that? I mean, say that I have a custom-built model via subclassing. My optimizer is a separated object. How is it that one command save weights of two different objects? In particular, how does it know that those two objects are related? Is it due to magic done by model.compile?

EDIT: I just realized that a model has an attribute model.optimizer, is that how Keras do it? make the optimizer an attribute of a model and will be saved with it?

Alex Deft
  • 2,531
  • 1
  • 19
  • 34
  • your question is still unclear! what are u trying to find? the custom function code/formula of the custom optimizer or what ? – smerllo Jul 09 '19 at 03:27
  • @Cs20 How's model.save include weights of an optimizer which is a separated object that is different from the model? – Alex Deft Jul 09 '19 at 05:18

1 Answers1

3

No, model.save_weights() only saves the model's parameters. The state of the stuffs that the model was compiled with (optimizers, callbacks, losses, metrics) will not be saved.

You should use model.save() to save the model's optimizer and other training configurations. Please refer to this documentation (the most straigtforward way to save the optimizer together with the model).

If you for some reason want to use exactly model.save_weights(), please refer to this stackoverflow question on how to save model's optimizer (might be a bit tricky).

Chan Kha Vu
  • 9,834
  • 6
  • 32
  • 64
  • 1
    model.save() is only available to models made with functional API, if you dig deeper and build it with more primitive ways, then it won't be serializable. save weights is the best I can get. – Alex Deft Jul 09 '19 at 11:14