Solve module 'tensorflow' has no attribute 'placeholder' by hand

1. Problem background : When building a neural network and adding a convolutional layer, an error occurs

face_recigntion_model.add(Conv2D(32,3,3,input_shape=(IMAGE_SIZE,IMAGE_SIZE,3),activation='relu'))

AttributeError: module 'tensorflow' has no attribute 'placeholder'

2. The reason for the error : It may be due to the version problem of tf.placeholder. tf.placeholder is something of tensorflow1.x version, and tensorflow2.0 cannot be used

Check your TensorFlow version print(tf.__version__)

Mine is 2.8.0, need to downgrade the version

3. Solution:

method 1:

Modify import tensorflow as tf to

import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()

Method 2:

Modify import tensorflow as tf to

import tensorflow.compat.v1 as tf

tf.compat.v1.disable_eager_execution()

Method 3: I can’t use the above two methods. I need to manually downgrade the version of TensorFlow. The way is to change the high version of python to a low version of python to install the low version of tensorflow.

1. Uninstall the existing TensorFlow first, and enter in the terminal:

pip uninstall tensorflow to uninstall

2. Reduce the compilation environment to python3.6

Since there is only version 2.x of tensorflow installed above python3.7, it is necessary to reduce the compilation environment to python3.6

Install python3.6, enter in the terminal:

conda create --name py36 python=3.6 anaconda

Restart pycharm, activate python3.6, and enter in the terminal:

activate py36

To verify the environment, enter in the terminal:

python –version

The output is:

Python 3.6.13 :: Anaconda, Inc.

If it is still the original version, you need to restart pycharm to check the version.

3. Install a lower version of TensorFlow

Install TensorFlow1.4.0 , enter in the terminal:

pip install tensorflow==1.4.0 -i Simple Index

Successful installation

PS: Many needed packages may be missing in the new compilation environment, you need to use pip install to download related packages

4. Additional follow-up questions :

Lack of cv2, sklearn... Install via pip in the terminal

pip install opencv-python -i Simple Index

pip install scikit-learn -i https://pypi.douban.com/simple

Continue to report an error: cannot import name 'tf2'

Reason: The version of keras does not match the version of tensorflow, the terminal input:

pip list to find the version of TensorFlow

tensorflow             1.4.0

Find the corresponding keras version in the corresponding table and install it through pip

pip install keras==2.0.8 -i https://pypi.douban.com/simple

Run the program every time you solve a problem, and solve new problems when you encounter new problems until the program can continue to run without reporting errors.

5. Personal experience

If the methods provided on the Internet can't solve the problem, don't keep looking for new answers. You should first understand what the cause of the problem is, and then solve it. For example, the main problem of this error report is that the version of TensorFlow is wrong, so it doesn’t matter what code is entered and whether it is useful or not. The important thing is to find and complete the replacement work of the lower version of TensorFlow.

If I solve your problem, please give me a thumbs up to cheer me on , and thank you for the references in the process of solving the problem. .

 Main reference:

Change the high version py and tensorflow to the low version python to install the low version tensorflow

 

Guess you like

Origin blog.csdn.net/MOZHOUH/article/details/124250715