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
Passing variable to Lambda function and loading model JSON #5396
Comments
I addressed a similar error to this in PR #5350. Keras seems to have a freeze on PRs while Keras 2 is being finalized. You can install my branch with Alternatively I believe saving the model to YAML and the weights separately should also work. Edit: Just realized this isn't exactly the same error, it's for a different arg, but a similar solution should work. The basic issue is that JSON does not preserve tuples and converts them to lists but |
I had tried your fix, previously, but with no success. When I converted the closure argument to a list:
the error that I get, then, is:
|
I'm getting the same error, If I try saving the model separately as yaml, this still doesn't work. In this case I get |
It's probably worth noting that I get the same exact bug if I try to save the model in the variational autoencoder example here. This problem definitely has to do with Lambdas, because if you change line 40 to say "z_mean" instead of "z", this problem goes away. |
Yeah, it looks like Keras doesn't properly handle serializing functions with closures. It dumps the contents of the closure but doesn't simulate having a closure when loading the dumped contents. Creating closure cells isn't straightforward but there's a way suggested in this StackOverflow post. Here's a workaround: Pass in every variable in the closure as an argument. When creating the Lambda layer supply the additional arguments with Lambda(f, arguments=kwargs). Another workaround is to never serialize your model, just do |
Hello all, Since I added a Lambda layer in my code, I'm getting an error of loading my saved keras model by calling load_model function. Here is the code of the Lambda:
And the error:
Is there any thought to fix? |
Just ran into this, FYI, workaround from @allanzelener (passing in every variable) worked for me |
I seem to have a similar issue, but aren't quite sure how to implement the suggested workaround.
|
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. |
Dear all, |
I'm not sure if I'm just ignorant of how Lambda functions work, but if I do the following:
x = Lambda(lambda x: x * 0.1)(x)
I have no problem saving and loading a model. However, if I try to pass a variable:
x = Lambda(lambda x: x * factor)(x)
I get the following error on the function closure when loading a model:
When I printed out the closure when the model failed, I get whatever that value passed to the variable was - [0.1], for example. I can train and run models, no problem, but I just can't load from the JSON.
Relevant JSON of what produces the error - getting a [0.1] where it expects a null:
{"class_name": "Lambda", "config": {"function": ["c\u0001\u0000\u0000\u0000\u0001\u0000\u0000\u0000\u0002\u0000\u0000\u0000\u0013\u0000\u0000\u0000s\b\u0000\u0000\u0000|\u0000\u0000\u0088\u0000\u0000\u0014S(\u0001\u0000\u0000\u0000N(\u0000\u0000\u0000\u0000(\u0001\u0000\u0000\u0000t\u0001\u0000\u0000\u0000x(\u0001\u0000\u0000\u0000t\u000e\u0000\u0000\u0000scale_residual(\u0000\u0000\u0000\u0000s-\u0000\u0000\u0000/home/jesse/Source/dl_code/mdl/model_keras.pyt\b\u0000\u0000\u0000<lambda>5\u0004\u0000\u0000s\u0000\u0000\u0000\u0000", null, [0.1]], "name": "lambda_1", "trainable": true, "function_type": "lambda", "arguments": {}, "output_shape": null, "output_shape_type": "raw"}, "inbound_nodes": [[["batchnormalization_18", 0, 0]]], "name": "lambda_1"}
JSON of working model:
{"class_name": "Lambda", "config": {"function": ["c\u0001\u0000\u0000\u0000\u0001\u0000\u0000\u0000\u0002\u0000\u0000\u0000S\u0000\u0000\u0000s\b\u0000\u0000\u0000|\u0000\u0000d\u0001\u0000\u0014S(\u0002\u0000\u0000\u0000Ng\u009a\u0099\u0099\u0099\u0099\u0099\u00b9?(\u0000\u0000\u0000\u0000(\u0001\u0000\u0000\u0000t\u0001\u0000\u0000\u0000x(\u0000\u0000\u0000\u0000(\u0000\u0000\u0000\u0000s-\u0000\u0000\u0000/home/jesse/Source/dl_code/mdl/model_keras.pyt\b\u0000\u0000\u0000<lambda>5\u0004\u0000\u0000s\u0000\u0000\u0000\u0000", null, null], "name": "lambda_1", "trainable": true, "function_type": "lambda", "arguments": {}, "output_shape": null, "output_shape_type": "raw"}, "inbound_nodes": [[["batchnormalization_18", 0, 0]]], "name": "lambda_1"}
The text was updated successfully, but these errors were encountered: