Saving Edward objects with pickle


Trying to save a Normal object with pickle (python 3.4) but get the following error:

_pickle.PicklingError: Can't pickle <class 'abc.Normal'>: attribute lookup Normal on abc failed

To reproduce the error:

import tensorflow as tf
import pickle
from edward.models import Normal

qw = Normal(loc=tf.Variable(tf.random_normal([2, 2])), scale=tf.nn.softplus(tf.Variable(tf.random_normal([2, 2]))))
#Saving the objects:
with open('pnn_results.pickle', 'wb') as f: 
    pickle.dump([qw],  f)

How to save it otherwise?


Edward is built on tensorflow, (for example, Normal and other RandomVariables extend tf.contrib.distributions.Distribution). So your best bet is to take the tensorflow approach to serialisation which is using protocol buffers.

This is probably a good starting point.

As a broader comment, some other libraries built on tensorflow support alternative serialisation formats (for example keras supports serialisation to hd5 files). If you don’t want to use protobuf, you can always evaluate to tf.Variable instance to np.ndarrays, then use numpy’s serialisation tools