TensorFlow中遇错合集
- 一、`AttributeError: module 'tensorflow' has no attribute 'placeholder'`
- 二、`RuntimeError: tf.placeholder() is not compatible with eager execution.`
- 三、`pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.`
- 四、`Could not find a version that satisfies the requirement Levenshtein (from versions: none)`
一、AttributeError: module 'tensorflow' has no attribute 'placeholder'
错误原因
- tensorflow版本问题:当前tensorflow版本为2.X,而tensorflow 2.0版本去掉了placeholder。tensorflow 1.*版本才有placeholder。
解决方案
- “向后兼容”。这种做法可以在新版本的TensorFlow中仍然使用旧的API,确保旧代码的兼容性。
具体实现
- 修改
import tensorflow as tf
为import tensorflow._api.v2.compat.v1 as tf
二、RuntimeError: tf.placeholder() is not compatible with eager execution.
错误原因
- tf.placeholder()意味着被提供给会话,该会话在运行时从feed dict接收值并执行所需的操作。(也就是默认为急切运行,我们爆出这个错误说明不需要急切运行)
解决方案
方式一:
- 在
tf.placeholder()
被调用前添加tf.compat.v1.disable_eager_execution()
方式二:
import tensorflow._api.v2.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
三、pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
报错信息:
- pip._小贩urllib3.异常。读取超时错误:Http连接池(主机=‘files.pythonhosted.org’,端口=443):读取超时。
错误原因:
- 使用pip安装模块的时候由于网络波动原因就会出现 pip._ vendor. urllib3. exceptions.ReadTimeoutError: HTTPSConnectionPool (host=’ files. pythonhosted.org’, port=443) : Read timed out.,小伙伴按照下面的命令使用国内的镜像地址安装即可!!!
问题解决:
- 设置国内镜像源,设置超时(替换模块名称即可):
pip --default-timeout=100 install 模块名称 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
--trusted-host pypi.douban.com
:表示信任豆瓣镜像地址
切换为其他国内镜像地址(替换命令中的网址即可):
- 清华:
https://pypi.tuna.tsinghua.edu.cn/simple
- 阿里云:
http://mirrors.aliyun.com/pypi/simple/
- 中国科技大学:
https://pypi.mirrors.ustc.edu.cn/simple/
- 华中科技大学:
http://pypi.hustunique.com/
四、Could not find a version that satisfies the requirement Levenshtein (from versions: none)
报错信息:
错误原因:
错误:找不到满足要求的版本<软件包>(来自版本:无)
错误:找不到匹配的<软件包>分布
错误解决:
-
首先确保输入软件包名称和版本号没有问题。
-
保证计算机连接网络。
-
更新pip:
pip install --upgrade pip
-
更换pip源 :
pip install <软甲包> -i https://pypi.tuna.tsinghua.edu.cn/simple/
-
更多请参考:解决参考