tensorflow / tensorflow/probability

Inconsistent behavior tfp.distributions.Bernoulli tf.float vs. np.float

Open
#676 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
4.4k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

Since the Bernoulli distribution is implemented for real numbers, I was testing a bit to see what it will do. There, I found an interesting behavior difference between tf.floats, python floats and numpy floats.
This test was done with the following versions:
numpy = 1.17.4
tensorflow = 1.14.0
tensorflow_probability = 0.7.0

float_tf_bug

Both behaviors make sense, but an inconsistency between numpy floats and tf/python floats seems weird.

I used the following code to run the experiments:

import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
import matplotlib.pyplot as plt

prob = np.array([0.9])
bern = tfp.distributions.Bernoulli(probs=prob)

x = np.arange(-2, 2, 0.01)
numpy_64_x = np.array(x, dtype=np.float64)
numpy_32_x = np.array(x, dtype=np.float32)
tf_64_x = tf.constant(x, dtype=tf.float64)
python_x = [float(el) for el in x]

sess = tf.Session()
sess.run(tf.global_variables_initializer())

numpy_64_y = sess.run(bern.prob(numpy_64_x))
numpy_32_y = sess.run(bern.prob(numpy_32_x))
tf_64_y = sess.run(bern.prob(tf_64_x))
python_y = sess.run(bern.prob(python_x))

# Just for plotting
lw = 5
plt.plot(x, numpy_64_y, label="np.float64", lw=lw)
plt.plot(x, numpy_32_y, label="np.float32", lw=lw, ls=":", zorder=10)
plt.plot(x, python_y, label="python float", lw=lw)
plt.plot(x, tf_64_y, label="tf.float64", lw=lw, ls=":", zorder=20)
ax = plt.gca()
ax.legend()
plt.show()

PS: tf.float64 had the same behavior as tf.float32
Same behavior for tf.contrib.distributions.Bernoulli

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied Python example with the listed NumPy, TensorFlow, and TensorFlow Probability versions, then inspect Bernoulli.prob handling for NumPy, Python, and TensorFlow float inputs. Done means the intended behavior is established and the inconsistent result is either corrected or documented with a regression check for the affected input types.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.