google / edward2

A simple probabilistic programming language.
Apache License 2.0
678 stars 75 forks source link

Cannot pass a random variable to TransformedDistribution #325

Open AdrienCorenflos opened 4 years ago

AdrienCorenflos commented 4 years ago

Hi,

I am using edward2==0.0.1 in combination with tensorflow_probability==0.9.0 and tensorflow==2.1.0, all installed from pip.

I am facing an issue with ed.TransformedDistribution that feel like a regression to me.

import edward2 as ed
import tensorflow_probability as tfp
tfb = tfp.bijectors

normal = ed.Normal(0., 1.)
shifted_normal = ed.TransformedDistribution(normal, tfb.Shift(0.5))
# This raises with AttributeError: 'RandomVariable' object has no attribute 'name'

I can patch the case 1 by passing normal.distribution to ed.TransformedDistribution but I don't think this is desirable.

dustinvtran commented 4 years ago

Thanks for raising this! In the code, we minimally wrap all distributions as ed2 rvs, and the input types are the same as distributions (this is why ed.TransformedDistribution still takes a distribution as input).

One way to resolve this is to augment all higher-order distributions that take a distribution as input to take a RandomVariable as input (that is, raising all tfp.Distribution types to ed.RandomVariable types). It's easy to do this on a one-off basis, but we haven't quite figured out how to automate this which would be better for maintenance.

AdrienCorenflos commented 4 years ago

Ah yes that sounds tricky if you don't want to break the open close principles.

Also even doing it on a case by case basis may prove complicated. For example, someone coded a distribution by subclassing the Transformed Distribution instead of composing it (I might send a PR for that one...), so you wouldn't be able to check the bases of the classes not their attributes either...