rstudio / keras3

R Interface to Keras
https://keras3.posit.co/
Other
831 stars 282 forks source link

TypeError: can't pickle PyCapsule objects when saving variational autoencoder model #90

Closed skeydan closed 7 years ago

skeydan commented 7 years ago

Hi,

when trying to save the VAE model from

https://rstudio.github.io/keras/articles/examples/variational_autoencoder.html

I get

vae %>% save_model_hdf5("test.h5") Error in py_call_impl(callable, dots$args, dots$keywords) : TypeError: can't pickle PyCapsule objects

Detailed traceback: File "/home/key/software/anaconda3/envs/r-tensorflow/lib/python3.6/site-packages/tensorflow/contrib/keras/python/keras/models.py", line 126, in save_model 'config': model.get_config() File "/home/key/software/anaconda3/envs/r-tensorflow/lib/python3.6/site-packages/tensorflow/contrib/keras/python/keras/engine/topology.py", line 2092, in get_config return copy.deepcopy(config) File "/home/key/software/anaconda3/envs/r-tensorflow/lib/python3.6/copy.py", line 150, in deepcopy y = copier(x, memo) File "/home/key/software/anaconda3/envs/r-tensorflow/lib/python3.6/copy.py", line 240, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "/home/key/software/anaconda3/envs/r-tensorflow/lib/python3.6/copy.py", line 150, in deepcopy y = copier(x, memo) File "/home/key/software/anaconda3/envs/r-tensorflow/lib/python3.6/copy.py", line 215, in _deepcopy_list append(deepcopy(a

With the same example in Python, it works.

Also, when I save the encoder part only, it works too. Could the lambda layer be the culprit (just guessing)?

Any help greatly appreciated!

Thanks Sigrid

jjallaire commented 7 years ago

I think you should try to save and restore just the model weights which you can do as follows: https://rstudio.github.io/keras/reference/save_model_weights_hdf5.html

That will get around the issue of attempting to persist the lambda layer (which is an R function that Keras doesn't know how to persist).

skeydan commented 7 years ago

Ok thank you! I had tried that (successfully) in combination with model_to_json, which didn't work (which makes sense now). So now I know I have to recreate the graph in code and then load the weights. Thanks!