keras: Can't load_model with error “Optimizer weight shape (256, 32) not compatible with provided weight shape (4,)”
I have 3 trained model file.
- left_branch.h5
- right_branch.h5
- concat.h5
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
and right_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.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 167, in load_model
model.optimizer.set_weights(optimizer_weight_values)
File "/usr/local/lib/python2.7/dist-packages/keras/optimizers.py", line 97, in set_weights
'provided weight shape ' + str(w.shape))
Exception: Optimizer weight shape (256, 32) not compatible with provided weight shape (4,)
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 9
- Comments: 28 (9 by maintainers)
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:
I had the same problem and what it seemed to work for me was to set the
compile
flag toFalse
in theload_model
function. Afterwards one can compile the model with the previously used optimizer.After updating from Keras 1.X to Keras 2.0.1, I have the same problem:
Deleting the
optimizer_weights
group (dataset? object? directory? what is the right term?) from the HDF5 file withhdf5view
fixed it as @dteoh suggested.I updated all my models with
Now it is working again.
Also: Is it possible to save the model without optimizer parameters?
The following codes really worked! Thank you!
@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. Finally, I solved it by a using load_weights, as follows:
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).
I can confirm the same issue. Deleting the
optimizer_weights
as suggested by @dteoh works if your model is already trained.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
optimizer_weights
would lead to the error:AttributeError: 'Model' object has no attribute 'optimizer'
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.
@fsonntag I think the problem comes from https://github.com/fchollet/keras/commit/028aae19bf5ae6efe0b32d25d1c700224eebfcf9
If you do not use
weights.sort(key=lambda x: x.name)
, weights order will be different from when it is saved.weights.sort(key=lambda x: x.auto_name)
should not be used.All the weights should have proper names and their order should be stable.
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