Probability: `sample_n` not implemented on Binomial random variable

Created on 13 Jun 2018  Â·  13Comments  Â·  Source: tensorflow/probability

from tensorflow_probability import edward2 as ed
y = ed.Binomial(10., probs=.5)

raises NotImplementedError: sample_n is not implemented. I checked the code, and the function is indeed not defined for the Binomial distribution. Is that correct? That would be an easy fix, but I'd rather have the opinion of someone who is more familiar with the codebase to give it a go.

Most helpful comment

@srvasude is working on a tf.random.binom that should be merged soon(-ish). The TFP binomial sample_n will be implemented shortly after.

All 13 comments

Thanks for reporting this! I'm having trouble replicating, using the most recent tfp-nightly:

>>> from tensorflow_probability import edward2 as ed
>>> y = ed.Bernoulli(probs=.5)
>>> y
<ed.RandomVariable 'Bernoulli_1/' shape=() dtype=int32>

Where are you seeing the error raised? Can you post a stack trace?

Edward ultimately wraps the Bernoulli distribution from core TensorFlow, which does define _sample_n, so I don't think this is an issue of missing implementation so much as perhaps bad plumbing somewhere.

My bad, I was tired and I meant the Binomial distribution (I edited my post). I’m working on the master branch.

Ah, yes you're right, and the fix is exactly to implement Binomial._sample_n(). This was on our radar internally, but it's not clear whether anyone is actively working on it -- let me look into that and get back to you.

In the meantime, you might be able to use the Multinomial distribution (https://www.tensorflow.org/api_docs/python/tf/distributions/Multinomial) as a workaround.

Thanks for the workaround, that’s what I did at first. I also implemented ‘sample_n’ on my local branch, but if you guys did it internally it’s even better :) Let me know, I’d be happy to push my branch.

Following up, it looks like we expect to have a basic sampling implementation checked in in the near future, O(days). That should fix the immediate issue of Binomials in Edward.

In the slightly longer term, we're planning to add more efficient samplers -- the current implementations of multinomial/binomial sampling are pretty naive in that they essentially just sum Bernoulli/Categorical samples, so the runtime and memory requirements scale with the number of trials. Fixing this will require new C++ ops, which are in the pipeline.

Fantastic, thanks! You’d be implementing something like the alias method?

+1 for feature request (also implemented my own workaround, but an efficient centralized version on the main branch would be great)

:+1: for scalable binomial/multinomial sampling

Sorry I've been quite delayed here.

I am cleaning up an old PR to add a binomial c++ sampler to tensorflow. This should let us have binomial sampling. For multinomial, the binomial sampling code should be able to be reused. If k is the number of classes, there should probably be a multinomial sampler that draws O(log(k)) binomials.

I'm running into same issue in PyMC4 when we're trying to use tfp distributions. Is this still on the radar for bugfix?

@csuter just in case you want visibility

For anyone interested we're wrapping tfp.distributions and below is a link to our tests.

https://github.com/pymc-devs/pymc4/blob/f090ceea5eba7f1caea4c3313411ddd6c2e50345/pymc4/tests/test_random_variables.py#L77

Code for reproducibility

import tensorflow as tf
import tensorflow_probability as tfp
from tensorflow_probability import distributions

print(f"Tensorflow version {tf.__version__}")
print(f"TFP version {tfp.__version__}")

with tf.Session() as sess:
    binom = distributions.Binomial(total_count=1, probs=.5)
    binom.sample()

WARNING: The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:

Tensorflow version 1.13.0-dev20190114
TFP version 0.6.0-dev
2019-01-18 08:54:03.034337: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-01-18 08:54:03.056314: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 4200000000 Hz
2019-01-18 08:54:03.056630: I tensorflow/compiler/xla/service/service.cc:162] XLA service 0x55a20738b740 executing computations on platform Host. Devices:
2019-01-18 08:54:03.056641: I tensorflow/compiler/xla/service/service.cc:169] StreamExecutor device (0): ,
Traceback (most recent call last):
File "/home/tfp_user/.PyCharmCE2018.3/config/scratches/binom_test.py", line 14, in
binom.sample()
File "/home/tfp_user/miniconda3/envs/pymc4/lib/python3.6/site-packages/tensorflow_probability/python/distributions/distribution.py", line 690, in sample
return self._call_sample_n(sample_shape, seed, name)
File "/home/tfp_user/miniconda3/envs/pymc4/lib/python3.6/site-packages/tensorflow_probability/python/distributions/distribution.py", line 669, in _call_sample_n
samples = self._sample_n(n, seed, **kwargs)
File "/home/tfp_user/miniconda3/envs/pymc4/lib/python3.6/site-packages/tensorflow_probability/python/distributions/distribution.py", line 660, in _sample_n
type(self).__name__))
NotImplementedError: sample_n is not implemented: Binomial

I also just ran into this problem ...
Are there any news concerning this issue?

@srvasude is working on a tf.random.binom that should be merged soon(-ish). The TFP binomial sample_n will be implemented shortly after.

Was this page helpful?
0 / 5 - 0 ratings