tensorflow: tf.experimental.numpy fails with plt.hist

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): using example from TF docs
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): v2.4.0-rc4-71-g582c8d236cb 2.4.0
  • Python version: 3.8.7
  • Bazel version (if compiling from source):
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version: 11.0 / 8.0.4
  • GPU model and memory: GTX 1660 Ti, 6 GB

Describe the current behavior Just trying to plot a histogram from a random standard normal distribution array made with tf.experimental.numpy The code is copied from here: https://www.tensorflow.org/guide/tf_numpy What I get is a bunch of random lines in the histogram. The values in the random matrices AFAICT seem fine - look like a normal distribution.

bad

Describe the expected behavior I should get the normal distribution bell curve in the histogram image.

good

Standalone code to reproduce the issue

import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
import tensorflow.experimental.numpy as tnp

# bad image - see above
labels1 = 15 + 2 * tnp.random.randn(1000)
_ = plt.hist(labels1)

# good image - see above
labels2 = 15 + 2 * np.random.randn(1000)
_ = plt.hist(labels2)

I run everything in Jupyter Notebook in Chrome.

Tensorflow is actually running on the GPU.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 20 (7 by maintainers)

Most upvoted comments

Found the culprit: matplotlib 3.3.4 gives the bad image, while 3.2.2 gives the good image.

I tried that call, and with my version I get this:

AttributeError                            Traceback (most recent call last)
<ipython-input-2-a739f37bf140> in <module>
----> 1 tnp.experimental_enable_numpy_behavior()

AttributeError: module 'tensorflow.experimental.numpy' has no attribute 'experimental_enable_numpy_behavior'

experimental_enable_numpy_behavior only exists and is necessary in tf-nightly. If you’re using other versions, you can keep using tnp in the old way.

@amahendrakar I was able to reproduce the issue(bad results) with tensorflow versions 2.3.0 and 2.4.0 in windows 10 . But the same versions produces the expected/good results when used in colab. I think this is a os specific issue. I want to contribute/help in this. Can you please giude me?