# Why does keras model get bigger after training?

I notice that I create a model using tensorflow.keras.Sequential(), save it and the file size is around 5 MiB, but after I call model.fit(..), the file size is increased to 17 MiB. I copied the model to reduce the filesize and see that the accuracy is the same.

My question is, what exactly is the content of extra 12 MiB that fit() produces? How can I access such content? If I remove those extra 12 MiB, could it affect prediction accuracy or any weird side-effect?

See my experiment code here: https://nbviewer.jupyter.org/github/off99555/TensorFlowExperiments/blob/master/test-save-keras-model.ipynb

• maybe batch normalisation info ? :D hm or dkn maybe some history is stored – user8426627 Jul 16 at 12:53
• @user8426627 See my answer. – off99555 Jul 16 at 13:56
• model.save also saves the optimiser's state. Try model.save_weights – Abhijit Balaji Jul 16 at 14:01
• model.save_weights() doesn't save the model architecture. So it's not convenient for loading the model again. – off99555 Jul 16 at 14:08

## 1 Answer

The answer is that it's the size of the Adam optimizer state. When I change the optimizer to SGD (the vanilla optimizer), the size is not big anymore. As far as I know, the Adam optimizer maintains gradients information of previous training iterations. And the gradient size can be as big as the model size. That's why it causes the file size to be so big.

With this in mind, when you save your model please make sure to set include_optimizer=False if you seem to use an optimizer that maintains big state similar to Adam.

Beware though, it means that you cannot load the model and continue training it again, it should only be used for inference.