"Neural Networks" example doesn't work

Hello All,

I am attempting to use a keras neural net in one of my Edward models, and running into trouble. Essentially I am taking a keras Model and trying to apply it to an Edward RandomVariable, which doesn’t seem to work.

From the documentation here this would be achieved simply:

from edward.models import Bernoulli, Normal
from keras.layers import Dense

z = Normal(loc=tf.zeros([N, d]), scale=tf.ones([N, d]))
h = Dense(256, activation='relu')(z)
x = Bernoulli(logits=Dense(28 * 28)(h))

however when I try I get the following error:

ValueError: Layer dense_1 was called with an input that isn't a symbolic tensor. Received type: <class 'abc.Normal'>. Full input: [<ed.RandomVariable 'Normal/' shape=(10, 4) dtype=float32>]. All inputs to the layer should be tensors.

It seems that keras doesn’t play nice with edward. Is there a work around?

Thanks!

You need to call Dense this way:

h = Dense(256, activation='relu')(z.value())

Keras layers stopped accepting instances of ed.RandomVariable as input starting with version 2.0.5 (here is an issue about that).

2 Likes

Awesome, thanks. This is also the solution in my applcation where I am calling a keras Model. Thanks @bkayalibay!

1 Like