2

I'm reading the official tutorial on save-load in Keras and it seems whether I used save or save_weights methods, then the optimizer parameters are going to be saved at any rate. How can save model's weights only?

Alex Deft
  • 2,531
  • 1
  • 19
  • 34

2 Answers2

2
model.save('./savedmodel.h5', save_format='h5', include_optimizer=False)

If save_format='tf', whether include_optimizer=False or True, it's useless as I tried.

Greenonline
  • 1,330
  • 8
  • 23
  • 31
lou Lian
  • 21
  • 3
1

In Keras, to save model weights, do:

model.save_weights('my_model_weights.h5')

To load model weights:

model.load_weights('my_model_weights.h5')

Also see additional example on saving/loading weights by layer name from here.

DSH
  • 1,038
  • 16
  • 27
  • Yes, but that saves optimizer state as well. If you have a model with 10 million weights, and you used Adam, then Adam itself comes with 40m parameters, even more than the model itself. I don't want that to be saved, I have no intention of resuming training after loading it. – Alex Deft Apr 02 '20 at 01:35
  • 1
    No it doesn't. Btw - you asked this question in another thread [here](https://stackoverflow.com/questions/56942525/does-model-save-weights-include-optimizer-state/56950665) and got the exact response that I wrote. Save_weights only saves the parameters pertaining to model layers, not the optimizer. Reference the docs I sent. – DSH Apr 02 '20 at 03:58
  • Tensorflow gives some errors that make it sounds like it's trying to apply weights from the h5 file, but what's actually happening is there's an uninitialised Optimiser somewhere in the network. If that's your problem the easiest solution is to load the weights by name, and not build the pre-trained model, just replicate the layers. – Steve Harris Feb 10 '21 at 14:44