【问题标题】:Why can't tensorflow be imported?为什么不能导入tensorflow?
【发布时间】:2023-03-13 16:38:01
【问题描述】:

我刚刚安装了 Tensorflow,并试图在 Python 中运行 test program 来验证安装。当我运行程序时,通常什么都不会发生。 Spyder IDE 崩溃了几次。这是测试程序:

print('test before')
import tensorflow as tf
print('test after')
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

有趣的是,导入 tensorflow 之前的所有行都会运行,即“test before”会被打印出来。但是,该程序似乎在导入 tensorflow 时遇到了麻烦,因为没有打印“test after”。为什么程序不能成功导入tensorflow?

另外,请注意 tensorflow 安装似乎已经成功,因为最后它打印“成功安装 tensorflow-1.7.0”。并且运行程序时没有出现错误。通常,如果在导入包时出现问题,您会收到一条错误消息,例如“找不到 [包]”。在这种情况下,程序只运行几秒钟然后停止(或 IDE 崩溃)而不返回错误。运行python3 -c 'import tensorflow' 时出现错误Illegal instruction (core dumped)

张量流版本:1.7.0 Python版本:3.5.2 使用 Spyder IDE 操作系统:ubuntu 16.04 LTS

【问题讨论】:

  • 您遇到了什么错误?如果没有导入应该报错
  • 从命令行试试这个:python -c 'import tensorflow'。如果这没有打印错误,那么您的安装不是问题
  • 您是否重新启动了 IDE/计算机?
  • 不,我在 Spyder IDE 中没有收到任何错误。
  • 我尝试运行python3 -c 'import tensorflow',它给出了错误Illegal instruction (core dumped)

标签: python tensorflow


【解决方案1】:

事实证明,较新版本的 TensorFlow 使用旧 CPU 不支持的 AVX 指令。这个问题在GitHub issue 和更早的Stack Overflow question 中讨论过。解决方案是将 TensorFlow 降级到 1.5 版,我使用以下命令:

pip3 uninstall tensorflow
pip3 install tensorflow==1.5

降级到 1.5 后,测试代码正常工作。

【讨论】:

    猜你喜欢
    • 2017-07-25
    • 2017-05-21
    • 1970-01-01
    • 2018-08-30
    • 2020-12-23
    • 2018-01-12
    • 2021-11-24
    • 2020-04-24
    • 2017-07-21
    相关资源
    最近更新 更多