简体   繁体   中英

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

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?

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.

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 "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM