tensorflow / tensorflow/probability
`JointDistributionCoroutineAutoBatched` seed behaviour with batches
Nobody has claimed this yet.
- Dominant language
- Jupyter Notebook
- Stars
- 4.4k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
Hi all,
I'm trying to compute a posterior predictive distribution over samples from a posterior distribution (Colab here). TFP 0.25 with JAX backend.
My (mre and therefore contrived) model specification is
@tfd.JointDistributionCoroutineAutoBatched
def model_autobatched():
theta = yield tfd.Normal(loc=0., scale=1., name="theta")
yield tfd.Normal(loc=theta, scale=0.1, name="y")
i.e. a Normally-distributed observation model with Normally-distributed mean. To compute the posterior predictive distribution, I wish to sample the y component conditional on a vector of theta samples.
theta_samples = np.arange(5.)
model_autobatched.sample(theta=theta_samples, seed=jax.random.key(0))
giving
StructTuple(
theta=Array([0., 1., 2., 3., 4.], dtype=float32),
y=Array([0.06215769, 1.0621576 , 2.0621576 , 3.0621576 , 4.0621576 ], dtype=float32)
)
Oh dear, we notice that y - theta = constant. This seems to suggest that a single PRNG key is being used for each draw of y given the sample from theta.
Moreover, this approach fails entirely if sample_distributions is called.
model_autobatched.sample_distributions(theta=theta_samples, seed=jax.random.key(0))
ValueError: Attempt to convert a value (<object object at 0x7a53561590d0>) with an unsupported type (<class 'object'>) to a Tensor.
As a workaround, we could use the older JointDistributionCoroutine with Root annotation which works as desired (see Colab)
[edit] actually, JDCoroutine/Root only works because the whole theta vector is passed to y's constructor, not vectorisation over the whole model.
Do we have a bug or a feature, I wonder?
Regards,
Chris
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the linked Colab or the model_autobatched sample and sample_distributions calls with the JAX backend and vectorized theta samples. Trace JointDistributionCoroutineAutoBatched sampling and seed handling; done means independent y draws across the theta batch and no unsupported object-to-Tensor conversion in sample_distributions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- jupyter-notebook, python, tensorflow
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100