Error when running tensorflow in virtualenv: module 'tensorflow' has no attribute 'truncated_normal'

Viewed 9356

I have the following error when running a CNN made in keras

File "venv/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 4185, in truncated_normal return tf.truncated_normal(shape, mean, stddev, dtype=dtype, seed=seed) AttributeError: module 'tensorflow' has no attribute 'truncated_normal'

I have already installed and reinstalled Tensorflow 2.0 several times. What could be happening?

3 Answers

For TewnsorFlow 2.x and keras 2.3 or the above, the following usage is more adaptable. Users do not need to downgrade both TensorFlow and Keras.

# Add the import statements 
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

# Change it to the sample expression as follows. 
init = tf.compat.v1.random.truncated_normal()

Cheers!

In Tensorflow v2.0 and above, "tf.truncated_normal" replaced with "tf.random.truncated_normal"

Keras 2.2.4 does not support TensorFlow 2.0 (it was released much before TF 2.0), so you can either downgrade TensorFlow to version 1.x, or upgrade Keras to version 2.3, which does support TensorFlow 2.0.

Related