-
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
Can't load_model with error “Optimizer weight shape (256, 32) not compatible with provided weight shape (4,)” #4044
Comments
Similar problem is mentioned in issue #3964. I tried hadikazemi solution. It worked for me. |
@xisnu Thanks for your advice! I trained many models and saved into many files, so it is laborious to delete the optimiser_weight manualy which advised by issue #3964.
|
In my case, the problem was that on one machine everything was fine, on another I got "Optimizer weight shape ... not compatible with provided weight shape ..." exception. The problem turned out to be in Keras installed using different methods on different machines. Although it had the same version 1.1.0 on both the machine that built the model and the machine that loaded the model, the files in /usr/local/lib/python2.7/dist-packages/keras/ were different (in particular, /usr/local/lib/python2.7/dist-packages/keras/models.py). Uninstalling and re-installing Keras on the problematic machine, solved the error, since now files in /usr/local/lib/python2.7/dist-packages/keras/ on both the machine that built the model and the machine that loaded the model were identical (to what I had on the machine that built the model). |
Confirming this issue with loading models stored after train. Issue is with weights shapes; it helps to remove optimizer from the model file. Then it loads successfully. I updated Keras to recent 1.1.0, and use Python 2.7.12 from Anaconda 4.0.0 but it does not help. I also found that if you load model in Spyder 2 times, then second load if fine. Seems to be really cool if the issue is fixed. Thanks |
Sorry, I don't really get your point. So you're saying, |
@fsonntag https://github.com/fchollet/keras/blob/master/keras/backend/theano_backend.py#L67 |
Yes, Theano variables have a |
Do you mean creating a variable without a name? Maybe you should change it to |
You have to save by the latest git version and load_model() by the latest git version. |
I fix it by cloning the repo and reinstall keras |
我模型算完了,就不重装了。 |
I am using Keras 1.1.1 and am having the same problem. Deleting the optimizer weights as a workaround works for me. Just in case someone needs to do the same, here's the code: import h5py
f = h5py.File('model_file.h5', 'r+')
del f['optimizer_weights']
f.close() |
I can confirm the same issue. Deleting the However, I'm working on a project where I train for N number of epochs, stop training, adjust learning rate, then re-load and continue training the model. In this case, deleting my
This error makes sense since the optimizer attribute has been deleted. But as it currently stands, I can't figure out how to:
Without the error others in this thread have mentioned. EDIT: Spun up a new virtual environment with Keras > 1.2 installed and was able to resume training. |
Another solution is to name each layer in the model. |
@WarpTime Have you tested it for a model with CTC loss? Because that has been a problem for me. |
@xisnu No. I have only tested it on crossentropy. |
Sample problem here using SGD and categorical_crossentropy. |
Something like this.... model = Sequential() |
After updating from Keras 1.X to Keras 2.0.1, I have the same problem:
Deleting the I updated all my models with
Now it is working again. Also: Is it possible to save the model without optimizer parameters? |
The code was crashing when loading back a model saved with ModelCheckpoint. This is a bug in keras 1.1.1: keras-team/keras#4044
The following codes really worked! Thank you!
|
I had the same problem and what it seemed to work for me was to set the
|
in case you are having issue in deleting the keys from the model, check this out: https://stackoverflow.com/questions/44236522/keyerror-couldnt-delete-link-cant-delete-self |
It seems that the problem of loading optimizer weights is patched in Keras 2.0.8. The patch does not address the reason why the weights have different size but at least you no longer get an exception. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed. |
@oarriaga It works for me, thank you ! |
Closing as this is resolved |
I have 3 trained model file.
The model
concat.h5
is fine-tuned by concatenating from the two pre-trained model as the initial model(left_branch.h5
,right_branch.h5
).While
left_branch.h5
andright_branch.h5
model file can be load by function keras.models.load_model(),but I load the trained
concat.h5
formatted model file, I get the error blew.The text was updated successfully, but these errors were encountered: