-
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
Unable to load custom initializer from the saved model, passing custom_objects is not working #3867
Comments
I am having the same problem. Are there any updates on this? |
I came across this problem today. A fix is to add your custom init function to the 'initialization' module after it is defined:
This doesn't fully decouple the model from the code as it doesn't allow for any init specific parameters to be serialised out. If it is worth it to extend the initialization design to allow that (think like the edit: my slip up |
As in #1634, mock the get function can also be a possible solution. |
Same issue here. However, using setattr does not solve the problem. Furthermore, I don't understand why when loading a model, the initialization matters. |
I found a workaround to this problem. When you add layers to your model, the "weights" parameters can be initialized with a numpy array. Then you can use numpy to do random initialization, such as: def weights_initialization(inputDim, outputDim, scale = 0.1):
return numpy.sqrt(scale)*numpy.random.randn(inputDim, outputDim), \
numpy.sqrt(scale) * numpy.random.randn(outputDim), |
|
I am having the exactly same issue. I try to pass my custom initializer via |
For anyone interested: My workaround to this situation was to introduce a callable class, which did finally work:
Still I think this shouild be considered a bug, cause the most natural interface/solution would be to use |
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. |
I agree this should be treated as a bug. It is important and a fairly common use case! |
@alxy Can you share the full piece of code? I have the similar problem and tried this solution, but looks like it's not serializable. The error message is:
and The code I am using is:
|
@xiaoyongzhu Do you think this is really related? The error is different from the one I was getting. However, I would think that Also note that keras changed quite a few things, as there have been new versions since then. What I found the most useful solution was to only load the weights, and not the entire model. If you still have the model definition, it is far easier to just do |
i have seen this error posted in several places on the internet, and has been fixed in tensorflowjs but not keras or tf python. my model is culled from early-stopping callback, im not saving it manually. this appears to be common Traceback (most recent call last): |
Same error as the one mentioned by jnorthrup here. |
i don't recall where i encountered this fix but i believe this helped get past the blocker.
with CustomObjectScope({'GlorotUniform': glorot_uniform()}):
model = load_model(
MODEL_PREFIX + '.hdf5') # compile_model(inmetrics=None, dropout_=dropout_, bias_regularizer_=regularize)
model.summary(print_fn=print)
# print(pd.DataFrame(final.history).describe())
plot_prediction(model) |
I solved my issue, I noticed that I had these imports:
by digging into the By changing my keras imports to purely tensorflow ones and tensorflow ones only:
(notice the last line changes) I do not meet the error anymore! /!\ So, to me, it seems that some of the Hope it helps. |
I have a simple custom initializer in the model. When I try to load the model, I get invalid initialization error. I saw similar issues where the suggested solution was to pass
custom_objects
argument to theload_model
function. However, this did not work for me. This is the code to reproduce the problem:Running it throws error at the
load_model
line:I also tried saving and loading with json using the
model_from_json
function, but the same issue appears.The text was updated successfully, but these errors were encountered: