-
Notifications
You must be signed in to change notification settings - Fork 19.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot save optimizer weights due to h5 error "object header message is too large" #11104
Comments
Do I understand this correctly that you're working with a model that has a quite large amount of weights? I'm guessing more than 2200 or so. Unfortunately it doesn't look like the names of these variables can be controlled, excepted for the name of the optimizer. So if you exceed those 64k by just a small amount you could perhaps save 3 bytes per variable by doing the following, but it really won't save you much: class A(Adam):
pass While you wait for this to be fixed properly, you could try to just do |
Could you provide a minimal script to help us narrow down the possible bug? Thank you. |
Here is a sample code that reproduces the error. It takes quite a bit of time though. 10 mins to save. Not sure why. import numpy as np
from keras.models import Model, Input
from keras.layers import Conv2D, Concatenate, GlobalAveragePooling2D
from keras.optimizers import Adam
inp = Input(shape=(10, 10, 1))
layersC = []
for i in range(10):
layers = []
for j in range(40):
x = Conv2D(1, (1, 1))(inp)
layers.append(x)
layersC.append(Concatenate()(layers))
out = Concatenate()(layersC)
out = Conv2D(1, (1, 1))(out)
out = GlobalAveragePooling2D()(out)
m = Model(inputs=inp, outputs=out)
m.compile(optimizer=Adam(1e-4), loss='mse')
m.summary()
x = np.array(np.random.normal(size=(100, 10, 10, 1), loc=0, scale=1))
y = np.array(np.random.normal(size=(100, 1), loc=0, scale=1))
m.fit(x=x, y=y)
symbolic_weights = getattr(m.optimizer, 'weights')
if symbolic_weights:
weight_names = []
for i, w in enumerate(symbolic_weights):
if hasattr(w, 'name') and w.name:
name = str(w.name)
else:
name = 'param_' + str(i)
weight_names.append(name.encode('utf8'))
print(np.array(weight_names).nbytes)
m.save('model') |
I could indeed reproduce the issue with this script. Thanks for the detailed report. |
Linked to issue #6766 PR welcome. |
When trying to save my model I get the runtime error below. There was a similar issue when model layers names were too long and it can be solved by giving layers shorter names. This time the error pops up when saving optimizer weights. getattr(model.optimizer,'weights') shows
and if I convert it to numpy array its length is above the 64k limits which gives h5 runtime. I can save the model if I use save_model(....,include_optimizer=False) but I need the optimizer state. Is there any way I can reduce the length of "training/Adam/Variable:0"... names so as to fit them into 64k hdf5 table limit. Thanks.
Please make sure that the boxes below are checked before you submit your issue. If your issue is an implementation question, please ask your question on StackOverflow or join the Keras Slack channel and ask there instead of filing a GitHub issue.
Thank you!
Check that you are up-to-date with the master branch of Keras. You can update with:
pip install git+git://github.com/keras-team/keras.git --upgrade --no-deps
If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found here.
If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with:
pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps
Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).
The text was updated successfully, but these errors were encountered: