2021年最新完整统计TensorFlow2.x报错记录与解决方法「希望能帮助到你」

在这里插入图片描述

报错1:module ‘tensorflow’ has no attribute ‘set_random_seed’

原因:在tensorflow 2.x中应该是: tf.random.set_seed

报错2:module ‘tensorflow’ has no attribute ‘GPUOptions’

修改为:tf.compat.v1.

报错3:RuntimeError: set_session is not available when using TensorFlow 2.0.

修改为:tf.compat.v1.keras.backend.set_session()

报错4:AttributeError: module ‘tensorboard.summary._tf.summary’ has no attribute ‘FileWriter’

将tf.summary.FileWriter()换成tf.summary.create_file_writer()

报错5:AttributeError: module ‘tensorflow’ has no attribute ‘get_default_graph’

修改为:使用tf.compat.v1.get_default_graph获取图而不是tf.get_default_graph。

报错6:AttributeError: ‘_thread._local’ object has no attribute ‘value’

解决办法:重新创建一个虚拟环境,版本号对应为:
TensorFlow 1.14.0 + Keras 2.2.5 on Python 3.6.

conda create -n tf1.14 python=3.6
pip3 install --index-url https://pypi.douban.com/simple tensorflow-gpu==1.1.4
pip3 install --index-url https://pypi.douban.com/simple Keras== 2.2.5
pip3 install --index-url https://pypi.douban.com/simple opencv-python==4.1.1.26或pip install opencv-python==4.1.1.26

注意:如果还是报错,那就去pycharm设置的配置环境中安装py-opencv即可

pip install astunparse
pip install opt-einsum
pip install numpy==1.15.4 

报错7:RuntimeError: The Session graph is empty. Add operations to the graph before calling run().

问题产生的原因:无法执行sess.run()的原因是tensorflow版本不同导致的,tensorflow版本2.0无法兼容版本1.0.
解决办法:
tf.compat.v1.disable_eager_execution()

测试代码:

import os
import cv2
import math
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
print(tf.__version__)

for i in range(3):
    tf.compat.v1.disable_eager_execution()#保证sess.run()能够正常运行
    tf.compat.v1.set_random_seed(1234)
    generate = tf.compat.v1.random_uniform((10,), 0, 10)
    with tf.compat.v1.Session() as sess:
        b = sess.run(generate)
        print(b)

报错8: import cv2 ImportError: numpy.core.multiarray failed to import

原因:由于 Tensorflow 不是最新版本,而 numpy 最新,可能是两者兼容性问题
命令:pip install numpy==1.18.2 -i https://pypi.tuna.tsinghua.edu.cn/simple/

一般情况下是由于numpy版本过高导致的

报错9:ModuleNotFoundError: No module named ‘numpy.core._multiarray_umath’

解决提升:tensorflow-cpu 2.2.0 requires numpy<2.0,>=1.16.0, but you have numpy 1.12.0 which is
incompatible.

报错10:安装numpy部分成功后报错:

tensorflow-cpu 2.2.0 requires scipy==1.4.1; python_version >= “3”, which is not installed.
matplotlib 3.3.0 requires kiwisolver>=1.0.1, which is not installed.
matplotlib 3.3.0 requires pillow>=6.2.0, which is not installed.
matplotlib 3.3.0 requires pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.3, which is not installed.
ble.
tensorflow-cpu 2.2.0 requires h5py<2.11.0,>=2.10.0, but you have h5py 3.1.0 which is
incompatible.
tensorflow-cpu 2.2.0 requires numpy<2.0,>=1.16.0, but you have numpy 1.12.0 which is
incompatible.
tensorflow-cpu 2.2.0 requires tensorboard<2.3.0,>=2.2.0, but you have tensorboard 1.14.0 which is incompatible.
tensorflow-cpu 2.2.0 requires tensorflow-estimator<2.3.0,>=2.2.0, but you have tensormatplotlib 3.3.0 requires numpy>=1.15, but you have numpy 1.12.0 which is incompatible.
tensorflow-gpu 1.14.0 requires numpy<2.0,>=1.14.5, but you have numpy 1.12.0 which is incompatible

报错11:tf2_cpu:RuntimeError: The Session graph is empty. Add operations to the graph before calling run().

原因:无法执行sess.run()的原因是tensorflow版本不同导致的,tensorflow版本2.0无法兼容版本1.0.
解决办法:
tf.compat.v1.disable_eager_execution()

报错12:tf2_cpu:RuntimeError: tf.placeholder() is not compatible with eager execution.

将tf替换为tf.compat.v1.disable_eager_execution()

numpy from versions:

1.3.0, 1.4.1, 1.5.0, 1.5.1, 1.6.0, 1.6.1, 1.6.2, 1.7.0, 1.7.1, 1.7.2, 1.8.0, 1.8.1, 1.8.2, 1.9.0, 1.9.1, 1.9.2, 1.9.3, 1.10.0.post2, 1.10.1, 1.10.2, 1.10.4, 1.11.0b3, 1.11.0rc1, 1.11.0rc2, 1.11.0, 1.11.1rc1, 1.11.1, 1.11.2rc1, 1.11.2, 1.11.3, 1.12.0b1, 1.12.0rc1, 1.12.0rc2, 1.12.0, 1.12.1rc1, 1.12.1, 1.13.0rc1, 1.13.0rc2, 1.13.0, 1.13.1, 
1.13.3, 1.14.0rc1, 1.14.0, 1.14.1, 1.14.2, 1.14.3, 1.14.4, 1.14.5, 1.14.6, 1.15.0rc1, 1.15.0rc2, 1.15.0, 1.15.1, 1.15.2, 1.15.3, 1.15.4, 1.16.0rc1, 1.16.0rc2, 1.16.0, 1.16.1, 1.16.2, 1.16.3, 1.16.4, 1.16.5, 1.16.6, 1.17.0rc1, 1.17.0rc2, 1.17.0, 1.17.1, 1.17.2, 1.17.3, 1.17.4, 1.17.5, 1.18.0rc1, 1.18.0, 1.18.1, 1.18.2, 1.18.3, 1.18.4, 1.18

http://tflearn.org/installation/

报错13:from keras.application.xception import Xception

ModuleNotFoundError: No module named ‘keras.application’

解决方法:改为keras_applications
import tensorflow.keras.applications.ResNet50
from keras_applications.resnet import ResNet50

import tensorflow.keras.applications.ResNet50
from keras.applications.resnet50 import ResNet50

报错14:set_session is not available when using TensorFlow 2.0.

若是遇到如题错误:keras.backend.tensorflow_backend.set_session(tf.compat.v1.Session(config=config))

改为tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config))

报错15:module ‘tensorflow’ has no attribute ‘enable_eager_execution’

解决办法:tf.compat.v1.enable_eager_execution()

报错16:module ‘tensorboard.summary._tf.summary’ has no attribute ‘FileWriter’

解决办法:将原来使用的tf.summary.FileWriter()换成tf.train.SummaryWriter()

或者将tf.summary.FileWriter()换成tf.summary.create_file_writer(),解决问题!

猜你喜欢

转载自blog.csdn.net/weixin_41194129/article/details/112745576