1
votes

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
Which version of Keras are you using?Dr. Snoopy
Keras==2.2.4 Keras-Applications==1.0.6 Keras-Preprocessing==1.0.5mad
That version of Keras does not support TensorFlow 2.0, you should downgrade TF or use Keras 2.3Dr. Snoopy
Good to know, let me try your second suggestion and ill give you a feedbackmad
It worked. If you answer my question, Ill surely mark my problem as solved. Thank you very, very much.mad

3 Answers

3
votes

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.

3
votes

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!

2
votes

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